Lec 5 Iteration
Lec 5 Iteration
Iteration
Repetition structure
Programmer specifies an action to be repeated while some
condition remains true
while loop repeated until condition becomes false.
Example
int product = 2;
while ( product <= 1000 )
product = 2 * product;
Common errors:
infinite loop
unitialized variables
The while Repetition Structure
true
condition statement
false
int x = 2;
while (x >= 0){
if ( x == 2){
cout << “Value of x is : “ << x << endl;
}
x = x – 1;
}
Example on (While)
7
#include <iostream>
using namespace std;
int main () {
// Local variable declaration:
int a = 10;
return 0;
}
1 // Fig. 2.7: fig02_07.cpp
2 // Class average program with counter-controlled repetition
3 #include <iostream>
4
5 using namespace std;
6
7
8
9 int main()
10 {
11 int total, // sum of grades
12 gradeCounter, // number of grades entered
13 grade, // one grade The counter gets incremented each
14 average; // average of grades time the loop executes. Eventually,
15
the counter causes the loop to end.
16 // initialization phase
17 total = 0; // clear total
18 gradeCounter = 1; // prepare to loop
19
20 // processing phase
21 while ( gradeCounter <= 10 ) { // loop 10 times
22 cout << "Enter grade: "; // prompt for input
23 cin >> grade; // input grade Enter grade: 98
Enter grade: 76
24 total = total + grade; // add grade to total
Enter grade: 71
25 gradeCounter = gradeCounter + 1; // increment counter
Enter grade: 87
26 } Enter grade: 83
27 Enter grade: 90
28 // termination phase Enter grade: 57
29 average = total / 10; Enter
// integer division grade: 79
30 cout << "Class average is " << average << endl; Enter grade: 82
31 Enter grade: 94
Class average is 81
32 return 0; // indicate program ended successfully
33 }
Ask Before Iterating
sum = 0;
cout << "Are there numbers in the list (Y/N)?";
char ans;
cin >> ans;
while (( ans == 'Y') || (ans == 'y'))
{
//statements to read and process the number
cout << "Are there more numbers(Y/N)? ";
cin >> ans;
}
The for Repetition Structure
Example:
for( int counter = 1; counter <= 10; counter++ )
cout << counter << endl;
Initialize variable
false
Extra Semicolon
#include <iostream>
using namespace std;
int main () {
// for loop execution
for( int a = 10; a < 20; a = a + 1 ) {
cout << "value of a: " << a << endl;
}
return 0;
}
The for and while Repetition
Sum is 2550
The do/while Repetition Structure
The do/while repetition structure is similar to the while
structure,
Condition for repetition tested after the body of the loop is
executed
Format:
do {
statement
} while ( condition );
Example:
int counter=0;
do {
cout << counter << " ";
counter++;
} while (counter <= 10);
This prints the integers from 1 to 10
All actions are performed at least once.
Flow chart of do/while
statement
true
condition
false
Example on (do-while)
18
#include <iostream>
using namespace std;
int main () {
// Local variable declaration:
int a = 10;
// do loop execution
do {
cout << "value of a: " << a << endl;
a = a + 1;
} while( a < 20 );
return 0;
When to choose a while, for, or
do-while loop
Break
Causes immediate exit from a while, for, do/while or switch
structure
Program execution continues with the first statement after the
structure
Common uses of the break statement:
Escape early from a loop
Skip the remainder of a switch structure
21 The break Statement
The continue Statement
Continue
Skips the remaining statements in the body of a while, for or
do/while structure and proceeds with the next iteration of the
loop
In while and do/while, the loop-continuation test is
evaluated immediately after the continue statement is
executed
In the for structure, the increment expression is executed,
then the loop-continuation test is evaluated
The continue Statement
int next = 0;
while (true){
cin >> next;
if (next < 0)
break;
if (next % 2==0)
continue;
cout << next << endl;
}
cout << “negative num so here we are!” << endl;
.
Nested loops
Nested Loops
while(condition)
{
while(condition)
{
statement(s);
}
statement(s); // you can put more statements.
}
The syntax for a nested do...while
loop statement in C++
do {
int a,b;
int a,b;
if (j > i) break;
cin >> a >> b;
int a,b;
Try to write a program in C++ that prints a tables Starting from 1 12.
1 2 3 4 5 6 7 8 9 10 11 12
2 4 6 8 10 12 14 16 18 20 22 24
3 6 9 12 15 18 21 24 27 30 33 36
4 8 12 16 20 24 28 32 36 40 44 48
5 10 15 20 25 30 35 40 45 50 55 60
6 12 18 24 30 36 42 48 54 60 66 72
7 14 21 28 35 42 49 56 63 70 77 84
8 16 24 32 40 48 56 64 72 80 88 96
9 18 27 36 45 54 63 72 81 90 99 108
10 20 30 40 50 60 70 80 90 100 110 120
11 22 33 44 55 66 77 88 99 110 121 132
12 24 36 48 60 72 84 96 108 120 132 144
.
Try to develop a code in C++ that generate the following series . Use
nested loop!
Series
1 1 1
12 22 23
123 333 456
1234 4444 7 8 9 10
36
Outline of course
Contents going to be covered during course:
Problem Solving
Flow chart, pseudo code, and Algorithm
Starting write first C++ program
Input and Output
Conditions
Loops
Array
Function
Pointers
String
Classes and Objects
37