1
Programming with C++
Lecture 5: Looping statements
Sikander Naseem
2
Introduction
Iterative Programming
Three iteration (looping) statements
The while statement
The do --- while statement
The for statement.
3
The while statement
The syntax for while statement is
while (condition) statement;
Condition is any integral expression (comparison
operators)
Statement is any executable statement.
The statement will be executed if the integral
expression is having any non zero value (true)
If the value from the integral expression is zero
(false) the statement will not be executed
4
The while statement
The factorial finding Program
int main()
{int m;
cout << "Enter a positive integer to find its factorial = ";
cin>>m;
int i=1, fact=1;
while(i<=m) fact=fact*(i++);
cout << "The factorial of "<<m<< " is "<<fact;
}
5
The while statement
A Continuously running program (notice statement block)
int main()
{int m;
cout << "Enter a number to find its cube = ";
cin>>m;
while (m != 0 ) {cout<< "Cube of the number is = "<<m*m*m<<endl;
cout << "Enter another positive number to find
cube(or 0 to quit) = ";
cin>>m;}
}
6
The do --- while statement
Syntax
do statement while (condition);
Condition is any integral expression
Statement is any executable statement.
The statement will be executed until the integral expression
is having any non zero value (true)
Same as while except the condition is evaluated at the end.
So the loop is always executed once.
Variables can be defined inside the loop.
Statement blocks are still required for multiple statements
Semicolons
7
The do --- while statement
The factorial program
int main()
{int m;
cout << "Enter a positive integer to find its factorial = ";
cin>>m;
int i=1, fact=1;
do
fact=fact*(i++);
while(i<=m);
cout << "The factorial of "<<m<< " is "<<fact<<endl;
}
8
The do --- while statement
A Continuously running program (notice statement block)
int main()
{int m;
cout << "Enter a number to find its cube = ";
cin>>m;
do
{cout<< "Cube of the number is = "<<m*m*m<<endl;
cout << "Enter another positive number to find cube(or 0 to
quit) = ";
cin>>m;}
while (m != 0 );
}
9
The for statement
Syntax
for (initialization; condition; update) statement;
Where (initialization; condition; update) are the
optional expressions.
Statement is any executable statement.
10
The for statement
Initialization is used to declare the control
variable (i in the previous examples) for the loop.
The condition is the integral expression which
determines weather the loop should continue to
iterate depending (true or false)
The update expression updates the control variable
and is performed after the statement is executed.
11
The for statement
for (initialization; condition; update) statement;
Steps for for statement
1. Evaluate the initialization expression;
2. Check the condition. If false terminate the loop;
3. If true execute the statement;
4. Evaluate the update expression;
5. Repeat 2-4
12
The for statement
More than one control variable
for (i1, i2 ; condition; update1, update1) statement;
Conversion of previous examples to for statement
loop.
13
Miscalenous
The break statement
The continue statement
The goto statement
14
Practice
Count down program (while loop)
Delay Program (do---while)
Continuous calculator from previous lecture
15
The End
Thanks for coming
Reference book:
Programming with C++, SCHAUM’s outlines, John Hubbard,
3rd/2nd Edition, McGraw Hill, New Delhi