Loops in c programming
Loops in programming comes into use when we need to execute same codes in sevaral times.
Suppose you need to print your name in 100 times normally you need write printf()
function 100 times but it is very difficult.Instead of that you can use loops. Loops
are three types. These are --
- For Loops
- While Loops
- Do While Loops
For Loop: For loop is a repetition control of a structure which allows us to write a loop that is executed a fixed number of times as you want. For loop is easy to write and easy to understand.Basically there are three parts in for loop these are starting point , condition or end point and increment or how fast it will go to the end point.
Syntax of For Loops:
for(start; condition; increment){
statement ;
}
Let's implement a full code:
Description: Write a program which pirint "www.tutorialforbeginner.com" 10 times using for loop.
#include<stdio.h>
int main(){
int i;
for( i = 1; i <= 10 ; i++){
printf("www.tutorialforbeginner.com");
}
return 0;
}
Output:
www.tutorialforbeginner.com
www.tutorialforbeginner.com
www.tutorialforbeginner.com
www.tutorialforbeginner.com
www.tutorialforbeginner.com
www.tutorialforbeginner.com
www.tutorialforbeginner.com
www.tutorialforbeginner.com
www.tutorialforbeginner.com
www.tutorialforbeginner.com
While Loop: While loop also a repetition control of a structure which allows us to write a loop that is executed a fixed number of times as you want. While loop is also easy to write and easy to understand.Basically there are two parts in for loop these are condition or end point and increment or how fast it will go to the end point.
Syntax of While Loops:
initialization;
while(condition){
statement ;
increment;
}
Let's implement a full code:
Description: Write a program which pirint 1 to 10 using while loop.
#include<stdio.h>
int main(){
int i=1;
while( i<=10 ){
printf("%d ",i);
i++;
}
return 0;
}
Output:
1 2 3 4 5 6 7 8 9 10
Do While Loop: Do while loop also a repetition control of a structure which allows us to write a loop that is executed a fixed number of times as you want. Do while loop is also easy to write and easy to understand.
Syntax of Do While Loops:
initialization expression;
do{
statements;
increment;
} while (condition);
Let's implement a full code:
Description: Write a program which pirint 1 to 10 using Do While loop.
#include<stdio.h>
int main(){
int i=1;
do{
printf("%d ",i);
i++;
}while( i<=10 );
return 0;
}
Output:
1 2 3 4 5 6 7 8 9 10
break Statement: Break statement is only used for stop a excuting code for in a certain time.
When a Compiler got the break;
statement the it stops to execute thak blocks of code or loops.Genarally it is used
in switch case statement or inside the loop.
#include<stdio.h>
int main(){
int i;
for( i = 1; i <= 10 ; i++){
printf("%d ",i);
if(i==5){
break;
}
}
return 0;
}
Output:
1 2 3 4 5
[Note: loop stop after print 5 because of break
statement]
continue Statement: continue statement is only used for skip a statement.
When a Compiler got the continue;
statement it skip that statement.Genarally it used inside the loop.
#include<stdio.h>
int main(){
int i;
for( i = 1; i <= 10 ; i++){
if(i==5){
continue;
}
else{
printf("%d ",i);
}
}
return 0;
}
Output:
1 2 3 4 6 7 8 9 10
[Note: 5 is not present because of continue
statement]
Nasted loop:
Nasted loop: Nasted loop is nothing but a loop inside another loop. See the following example:
#include<stdio.h>
int main(){
int i,j;
for( i = 1; i <= 5 ; i++){
for( j = 1; j <= 5 ; j++){
printf("%d ",j);
}
printf("\n");
}
return 0;
}
Output:
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
Lets analize the above code . In the outer loop when i=1 then it goes to the inner loop to excute the whole loop once that means it is looping 5 times value is print 1 2 3 4 5 and getout of the inner loop. after that Compiler found "\n"(newline) so it goes to the next line. Then i become 2 and inner loop do the same. Eventualy, we got the result.