Unit 2.6 For, Do, While Loop
Unit 2.6 For, Do, While Loop
PROGRAMMING)
UNIT-2
By
ADITYA BHARDWAJ
1
LOOPS
• There are three kinds of loops in C++: the for loop, the while
loop, and the do while loop.
1. For Loop Introduction
Loops in C++
Loops are the iterative Body
statements which are used Statement; of loop
to repeatedly execute a set
of instructions till a
condition is met. True
Loop
Condition? Condition
False
For Loop in C++
For loop in C++ looks like the following :
for (initialization; loop condition; increment/decrement)
Statement/Block_statement;
The statements inside the for loop are called the body of the loop and
will be executed till the loop condition is true.
False
i < 2?
for (i = 0; i < 2; i++)
cout << i << endl; True
i++;
Tracing the loop
So to find out what the output of our small loop will be, we will trace the
execution of the loop :
Output :
Hence after the loop exits, the value of variable i is 0
2 and we have the output as shown. 1
For Loop in C++
for (initialization_expression; loop_condition; update_expression)
Statement/Block_statement;
For example,
Input : 5 Output : 15
Input : 3 Output : 6
Solution Scheme
Say the value of N is 5, so we have to compute the sum
(1 + 2 + 3 + 4 + 5)
One method is to have two variables sum and count.
The variable count iterates from 1 to N and we keep
adding count to sum.
So we can write a for loop for doing that…
Solution Scheme
int count, sum, N;
cin >> N;
Note the for (count = 1; count <= N; count++)
block {
statement sum = sum + count;
}
#include <iostream>
using namespace std;
Solution
Note : We generally declare
all our variables at the top
int main()
{ int count, sum, N;
cout << "Enter N : ";
cin >> N;
The following is the main sum = 0;
logic of the program :
for (count = 1; count <= N; count++)
{
sum = sum + count;
}
cout << "Sum is : " << sum;
return 0;
A new shorthand operator : +=
Very often in programming, we need to write expressions like :
number = number + 5;
output: 123
Example -3 Practice Yourself
For example,
Input : 4 Output : 24
Input : 5 Output : 120
#include <iostream.h>
using namespace std;
Solution int main()
{
int count, fact, N;
cout << "Enter N : ";
The following is the main cin >> N;
logic of the program : fact = 1;
for (count = 1; count <= N; count++)
{
fact = fact * count;
}
cout << "Factorial is : " << fact;
return 0; }
2. While Loop
While loop
The syntax for while loop :
Variable Initialization
while (condition expression)
increment/decrement /
statements; Body of loop
int i = 0;
while (i < 10)
i++;
Example
For example,
Input : 5 Output : 1 2 3 4 5
Input : 3 Output : 1 2 3
Solution
int i, N;
The following is the main cin >> N;
logic of the program :
i = 1;
while (i <= N)
Note that for while loops,
we initialize the loop {
variable outside the loop cout << i << " ";
and update it inside
i++;
}
Find Output int i, N;
N = 5;
i = 1;
while (i <= N)
{
cout << i << " ";
i += 2;
}
cout << i << " ";
Find Output int i, N;
N = 5;
i = 1;
357
1 i while (i <= N)
{
cout << i << " ";
i += 2;
Output : 1 3 5 7
}
cout << i << " ";
Example
Write a program which takes a natural number (say, N) as input such
that N > 1, and checks if it is prime.
For example,
Input : 5 Output : Prime
Input : 9 Output : Not Prime
Solution Scheme
We keep a counter i which iterates from 2 to (N-1)
If yes, we have found a perfect divisor for N between [2, N-1], hence
the number is not prime
The loop condition is evaluated after executing the body of the loop,
hence the loop will run at-least once
int main()
Find Output {
int count = 10;
do
{
cout << count << " ";
count -= 2;
} while ( count > 0 );
return 0;
}
int main()
Find Output {
int count = 10;
do
{
Output : cout << count << " ";
10 8 6 4 2
count -= 2;
} while ( count > 0 );
return 0;
}
int main()
Example {
int number;
do
{
Think what the following
program will do? cout << "Enter a positive integer : ";
cin >> number;
} while ( number < 0 );
cout << "Number = " << number;
return 0;
}
int main()
Example {
int number;
This program will keep on do
reading the input from user
till it reads a positive integer {
cout << "Enter a positive integer : ";
This is an interesting
application of loops cin >> number;
} while ( number < 0 );
The concept used is that
body of do-while loop will be cout << "Number = " << number;
executed at least once before return 0;
testing the condition
}
{
int N = 4;
Exercise int i, j;
for (i = 0; i < N; i++)
{
for (j = 0; j < N; j++)
What will be the output
of this program? {
cout << j;
}
cout << endl;
}
}
/*The following program fragment is an input routine that insists that the user type a correct
response -- in this case, a small case ‘y’ or capital case 'Y'. The do-while loop guarantees that the
body of the loop (the question) will always execute at least one time.*/
#include <iostream>
using namespace std;
int main()
{
char ans;
do
{
cout<<"Do you want to continue (Y/N)?\n";
cout<< "You must type a 'Y' or an 'N'.\n";
cin >> ans;
} while((ans =='Y')|| (ans =='y'));
}
/*the loop continues until the user enters the correct response*/