Looping Control Statements in C++
Looping
A set of statement execute again and again until a particular condition is satisfied. It helps in save time, reduce errors and they make our coder shorter.
Loops are of 3 Types
- for loop
- while loop
- do-while loop
for loop This loop is used when we already know how many times the code or loop will be executed.
Syntax:-
for(initilization;condition;iteration){
//set of code;
}
OR
for(int i=0;i<=5;i++){
cout<<i<<endl;
}
Output:
0
1
2
3
4
5
while loop This loop is also known ad Pre-Test loop. Because it will execute our program if the condition is true or satisfied. It will first chech the condition and then execute our program.
Syntax:-
while(condition){
//Set of code;
iteration;
}
Example:-
int i=0;
while(int i<=3){
cout<<i<<endl;
i++;
}
Output:
0
1
2
3
do while loop This loop will be execute atleast once wheather the condition is true or false.
This loop is also known as Post-Test loop.
Syntax:-
do{
//set of code;
}
while(condition);
Example:-
int i=0;
do{
cout<<i<<endl;
i++;
}
while(i<=2);
Output:
0
1
2
Comments
Post a Comment