➢ Looping Statement :-
In computer programming, loop cause a certain piece of program to be executed a certain
number of times. Consider these scenarios:
• You want to execute some code/s certain number of time.
• You want to execute some code/s certain number of times depending upon input from
user.
These types of task can be solved in programming using loops.
There are 3 types of loops in C++ Programming:
• for Loop
• while Loop
• do...while Loop
1) while Loop :-
Syntax of while Loop :-
while (test expression)
{
statement/s to be executed.
}
The while loop checks whether the test expression is true or not. If it is true, code/s
inside the body of while loop is executed, that is, code/s inside the braces { } are executed.
Then again the test expression is checked whether test expression is true or not. This process
continues until the test expression becomes false.
Flowchart of while Loop in C++ :-
.png)
Example Program:-
//C++ program to find factorial of a positive integer entered by user.
#include <iostream.h>
#include <conio.h>
int main()
{
int n, i = 1, fact = 1;
cout<< "Enter a positive integer: ";
cin >> n;
while ( i <= n)
{
fact=fact*i;
i++;
}
cout<<"Factorial of "<<n<<" = "<<fact;
getch();
}
Output:-
Enter a positive integer:5
Factorial of 5 : 120
2) do...while Loop :-
The do...while Loop is similar to while loop with one very important difference. In
while loop, check expression is checked at first before body of loop but in case of do...while loop,
body of loop is executed first then only test expression is checked. That's why the body of
do...while loop is executed at least once.
Syntax of do...while Loop :-
do
{
statement/s;
}
while (test expression);
The statement/s inside body of loop is executed at least once, that is, the statement/s
inside braces { } is executed at least once. Then the test expression is checked. If the test
expression is true, the body of loop is executed. This process continues until the test expression
becomes false. Since the body of loop is placed before the test expression in do...while loop, the
body of loop is executed at least once.
.png)
Example Program :-
//C++ program to print the numbers from 1 to 5
#include <iostream.h>
#include <conio.h>
int main()
{
int a=1;
do
{
cout<<a<<"\t";
a++;
}
while(a<6);
getch();
}
Output:-
1 2 3 4 5
3) for Loop:-
Syntax:-
for(initialization statement; test expression; update statement)
{
code/s to be executed;
}
The initialization statement is executed only once at the beginning of the for loop. Then the
test expression is checked by the program. If the test expression is false, for loop is terminated.
But if test expression is true then the code/s inside body of for loop is executed and then update
expression is updated. This process repeats until test expression is false.
Flowchart of for Loop in C++ :-
Example Program:-
//C++ Program to find factorial of a number
#include <iostream.h>
#include <conio.h>
int main()
{
int i, n, fact = 1;
cout<<"Enter a positive integer: ";
cin>>n;
for (i = 1; i <= n; ++i)
{
fact=fact*i;
}
cout<< "Factorial of "<<n<<" = "<<fact;
getch();
}
Output :-
Enter a positive integer: 5
Factorial of 5 = 120
Comments
Post a Comment