Compare the Difference Between Similar Terms

Difference Between for and while Loop

Key Difference – for vs while Loop
 

A program is a set of instructions written in a programming language to perform a certain task. It can be a logical operation or a mathematical operation. Generally, the statements in the program execute one after the other. Sometimes it is necessary to execute a set of statements again and again. The control structures are used to achieve this task. Two of them are for and while loop. These structures help to execute a sequence of code until the given condition is true. The syntax of the for loop consist of initialization, test expression and update expression. The syntax of the while loop contains the test expression. This article discusses the difference between for and while loop. The key difference between for and while loop is that the for loop can be used when the number of iterations is known and the while loop can be used when the number of iterations is not known.

CONTENTS

1. Overview and Key Difference
2. What is for Loop
3. What is while Loop
4. Similarities Between for and while Loop
5. Side by Side Comparison – for vs while Loop in Tabular Form
6. Summary

What is for Loop?

The for loop is used in many programming languages such as C, Java etc. It is used to execute a set of statements multiple times. The syntax of the for loop is as follows.

for (initialization; test expression, update){

//statements inside the for loop

}

The initialization expression executes only once. Then, the test expression is evaluated. The test expression can contain variables, values, constant and operators. It is a Boolean expression. If the evaluated expression is true, the code inside the for loop executes. After reaching the end of the loop, the update expression is executed. It can be an increment or a decrement. Again the test expression is checked. If the evaluated expression is true, the statements inside the for loop execute. At the end of the loop, the update expression is executed. This process repeats until the test expression is false. When the test expression is false, the for loop terminates and the control is passed to the next statement after the for the loop.

Figure 01: A program with for loop to calculate sum of  5 numbers

The above program is to find the summation of first five numbers, which are 1,2,3,4 and 5. In the for loop, i is 1. It is less than 5. So the sum is calculated. Initially, sum is 0. It is added to i which is 1. The total is assigned to variable sum. Now the sum is 1.  Then the update expression is evaluated. The i is incremented by one. Now i is 2. It is less than 5. So, the sum is calculated. The previous sum value is 1 and it is added to i value which is 2. Now the sum is 3. The update expression is evaluated and i is incremented by 1. Now it is 3. This process repeats. When i becomes 6, the expression becomes false because 6 is not equal or less than 5. Therefore, the for loop terminates. Finally, the sum of all five numbers is printed to the screen.

What is while Loop?

The while loop executes the target statements as long as the given condition is true. The syntax of the while loop is as follows.

while(test expression){

//statements inside the while loop

}

The while loop contains a test expression. It is a Boolean expression. If the expression evaluated is true, then the statements inside the while loop execute. At the end of those statements, the test expression is evaluated again. This process repeats until the test expression becomes false. When it becomes false, the while loop terminates and the control is passed to the statement after the while loop.

Figure 02: A program with while loop to calculate sum of  5 numbers

According to the above program, the sum is initialized to 0 and i is initialized to 1. In the while loop, the i value is evaluated. It is less than 5. So, the sum is calculated. The initial value of sum is 0. It is added to i value 1. Now the sum is 1. Then the i value is incremented by one. Now i value is 2. It is less than 5. So the sum is calculated. The current sum which is 1 is added to the i value which is 2. Now the sum is 3. Again the i value is incremented. Now the i value is 3. This process repeats. When i value becomes 6, the expression becomes false because it is not less than or equal to 5. So, the while loop terminates. Finally, the sum value is printed to the screen. If there was no increment such as i++, then the i value remains in the same which is 1. It is less than 5.  The condition is true always. So it will be an infinite loop.

What are the Similarities Between for and while Loop?

What is the Difference Between for and while Loop?

for vs while Loop

The for loop is a repetition control structure that allows the programmer to efficiently write a loop that needs to execute a specific number of times. The while loop is a repetition control structure that executes target statements as long as the given condition is true.
 Usage
The for loop can be used when the number of iterations is known. The while loop can be used when the number of iterations is not known.
Initialization
The initialization occurs once in for loop. In while loop, if the initialization statement is inside the loop, then the initialization is done each time the loop iterates.

Summary – for vs while Loop

In programming, sometimes it is required to repeat a set of statements multiple times. There are repetition control structures to achieve this tasks. Two of them are for and while loop. The difference between for and while loop is that the for loop is used when the number of iterations is known and the while loop is used when the number of iterations is not known.

Reference:

1.Point, Tutorials. “For loop in C.”, Tutorials Point, 8 Jan. 2018, www.tutorialspoint.com/cprogramming/c_for_loop.htm.
2.Point, Tutorials. “While loop in C.”, Tutorials Point, 8 Jan. 2018, www.tutorialspoint.com/cprogramming/c_while_loop.htm.