Lec-09 - W9 (Loops, For Loop)
Lec-09 - W9 (Loops, For Loop)
C++
Lecture W
Introduction to Repetition
FOR .. Statement
2
Programming Fundamentals
Repetition: An Introduction
Types of loops:
- Counter-controlled loops
(e.g. WHILE.., FOR.., DO..WHILE)
- Conditional loops
(e.g. WHILE.., DO..WHILE)
3
Programming Fundamentals
1
Repetition: An Introduction
Pre-test loops
the test is made before entering the loop
while loops
for loops
Post-test loop
the test is done at the end of the loop
do … while loops
Example
int sum ;
sum = 1+2+3+4+5+……..+10 ;
cout << sum ;
5
Programming Fundamentals
Programming Fundamentals
? 6
2
Counter-Controlled Loop
NOTE:
All types of repetition statements could be used as counter-controlled
loops. The FOR statement is based suited for this type of looping.
7
Programming Fundamentals
lcv initial_value
False
lcv final_value
True
continue
Statements
3
The for loop executes as follows:
1. The initial statement executes.
2. The loop condition is evaluated. If the loop condition evaluates to true
i. Execute the for loop statement.
ii. Execute the update statement (the third expression in the
parentheses).
3. Repeat Step 2 until the loop condition evaluates to false.
The initial statement usually initializes a variable (also called the for loop
control, or for indexed, variable).
10
If the loop condition is initially false, the loop body does not execute.
C++ allows you to use fractional values for loop control variables of the
double type (or any real data type).
A semicolon at the end of the for statement (just before the body of the
loop) is a semantic error. In this case, the action of the for loop is empty.
In the for statement, if the loop condition is omitted, it is assumed to
be true.
In a for statement, you can omit all three statements—initial statement,
loop condition, and update statement. The following is a legal for loop:
for (;;)
cout << "Hello" << endl;
11
11
Example of Repetition
int n;
12
12
4
num ?
Example of Repetition
int num;
OUTPUT
13
13
num 1
Example of Repetition
int num;
OUTPUT
14
14
num 1
Example of Repetition
int num;
true
OUTPUT
15
15
5
num 1
Example of Repetition
int num;
OUTPUT
1Apple
16
16
num 2
Example of Repetition
int num;
OUTPUT
1Apple
17
17
num 2
Example of Repetition
int num;
true
for ( num = 1 ; num <= 3 ; num++ )
OUTPUT
1Apple
18
18
6
num 2
Example of Repetition
int num;
OUTPUT
1Apple
2Apple
19
19
num 3
Example of Repetition
int num;
OUTPUT
1Apple
2Apple
20
20
num 3
Example of Repetition
int num;
true
for ( num = 1 ; num <= 3 ; num++ )
OUTPUT
1Apple
2Apple
21
21
7
num 3
Example of Repetition
int num;
OUTPUT
1Apple
2Apple
22
3Apple
22
num 4
Example of Repetition
int num;
OUTPUT
1Apple
2Apple
23
3Apple
23
num 4
Example of Repetition
int num;
false
for ( num = 1 ; num <= 3 ; num++ )
OUTPUT
1Apple
2Apple
24
3Apple
24
8
num 4
Example of Repetition
int num;
false
for ( num = 1 ; num <= 3 ; num++ )
25
The output is
1Apple
2Apple
3Apple
26
26
#include <iostream>
using namespace std;
void main ( )
{
for( I = 1;I<= 10;I++ )
cout << I<< “\n”;
}
27
Programming Fundamentals
27
9
Exercise
28
Programming Fundamentals
28
Write an algorithm that finds the sum of the first 5 positive integer
numbers
#include <iostream>
using namespace std;
void main ( )
{
int I, sum = 0;
for ( I = 1; I < =5; I++ )
sum = sum + I;
cout << “ Sum = “ << sum << endl;
}
29
Programming Fundamentals
29
30
10
Assignment
1. Write an algorithm to calculate the following:
n
1
Y 1 for any given x , n
i 1 xi
31
Programming Fundamentals
31
32
1. #include <iostream>
2. using namespace std;
3. void main ( )
4. { int I, n, max, min;
5. cout << “ Enter a number: ” ;
6. cin >> n ;
7. max = n ; min = n ;
8. for ( I = 2 ; I <= 10; I++ )
9. { cout << “ Enter a number: ” ;
10. cin >> n ;
11. if ( n > max )
12. max = n ;
13. if ( n < min )
14. min = n ;
15. }
16. cout << “ Max = “<< max<< “ Min = “<< min<< endl;
17. }
33
Programming Fundamentals
33
11
Thank You
34
12