Loop Control Statements
A Loop is used to consecutively execute a line of code or a block of code again and again until it reaches the desired value.
Types of Loop
1. for loop.
syntax :
for(initialization; condition; increment){
// do something
}
Initialization: initialize the value.
Condition: check the loop to determine whether its value has reached the number of repetitions desired.
increment: updating the value of the loop each time, the body of the loop executes.
Example :
let us write a program to print "well come" 10 time
#include<stdio.h>
#include<math.h>
int main()
{
for(int i = 1; i<=10; i = i+1){
printf("well come \n");
}
return 0;
}
Output :
well come will be printed 10 times
Short hand :
a. increment operator: ++i and i++
++1 (pre increment operator) means increase and then use.
use of ++i
#include<stdio.h>
#include<math.h>
int main()
{
int i = 1;
printf("%d\n", ++i);// increase
printf("%d\n", i);// and then use
return 0;
}
Output:
2
2
--------------------------------
Process exited after 0.007114 seconds with return value 0
Press any key to continue . . .
i++ (Post increment operator) means use and then increase.
use of i++
#include<stdio.h>
#include<math.h>
int main()
{
int i = 1;
printf("%d\n", i++);// use
printf("%d\n", i);// and then increase
return 0;
}
Output:
1
2
--------------------------------
Process exited after 0.007114 seconds with return value 0
Press any key to continue . . .
b. decrement operator: --1 and 1--
--1 (pre decrement operator)
use of --1
#include<stdio.h>
#include<math.h>
int main()
{
int i = 1;
printf("%d\n", --i);// decrease
printf("%d\n", i);// and then use
return 0;
}
Output :
0
0
--------------------------------
Process exited after 0.007806 seconds with return value 0
Press any key to continue . . .
1-- (post decrement operator)
use of 1--
#include<stdio.h>
#include<math.h>
int main()
{
int i = 1;
printf("%d\n", i--);// use
printf("%d\n", i);// and then decrease
return 0;
}
Output :
1
0
--------------------------------
Process exited after 0.007806 seconds with return value 0
Press any key to continue . . .
Note: Loop counter can be float or even character
Example :
use of float and character in for loop
#include<stdio.h>
#include<math.h>
int main()
{
for(float i=1; i<=5; i++){
printf("%f\n", i);
}
for(char ch='a'; ch<='z'; ch++){
printf("%c\n", ch);
}
return 0;
}
Output :
1.000000
2.000000
3.000000
4.000000
5.000000
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z
--------------------------------
Process exited after 0.01053 seconds with return value 0
Press any key to continue . . .
infinite loop:
loop that does not terminate.
syntax:
for(initialize; ; updation){
//doing something
}
2. while loop.
while loop repeatedly executes a code as long as a given condition is true.
Syntax :
while(condition){
//do something
}
use of while loop in c program
#include<stdio.h>
int main()
{
int i=1;
while(i <=5){
printf("hello world\n");
i++;
}
return 0;
}
Output :
hello world
hello world
hello world
hello world
hello world
--------------------------------
Process exited after 0.01131 seconds with return value 0
Press any key to continue . . .
3. do while loop.
syntax :
do{
//do something
}while(condition);
use of do while in c program
#include<stdio.h>
int main()
{
int i=1;
do{
printf("%d\n", i);
i++;
}while(i <= 5);
return 0;
}
Output:
1
2
3
4
5
--------------------------------
Process exited after 0.008616 seconds with return value 0
Press any key to continue . . .
Note : each type of loop serves same purpose, only the syntax is different.
4. break statement
used to exit from the loop.
break statement can be also used in nested loop.
use of break statement
#include<stdio.h>
int main()
{
for(int i=1; i<=10; i++){
if(i == 5){
break;
}
printf("%d\n", i);
}
return 0;
}
Output :
1
2
3
4
--------------------------------
Process exited after 0.007389 seconds with return value 0
Press any key to continue . . .
5. continue statement
it is the vice versa of break statement.(skip to next iteration)
use of continue statement
#include<stdio.h>
int main()
{
for(int i=1; i<=10; i++){
if(i == 5){ // 5 will be skiped
continue;
}
printf("%d\n", i);
}
return 0;
}
output :
1
2
3
4
6
7
8
9
10
--------------------------------
Process exited after 0.008552 seconds with return value 0
Press any key to continue . .
Comments
Post a Comment
Please do not enter any spam link in the comment box.