Flowchart of While Loop in C
Flowchart of While Loop in C
Flowchart of While Loop in C
In general, a
while loop allows a part of the code to be executed multiple
times depending upon a given boolean condition. It can be
viewed as a repeating if statement. The while loop is mostly
used in the case where the number of iterations is not known in
advance.
Syntax
while(condition){
//code to be executed
}
do while loop
The do while loop is a post tested loop. Using the do-while loop, we can repeat the
execution of several parts of the statements. The do-while loop is mainly used in the
case where we need to execute the loop at least once. The do-while loop is mostly
used in menu-driven programs where the termination condition depends upon the
end user.
Syntax
do
{
Statement;
}
while(condition);
Here firstly statement inside body is executed then condition is checked. If the
condition is true again body of loop is executed and this process continue until
the condition becomes false. Unlike while loop semicolon is placed at the end of
while. There is minor difference between while and do while loop, while loop test
the condition before executing any of the statement of loop. Whereas do while
loop test condition after having executed the statement at least one within the
loop. If initial condition is false while loop would not executed it’s statement on
other hand do while loop executed it’s statement at least once even If condition
fails for first time. It means do while loop always executes at least once.
For Loop
The for loop in C language is used to iterate the statements or a part of the
program several times. It is frequently used to traverse the data structures like the
array and linked list.
Syntax
for(Expression 1; Expression 2; Expression 3){
//code to be executed
}
Nested Loops in C
Any number of loops can be defined inside another loop, i.e., there is no
restriction for defining any number of loops. The nesting level can be defined at
n times. You can define any type of loop inside another loop; for example, you
can define 'while' loop inside a 'for' loop.
Outer_loop
{
Inner_loop
{
// inner loop statements.
}
// outer loop statements.
}
The nested for loop means any type of loop which is defined inside the 'for' loop.