Loops are very important concept in C language and any other programming language. Loops are important to convert the larger program to smaller ones. Loops are 'the sequence of statement which continues to execute until the desired condition is met.'
There are 3 types of loops in C language :
1. for loop
The syntax of for loop is " for(initialization;expression;increment/decrement)". For loop is also called as entry controlled loop, where we initialize statement, give condition, and update statement in a single line. The for loop statement will be executed until the condition evaluation is true. Whenever the condition fails the control skip the for loop.
Flowchart of for loop.
Simple program to print number from 1 to 10 using 'for loop'.
Output:
2. while loop
Syntex of while loop is :
while loop is also called as entry controlled loop. The statement under while loop is executed till the condition is true. When the condition fails the control leaves the while loop.
Flowchart of while loop.
Simple program of while loop to print 0 to 10.
3. do while loop
Syntex of do while loop is :
do while is sed to repeat the statement for some number of times. In do loop condition is checked at the end of the loop. Even if the condition is false the statement is executed at least once, then the control skips the loop.
Flowchart of do while loop.
Simple program of do while loop to print 0 to 10.
There are 3 types of loops in C language :
1. for loop. 2. while loop. 3. do while loop.
1. for loop
The syntax of for loop is " for(initialization;expression;increment/decrement)". For loop is also called as entry controlled loop, where we initialize statement, give condition, and update statement in a single line. The for loop statement will be executed until the condition evaluation is true. Whenever the condition fails the control skip the for loop.
Flowchart of for loop.
Simple program to print number from 1 to 10 using 'for loop'.
#include <stdio.h> void main() { int i; for(i=0;i<=10;i++) { printf("%d\n",i); } }At first we initialize a variable i=0. Until i is less than or equal to 10 for loop is executed,and it will print the numbers from 1 to 10. When i is greater than 10 the condition fails and control skips the for loop.
Output:
1 2 3 4 5 6 7 8 9 10
2. while loop
Syntex of while loop is :
while(condition) {________ _________.....statement update statement; }
while loop is also called as entry controlled loop. The statement under while loop is executed till the condition is true. When the condition fails the control leaves the while loop.
Flowchart of while loop.
Simple program of while loop to print 0 to 10.
#include <stdio.h> void main() { int i; while(i<=10) { printf("%d\n",i); i++; } }At first we initialize a variable i. Until i is less than or equal to 10 while loop is executed,and it will print the numbers from 0 to 10. When i is greater than 10 the condition fails and control skips the loop. Output :
0 1 2 3 4 5 6 7 8 9 10
3. do while loop
Syntex of do while loop is :
do { statement } while(condition);
do while is sed to repeat the statement for some number of times. In do loop condition is checked at the end of the loop. Even if the condition is false the statement is executed at least once, then the control skips the loop.
Flowchart of do while loop.
Simple program of do while loop to print 0 to 10.
#include <stdio.h> void main() { int i=0; do { printf("%d\n",i); i++; } while (i<=10); }At first we initialize a variable i=0. The we write do loop to print the value of i after printing it will increment the value by 1 until we give while loop i is less than or equal to 10. Output :
0 1 2 3 4 5 6 7 8 9 10
Comments
Post a Comment