Loop Constructs: by Ansar Javed Department of Computer Science, Capital University of Science and Technology, Islamabad
Loop Constructs: by Ansar Javed Department of Computer Science, Capital University of Science and Technology, Islamabad
By
Ansar Javed
2
Loops in C++
1. for loop
2. while loop
3. do loop
3
Loops
Counter-controlled Loops
• Depends on the value of a variable known as counter
variable. The value of the variable is incremented or
decremented in each iteration.
Example: for loop
return 0;
}
5
(1) for loop - Syntax
6
(for loop) -- Class Exercise-1
- Get a number form user and calculate its factorial
7
(for loop) -- Class Exercise-2
- Write a program that ask the user to enter a
number. The program should print the Cube of all
integers starting from 1 to the Number.
E.g.,
Enter a Number: 4
1 1
2 6
3 27
4 64
8
(for loop) -- Class Exercise-3
- Write a program that asks the user to enter two
numbers: speed1, and speed2 representing speeds
in KPH (Kilo meters per Hour). Then the program
should convert and show table of speeds in MPH
(Miles per Hour) for all the speed values between
speed1 and speed2.
MPH = KPH * 0.6214
10
(1) for loop - Variable Visibility
void main()
{
int j;
for(j=0; j<10; j++) {
int k=0;
k = j*j;
cout<<“\nValue of k: “<<k;
}
// k = 23; Cannot do this!
} 11
(1) for loop – optional expressions
int j=0;
for(; j<10; j++)
cout<<“\nHello world“;
int j=0;
for(; j<10;)
{
cout<<“\nHello world“;
j++;
}
int count = 0;
while (count < 2)
{
cout << "Welcome to C++!";
count++;
}
Example: Tracing a while Loop
(count < 2) is true
int count = 0;
while (count < 2)
{
cout << "Welcome to C++!";
count++;
}
Example: Tracing a while Loop
int count = 0;
Print “Welcome to C++”
while (count < 2)
{
cout << "Welcome to C++!";
count++;
}
Example: Tracing a while Loop
int count = 0;
while (count < 2)
{ Increase count by 1
count is 1 now
cout << "Welcome to C++!";
count++;
}
Example: Tracing a while Loop
(count < 2) is still true since
int count = 0; count is 1
int count = 0;
while (count < 2) Print “Welcome to C++”
{
cout << "Welcome to C++!";
count++;
}
Example: Tracing a while Loop
int count = 0;
while (count < 2)
Increase count by 1
{ count is 2 now
int count = 0;
while (count < 2)
{
cout << "Welcome to C++!";
count++; The loop exits. Execute the next
statement after the loop.
}
Example Program
-Writea program to print character entered by user,
terminate the program when ‘z’ is pressed.
#include <iostream>
#include <conio.h>
using namespace std;
void main( )
{
char ch='0';
cout<<"Enter characters, z to terminate..\n";
while(ch!='z')
ch = _getche();
cout<<“Program ended…”
}
(while loop) -- Class Exercise-1
-Writea program to get characters from the user. In the
end of the program should count total characters
(entered by the user). The program should stop taking
input when 0 is pressed.
(while loop) -- Class Exercise-2
-Write a program to get input in the form of characters
from the user. In the end of the program should count
total words entered by the user. The program should
stop taking input when 1 is pressed.
(while loop) -- Class Exercise-3
- Writea program that inputs a value in an integer number
from user. For this number the program returns the count
for how many times can we divide this number by 2 to
get down to 1”.
void main( )
{
int counter, howmuch;
cin>>howmuch;
counter = 0;
do {
counter++;
cout<<counter<<'\n';
} while ( counter < howmuch);
}
do loop – Example2
void main( )
{
int num1, num2; char choice;
do {
cout<<“\nEnter a number:”;
cin>>num1;
cout<<“\nEnter another number:”;
cin>>num2;
cout<<“\nTheir sum is: “<<num1+num2;
cout<<“\nDo another time (y/n):”;
ch = _getche(); // #include <conio.h>
} while(ch==‘y’);
}
“break” Statement
• break statement
– Immediate exit from while, for, do/while, (also
used in switch)
– Program continues with first statement after the loop
structure
*
**
***
****
*****
******
*******
********
*********
(Nested Loops) – Example Program-2
- Write a program to print triangle of starts.
*********
********
*******
******
*****
****
***
**
*
(Nested Loops) – Example Program-3
- Write a program to print Rectangle based on two
triangles (One using + and other using * symbol).
+********
++*******
+++******
++++*****
+++++****
++++++***
+++++++**
++++++++*
+++++++++
(Nested Loops) – Example Program-4
- Write a program for a company to calculate total sales for
3 regions. Each region’s sales is entered by user which is
then summed in total regional sales. The program keep
accepting regional sales until it is not 0. The program
prints the final regional sum for three regions and exits.
Example Output:
Total Sales for Region 1: 87645
Total Sales for Region 2: 2312
Total Sales for Region 3: 8874