Unit 4 Loops in C++
Unit 4 Loops in C++
Loop
Loops are very commonly used in programming. It allows to repeatedly execute one or more
statements in a program as many times as required. Loops provide an easy and efficient way
of writing computer programs.
There are three types of loops used in C++ programming. These are for, while and do while
loops.
1. The for Loop
The for loop is used to execute one or more statements repeatedly for a fixed number of
times.
It has the general form:
for (initialization; condition; increment/decrement)
{
Block of statements
}
In the initialization part, a variable (also known as loop variable) is assigned an initial value,
such as, k=1 or count=0. The condition is a relational expression such as k<=10. When loop is
executed, the condition is evaluated, if the condition is true, the block of statements within
the braces are executed otherwise it will exit the loop. After the execution of block of
statements, the control is transferred back to the increment/decrement part of the loop.
The loop variable is incremented or decremented using an assignment statement such as
k=k+1. The new value of loop variable is again checked with the condition. If the condition is
satisfied then the block of statements is again executed. In this manner, the loop is
repeatedly executed till the condition becomes false. If there is only one statement in the
loop then braces are not required. The initialization part is executed only once at the start of
loop.
//Program to print squares of the numbers from 1 to 10 on a single line.
#include <iostream>
using namespace std;
int main()
{ int j;
for (j=1; j<=10; j++)
{
cout << j*j <<”\t”;
}
cout <<endl;
return 0;
{
while (condition)
{
Block of Statements
}
When while loop is executed, the condition is evaluated. The condition is a relational
expression. If the condition is true, the Block of statements is executed. After the execution
of the statements, the control is transferred back to the beginning of the loop and the
condition is evaluated again. This process continues till the condition becomes false. When
the loop condition becomes false, the control is transferred to the next statement after the
loop. Braces are not required if a single statement is to be executed.
//Program to print the cubes of the numbers till its value is less than 600.
#include <iostream>
using namespace std;
int main()
{
int n=1, cube=1;
while(cube<600)
{
cout <<n<<”\t”<<cube <<endl;
n++;
cube=n*n*n;
}
cout <<endl;
return 0;
}
return 0;
}
Enter a number: 2
Enter a number (negative number to quit):3
Enter a number (negative number to quit):4
Enter a number (negative number to quit):5
//Program that reads two numbers. The first number is a dividend and the
//second number is a divisor. It then calculates the quotient and the remainder
//and prints out the result.
#include <iostream>
using namespace std;
int main()
{
int dividend, divisor;
char ch;
do
{
cout <<”\nEnter the dividend: “; cin >>dividend;
cout <<”\nEnter the divisor: “; cin >>divisor;
cout <<”\nThe quotient is “ <<dividend / divisor <<endl;
cout <<”\nThe remainder is “ <<dividend % divisor <<endl;
cout <<”\nDo another? (y/n): “;
cin >>ch;
}
while (ch==’y’);
return 0;
}
Enter dividend: 11
Enter the divisor: 3
The quotient is 3
The remainder is 2
Do another? (y/n): y
Enter the dividend: 222
Enter the divisor: 17
The quotient is 13
The remainder is 1
Do another? (y/n): n
A B C D E F L M N O P Q R S T U V W X Y Z
Nested Loops
In C++, it is allowed to nest loops within another loop. A for, while, do while loop can be
nested inside another for, while or do while loop. Loops can be mixed in any way as
required by the programmer.
//Program to demonstrate the use of for loop nested inside another for loop.
//It prints 5 lines of asterisks (*), each having 10 asterisks.
#include <iostream>
using namespace std;
int main()
{
int j, k;
In this program, the outer loop is controlled by loop variable j and it will execute 5 time. The
inner loop is controlled by loop variable k and it executes 10 times. Each time the inner loop
is executed, it prints a line of 10 asterisks. Since the outer loop executes 5 times, the
program prints 5 lines of asterisks.
**********
**********
**********
**********
**********
The next program prints the following pattern using nested while loop inside a for loop.
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
#include <iostream>
using namespace std;
int main()
{