Difference Between For loop and While loop

The ‘for’ loop is used only when the number of iterations are already known. The ‘while’ loop is used when the number of iteration are not known beforehand.

Computers work on High-level languages such as C, C++, Java, Python, etc. Computer languages assist us in getting the results that are difficult to obtain manually.

These high-level languages operate on a defined structure of commands. One of the basic structure of commands used in high-level languages are ‘Loops’.

A loop is a command that tends to repeat itself to obtain the desired result. In other words, a programming command that repeats itself either the known number of times or the unknown number of times to fulfill certain conditions is defined as a loop.

There are various kinds of loops such as for loop, while loop, if loop, if-else loop, if-else-if loop, etc. But the most commonly used loops are for and while loops.

For loop vs While loop

The difference between for loop and while loop is that in for loop the number of iterations to be done is already known and is used to obtain a certain result whereas in while loop the command runs until a certain condition is reached and the statement is proved to be false.

For loop vs While loop

 

Comparison Table

Parameter of ComparisonFor loopWhile loop
CommandThe structure of for loop is –
for(initial condition; number of iterations){//body of the loop }
Structure of while loop is-
While(condition){statements;//body}
IterationsIterates for a preset number of times.Iterates till a condition is met.
ConditionIn the absence of a condition, the loop iterates for an infinite number of times till it reaches break command.In the absence of a condition, while loop shows an error.
InitializationInitialization in for loop is done only once when the program starts.Initialization is done every time the loop is iterated.
UseUsed to obtain the result only when the number of iterations is known.Used to satisfy the condition when the number of iterations is unknown.

 

What is For loop?

A loop in which the control statement is executed for a pre-known number of times to obtain the result is known as the for loop. It can be said that for loop is a repetitive command.

In for loop, the command is controlled by a variable. Each time the loop iterates, the predefined variable gets a new value.

For loop is a set of three segments-       

  1. Initialization
  2. Condition
  3. Updation

During the initialization of for loop, a starting value for the variable is to be declared. This loop requires initialization only once.

After initialization, the compiler checks whether the condition is true or not, and if it is true, the loop continues to iterate till the predefined number of iterations are obtained.

for loop
 

What is While loop?

A while loop is when the command iterates for an uncertain number of times till the condition is true. Once the condition is proved false, the iteration of command stops.

Initialization in while loop is done each time the loop iterates. The compiler checks the condition and if it is proved false, the loop jumps to the next statement.

A while loop works on the following syntax–   

  1.  While(condition){
  2. //code
  3. //loop body} 

If the starting condition in the while loop is missing, the loop iterates infinitely. In while loop, no new value is created for the variable, only the condition is satisfied.

In while loop, the condition is checked before iteration therefore it is also known as the pre-test loop.

while loop

Main Differences Between For loop and While loop

  1. BIn for loop, the number of iterations to be conducted is already known whereas in while loop the number of iterations are not known.
  2. For loop contains only a single condition whereas while loop may contain a set of commands to be executed together.
  3. In for loop, initialization of command is done only once but in while loop initialization of command is needed each time the iteration of command is done.
  4. If the condition is absent in for loop, the loop iterates for an infinite number of times whereas the while loop shows an error in case of the absence of the condition.
  5. For loop can be used only in case of a known number of iterations whereas while loop is used only when the number of iterations is not known.
Difference Between For loop and While loop

References

  1. https://link.springer.com/chapter/10.1007/11589990_144
  2. https://www.date-conference.com/proceedings-archive/PAPERS/2011/DATE11/PDFFILES/06.6_1.PDF
Search for "Ask Any Difference" on Google. Rate this post!
[Total: 0]
One request?

I’ve put so much effort writing this blog post to provide value to you. It’ll be very helpful for me, if you consider sharing it on social media or with your friends/family. SHARING IS ♥️

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments