Javatpoint Logo
Javatpoint Logo

For vs. While loop in C

Understanding the Difference Between a for loop and a while loop

The iteration statements in C++, such as for loop, while loop, and do-while loop, allow a set of instructions to be executed repeatedly until the condition is true, and then terminate when the condition is false. Iteration statements can have predefined conditions, such as in a for loop, or open-ended conditions, such as in a while loop.

In C++, a variety of 'for' loop variations are implied to increase the language's applicability, power, and flexibility. For example, the for loop allows us to control the loop by using multiple variables inside it, as well as the use of the converge function with the 'for' loop. In contrast, we cannot use many variations with the while loop; it must be used with the standard syntax.

There are some significant differences between the for and while loops, which are further explained using a comparison chart.

For vs. While loop in C

For Loop is defined as

There are two types of for loops in Java. The first is the "traditional" form, while the second is the "for-each" form.

The most general form of a for loop statement.

  • Initialization: The for loop's loop controlling variable is only initialised once, during the first iteration of the loop. The loop controlling variable is initialised here; if the loop variable is never used again in the programme and is only used as the loop's controlling variable, it is both declared and initialised in the 'for' loop.
  • Condition: The 'for' loop's condition is executed each time the loop is iterated.
  • The iteration statement is an expression that increments or decrements the loop controlling variable.

When the loop is executed, the initialization condition is executed first, followed by the condition check. If the condition is met, the body of the loop is executed, followed by the iteration statement. The condition is then checked again to determine whether the loop will iterate further or terminate.

In Java, the initialization and iteration statements can both contain multiple statements. A comma separates each statement; in Java, a comma is a separator; in C++, a comma is an operator that can be used in any valid expression.

The syntax of the for-each loop

The "for-each" form is a more advanced version of the for loop. The for-each loop takes the following general form.

The "type" parameter specifies the type of iteration variable, which is followed by the iteration variable. The element from the collection variable will be passed to the iteration variable. The type must match the type of elements in the collection variable. The for-each form of the for loop automates the loop's iteration from beginning to end, accessing the values in sequential order.

Example

There are various types of collections that can be used with a for loop. Let's talk about it using an array as a collection.

Output:

value in c 10
value in c 20
value in c 30
value in c 40
value in c 50
value in c 60
additon of array elements is 210

'c' is an iteration variable in this case; it receives the values from array[], one by one, from the lowest to the highest index in the array. The loop iterates until all of the array's elements are examined. The loop can be broken in the middle by using "break." The change in the iteration variable, on the other hand, has no effect on the array because it is a read-only variable.

While loop is defined as

The while loop is the most basic loop in both C++ and Java. A while loop's operation is similar in C++ and Java.

Syntax

The following is the while loop declaration:

The while loop first checks the condition and then executes the statements until the condition in the while loop is true. In a while loop, the condition can be any boolean expression. When an expression returns a non-zero value, the condition is true; when it returns a zero value, the condition is false.

If the condition is true, the loop iterates itself; if the condition is false, control is passed to the line of code immediately following the loop. The body loop or statements can be an empty statement, a single statement, or a block of statements.

Example

Let's look at how a while loop works. The code in the example below will print from 1 to 10.

Output:

n=1
n=2
n=3
n=4
n=5
n=6
n=7
n=8
n=9
n=10

The initial value of 'n' in this case is 0, which makes the condition in the while loop true. The control then enters the while loop's body, where the value of 'n' is incremented in accordance with the first statement.

The value of 'n' is printed, then control returns to the condition in a while loop, where the value of 'n' is now 1, satisfying the condition once more, and the body of the loop is executed once more. This continues until the condition becomes false, at which point the loop is terminated.

The 'while' loop, like the 'for' loop, can initialise the control variable at the beginning of the loop, i.e. during condition checking.

At the top of the loop, the control variable 'ch' is initialised, and the loop's condition is verified.

Note: If there is only one statement in the body of the loop, whether it is a for loop or a while loop, the curly braces are not required.

In C, what is the difference between a for loop and a while?

Parameters For Loop While Loop
Declaration for(initialization ; condition ; iteration ) {
//body of 'for' loop
}
initialization
while ( condition ) {
statements;
//body of loop
}
Format. At the top of the loop, initialization, condition checking, and iteration statements are written. At the top of the loop, only initialization and condition checking are performed.
Use. The 'for' loop was only used when the number of iterations was already known. When the number of iterations is unknown, the 'while' loop is used.
Condition. If the condition is not included in the 'for' loop, the loop iterates indefinitely. If the condition is not included in the 'while' loop, a compilation error occurs.
Initialization The initialization is never repeated in a 'for' loop. If initialization is performed during condition checking in a while loop, initialization is performed each time the loop iterates.
Iteration assertion Because the iteration statement in the 'for' loop is written at the top, it executes only after all statements in the loop have been executed. The iteration statement in a 'while' loop can be written anywhere in the loop.

The Key Differences Between for and while loop

  • Initialization, condition checking, and increment or decrement of iteration variables are all done explicitly in the loop syntax only. In contrast, in the while loop, we can only initialise and check the condition in the loop syntax.
  • When we know the number of iterations that must occur in a loop execution, we use the for loop. On the other hand, if we do not know how many iterations must occur in a loop, we use a while loop.
  • If you do not include a condition statement in the for loop, the loop will loop indefinitely. In contrast, failing to include a condition statement in the while loop will result in a compilation error.
  • The initialization statement in the for loop syntax is only executed once at the beginning of the loop. If the while loop's syntax includes an initialization statement, the initialization statement will be executed each time the loop iterates.
  • The iteration statement in the for loop will run after the body of the for loop. On the contrary, because the iteration statement can be written anywhere in the body of the while loop, there may be some statements that execute after the iteration statement in the body of the while loop is executed.

Conclusion

Loops are thus a collection of commands that must be used in a specific order. If the loop structure is incorrect, the programming will display the syntax error. Loops run to obtain a result or to satisfy a condition or set of conditions. It is the foundation of all programming languages.

During execution, the loop structure asks a question and executes until the answer is satisfactory. The same question is asked again and again until the new statement is applied. The looping process continues indefinitely until the programme reaches a breakpoint. In the event that the breaking point is not reached, the programme will crash.

The for and while loops are both conditional statements. A for loop is a single-line command that will be executed repeatedly. While loops can be single-lined or contain multiple commands for a single condition.

Both the for loop and the while loop are important in computer languages for obtaining results. The condition is met if the command syntax is correct.

Both the for loop and the while loop are iteration statements, but they have distinct characteristics. The for loop declares everything (initialization, condition, iteration) at the top of the loop body. In contrast, only initialization and condition are at the top of the body of the loop in a while loop, and iteration can be written anywhere in the body of the loop.


Next Topic#





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA