Lec 9-10 (Loop)Finish
Lec 9-10 (Loop)Finish
ITERATIVE STATEMENTS
• Nested loop
2
LOOP STATEMENTS
• A loop is a program construction that repeats a
statement or sequence of statements a number
of times
• Loop design questions:
• What should the loop body be?
• How many times should the body be iterated?
LOOP STATEMENTS
4
THE WHILE REPETITION STRUCTURE
• 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)
While
#include <iostream>
using namespace std;
int main () {
// Local variable declaration:
int a = 10;
return 0;
}
7
(COUNTER-CONTROLLED REPETITION)
• Counter-controlled repetition
• Loop repeated until counter reaches a certain value.
• Definite repetition
• Number of repetitions is known
• Example
A class of ten students took a quiz. The grades (integers in the range 0 to
100) for this quiz are available to you. Determine the class average on
the quiz.
(COUNTER-CONTROLLED REPETITION)
• Pseudocode for example:
Set total and grade counter to zero
While grade counter <= 10
input the next grade
add the grade into the total
grade counter++
Average = total divided / 10
Print the class average
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;
• Prints the integers from one to ten
FLOWCHART FOR FOR
Initialize variable
false
EXTRA SEMICOLON
• Placing a semicolon after the parentheses of a for loop creates an empty
statement as the body of the loop
• Example: for(int count = 1; count <= 10; count++);
cout << "hello\n";
For
#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;
}
15
THE FOR AND WHILE REPETITION
SUM IS 2550
THE DO/WHILE REPETITION STRUCTURE
statement
true
condition
false
Example on (do-while)
Do-while
#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;
20
WHEN TO CHOOSE A WHILE, FOR, OR
DO-WHILE LOOP
23
THE CONTINUE STATEMENT
• Continue
• In computer programming, the continue
statement is used to skip the current
iteration of the loop and the control of the
program goes to the next iteration.
• 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 the for structure, the increment
expression is executed, then the loop-
continuation test is evaluated
• In while and do/while, the loop-
continuation test is evaluated immediately
after the continue statement is executed
25
.
NESTED LOOPS
NESTED LOOPS
• When working with nested loops, the outer loop changes only
after the inner loop is completely finished
THE SYNTAX FOR A NESTED FOR
LOOP STATEMENT IN C++
For ( init; condition; increment )
{
for ( init; condition; increment )
{
statement(s);
}
Statement(s); // you can put more statements.
}
THE SYNTAX FOR A NESTED WHILE
LOOP STATEMENT IN C++
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 {
32
EXAMPLE 3 :NESTED LOOPS (LOOP IN LOOP)
int a,b;
if (j > i) break;
cin >> a >> b;
33
EXAMPLE 2:NESTED LOOPS (LOOP IN LOOP)
int a,b;