The looping can be defined as repeating the same process multiple times until a specific condition satisfies. C programming language provides the following types of loops to handle looping requirements:-
while loop
The while loop in c is to be used in the scenario where we don't know the number of iterations in advance. It tests the condition before executing the loop body.
Syntax:-
while(condition){
//code to be executed
}
Exercises:-
- Write a C program to print all natural numbers from 1 to n.
- Write a C program to print all even numbers between 1 to 100.
do-while loop
It works the same as while loop except it executes once even the given condition fails.
Syntax:-
do{
//code to be executed
}while(condition);
Exercises:-
The for loop is also called as a per-tested loop. It is better to use for loop if the number of iteration is known in advance.
Syntax:-
for(initialization;condition;incr/decr){
Comments
Post a Comment