

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Difference Between for and while loop
In this post, we will understand the difference between the ‘for’ and the ‘while’ loop.
For loop
The initialization, condition checking, and the iteration statements are written at the beginning of the loop.
It is used only when the number of iterations is known beforehand.
If the condition is not mentioned in the 'for' loop, then the loop iterates infinite number of times.
The initialization is done only once, and it is never repeated.
The iteration statement is written at the beginning.
Hence, it executes once all statements in loop have been executed.
Example
for(initialization; condition; iteration){ //body of the 'for' loop }
Following is the flowchart of for loop −
While condition
The initialization and the condition checking is done at the beginning of the loop.
It is used only when the number of iterations isn’t known.
If the condition is not mentioned in the 'while' loop, it results in a compilation error.
If the initialization is done when the condition is being checked, then initialization occurs every time the loop is iterated through.
The iteration statement can be written within any point inside the loop.
Example
while ( condition) { statements; //body of the loop }
Following is the flowchart of while loop −
- Related Questions & Answers
- Difference Between while and do-while Loop
- What are the differences between while loop and do-while loop in Java?
- For Versus While Loop in C
- How to convert a Python for loop to while loop?
- Python - How to convert this while loop to for loop?
- Difference between while(1) and while(0) in C/C++
- Difference between while(1) and while(0) in C language
- Java while loop
- do…while loop vs. while loop in C/C++
- How can I write in order with for loop or while loop?
- Infinite while loop in Java
- Java do-while loop example
- Java infinite do-while loop
- The while loop in Javascript
- Do-while loop in Arduino