2. There are mainly two types of loops:
loops
Entry
control loop
For loop
While
loop
Exit control
loop
Do while
3. Entry control loop
An entry control loop checks the condition at the time of entry
and if condition or expression becomes true then control
transfers into the body of the loop.
Entry
control loop
For loop while loop
4. for Loop :-
The syntax of for loop is :
for (initializationStatement; testExpression; updateStatement)
{
// body of loop
}
The initialization statement is executed only once.
Then, the test expression is evaluated. If the test expression is
evaluated to false, the for loop is terminated.
If the test expression is evaluated to true, statement inside the
body of for loop are executed and the update expression is
updated
This process goes on until the test expression become false.
How for loop works?
Flowchart of for loop
5. 1 2 3 4 5 6 7 8 9 10
Example 1
output :-
Program :-
Example 2
// Print numbers from 10 to 1
#include <stdio.h>
void main()
{
int i;
for (i = 10; i >=1; i--)
{
printf("%d ", i);
}
} // end of main
10 9 8 7 6 5 4 3 2 1
output :-
// Print numbers from 1 to 10
#include <stdio.h>
void main()
{
int i;
for (i = 1; i <= 10; i++)
{
printf("%d ", i);
}
} // end of main
6. While loop
The syntax of while loop is :
How while loop works?
while (testExpression)
{
// body of the loop
}
if the expression Is true, statements inside the body of while
loop are executed. Then, the test expression is evaluated
again.
The process of goes on until the expression is evaluated to
false.
If the expression is false, the loop terminates(Ends).
Flowchart of while loop
7. // program to print table
#include <stdio.h>
void main()
{
int i = 1, number;
printf("Enter a number : ");
scanf("%d", &number);
while (i <= 10)
{
printf("n%d * %d = %d",i,number,i*number);
i++;
}
}
1 * 8 = 8
2 * 8 = 16
3 * 8 = 24
4 * 8 = 32
5 * 8 = 40
6 * 8 = 48
7 * 8 = 56
8 * 8 = 64
9 * 8 = 72
10 * 8 = 80
Program :- output :-
Enter a number : 8
Example 1
8. Exit control loop
An Exit Control Loop checks the condition for exit and if given
condition for exit evaluate to true, control will exit from the loop
body else control will enter again into the loop.
Exit control
loop
Do while
loop
9. Do while loop
The syntax of while loop is :
How while loop works?
do
{
body of the loop
}
while (testExpression);
The body of do...while loop is executed once. Only then, the
test expression is evaluated.
If the test expression is true, the body of the loop is executed
again and the test expression is evaluated.
This process goes on until the test expression becomes false
If the test expression is false, the loop ends. Flowchart of do while
10. Example 1
// program to print factorial of number
#include <stdio.h>
void main()
{
int i = 1, num = 0, fact = 1;
printf("Enter a number: ");
scanf("%d", &num);
do
{
fact = fact * i;
i++;
} while (i <= num);
printf("%d", fact);
}
Program :- output :-
Enter a number: 5
120
11. Nesting of loops is the feature in C that allows the looping of
statements inside another loop. Let's observe an example of nesting
loops in C.
Nested Loops
Outer_loop
{
Inner_loop
{
// inner loop statements.
}
// outer loop statements.
}
Syntax of Nested loop
NOTE :- Outer loop and Inner loop are the valid loops that can be a 'for' loop,
'while' loop or 'do-while' loop