Introduction to Loop
Loop means to repeat again and again. In C++ there are three loops that While loop, do-While loop & For loop. In C++ loop continues while a condition is true. When condition becomes false, the loop ends and control passes to the statements following the loop.
There are three loop as i told before these are
For Loop:
This loop is easiest C++ loops to understand. This loop contain three parts first initialize , second condition & third increment.
Syntax:
for(initialize variable; condition ; variable increment)
like for(int i=0; i < 10; i++)
In this above statement 'i' variable is declared which have value 0 and 'i' gave condition that 'i' should less than 10 and then increment.
This works like that at begin its value is 0 and it execute the statement within the braces of for loop then increment is done and condition is check now i = 1 and i < 10 . It again run the code and this go on again and again.
Example :-
#include<iostream.h>
#include<conio.h>
int main()
{
for(int i=1; i <=10; i++) // don't put semicolon after for loop
{
cout<< i <<endl; // print value of i variable <<endl for new line
}
getch();
return 0;
}
Output :
While Loop
While loop is similar to for loop just little difference is there in while loop check the Syntax:
Syntax:
Initialize
While(condition)
statements 1;
statements 2;
increment;
}
like
int i=0;
while(i<10) //no semicolon
{
statement;
i++;
}
In Above we initialize 'i' value 0 first then while contain condition that i <10and if condition its true then code executes else it skip the loop and continue with program. and here it execute and run the statement 10 times.
Example:
#include<iostream.h>
#include<conio.h>
int main()
{
int i=1;
while(i<=10)
{
cout<< i <<endl; // print value of i variable <<endl for new line
i++;
}
getch();
return 0;
}
Output :
And Last
do-While Loop
Its different from other loop. In this loop Its first initialize then it execute, then increment & last condition is check. My mean to say that Code within do while loop execute at least one time if condition is true or false.
Syntax:
intialize
do
{
statement 1;
statement 2;
increment;
}while(condition); // semicolon at end
Check out example:
#include<iostream.h>
#include<conio.h>
int main()
{
int i=1; // initialize
do
{
cout << i<<endl; // print i value
i++; // increment
}while(i<=10); // check condition and semicolon at end
getch();
return 0;
}
Output :
Loop means to repeat again and again. In C++ there are three loops that While loop, do-While loop & For loop. In C++ loop continues while a condition is true. When condition becomes false, the loop ends and control passes to the statements following the loop.
There are three loop as i told before these are
For Loop:
This loop is easiest C++ loops to understand. This loop contain three parts first initialize , second condition & third increment.
Syntax:
for(initialize variable; condition ; variable increment)
like for(int i=0; i < 10; i++)
In this above statement 'i' variable is declared which have value 0 and 'i' gave condition that 'i' should less than 10 and then increment.
This works like that at begin its value is 0 and it execute the statement within the braces of for loop then increment is done and condition is check now i = 1 and i < 10 . It again run the code and this go on again and again.
Example :-
#include<iostream.h>
#include<conio.h>
int main()
{
for(int i=1; i <=10; i++) // don't put semicolon after for loop
{
cout<< i <<endl; // print value of i variable <<endl for new line
}
getch();
return 0;
}
Output :
While Loop
While loop is similar to for loop just little difference is there in while loop check the Syntax:
Syntax:
Initialize
While(condition)
statements 1;
statements 2;
increment;
}
like
int i=0;
while(i<10) //no semicolon
{
statement;
i++;
}
In Above we initialize 'i' value 0 first then while contain condition that i <10and if condition its true then code executes else it skip the loop and continue with program. and here it execute and run the statement 10 times.
Example:
#include<iostream.h>
#include<conio.h>
int main()
{
int i=1;
while(i<=10)
{
cout<< i <<endl; // print value of i variable <<endl for new line
i++;
}
getch();
return 0;
}
Output :
And Last
do-While Loop
Its different from other loop. In this loop Its first initialize then it execute, then increment & last condition is check. My mean to say that Code within do while loop execute at least one time if condition is true or false.
Syntax:
intialize
do
{
statement 1;
statement 2;
increment;
}while(condition); // semicolon at end
Check out example:
#include<iostream.h>
#include<conio.h>
int main()
{
int i=1; // initialize
do
{
cout << i<<endl; // print i value
i++; // increment
}while(i<=10); // check condition and semicolon at end
getch();
return 0;
}
Output :
0 comments:
Post a Comment