0% found this document useful (0 votes)
7 views12 pages

Lec-09 - W9 (Loops, For Loop)

This document covers the fundamentals of loops in C++, specifically focusing on repetition structures such as FOR, WHILE, and DO..WHILE loops. It explains the types of loops, their control statements, and provides examples of how to implement them in C++. Additionally, it includes exercises and assignments for further practice.

Uploaded by

aurangzeb5060
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views12 pages

Lec-09 - W9 (Loops, For Loop)

This document covers the fundamentals of loops in C++, specifically focusing on repetition structures such as FOR, WHILE, and DO..WHILE loops. It explains the types of loops, their control statements, and provides examples of how to implement them in C++. Additionally, it includes exercises and assignments for further practice.

Uploaded by

aurangzeb5060
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Programming Fundamental

C++
Lecture W

Loop - Repetition structure

Topics to cover here:

 Introduction to Repetition
 FOR .. Statement

2
Programming Fundamentals

Repetition: An Introduction

 You can repeat many steps in an algorithm.


 Repetition of a sequence of steps in an algorithm is called a
loop.

 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

Find the Sum of the first 100 Integer starting from 1

Programming Fundamentals
? 6

2
Counter-Controlled Loop

 The repetition of this loop is controlled by a loop control variable (lcv)


whose value represents a count.
 This type of loops is used when you can determine prior to loop execution
exactly how many loop repetitions will be needed to solve a problem.
 The lcv should be incremented as the final statement of the 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

The for Loop

 The general form of the for statement is:


for (initialize lcv; loop condition;
update lcv)
statement

 The initial statement, loop condition, and update statement are


called for loop control statements

The for Loop.. Flowchart

lcv  initial_value

False
lcv  final_value

True
continue
Statements

lcv  update lcv _value

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

The for Loop (comments)

 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;

for ( int i = 1 ; i <= n ; i++ )


{
cout << i << “ Apple” << endl;
}

12

12

4
num ?
Example of Repetition

int num;

for ( int num = 1 ; i <= 3 ; num++ )

cout << num << “ Apple” << endl;

OUTPUT

13

13

num 1
Example of Repetition

int num;

for ( num = 1 ; num <= 3 ; num++ )

cout << num << “Apple” << endl;

OUTPUT

14

14

num 1
Example of Repetition

int num;
true

for ( num = 1 ; num <= num++


3; )

cout << num << “Apple” << endl;

OUTPUT

15

15

5
num 1
Example of Repetition

int num;

for ( num = 1 ; num <= 3 ; num++ )

cout << num << “Potato” << endl;

OUTPUT

1Apple

16

16

num 2
Example of Repetition

int num;

for ( num = 1 ; num <= 3 ; num++ )

cout << num << “Potato” << endl;

OUTPUT

1Apple

17

17

num 2
Example of Repetition

int num;
true
for ( num = 1 ; num <= 3 ; num++ )

cout << num << “Potato” << endl;

OUTPUT

1Apple

18

18

6
num 2
Example of Repetition

int num;

for ( num = 1 ; num <= 3 ; num++ )

cout << num << “Potato” << endl;

OUTPUT

1Apple
2Apple
19

19

num 3
Example of Repetition

int num;

for ( num = 1 ; num <= 3 ; num++ )

cout << num << “Potato” << endl;

OUTPUT

1Apple
2Apple
20

20

num 3
Example of Repetition

int num;
true
for ( num = 1 ; num <= 3 ; num++ )

cout << num << “Potato” << endl;

OUTPUT

1Apple
2Apple
21

21

7
num 3
Example of Repetition

int num;

for ( num = 1 ; num <= 3 ; num++ )

cout << num << “Potato” << endl;

OUTPUT

1Apple
2Apple
22
3Apple

22

num 4
Example of Repetition

int num;

for ( num = 1 ; num <= 3 ; num++ )

cout << num << “Potato” << endl;

OUTPUT

1Apple
2Apple
23
3Apple

23

num 4
Example of Repetition

int num;
false
for ( num = 1 ; num <= 3 ; num++ )

cout << num << “Apple” << endl;

OUTPUT

1Apple
2Apple
24
3Apple

24

8
num 4
Example of Repetition

int num;
false
for ( num = 1 ; num <= 3 ; num++ )

cout << num << “Potato” << endl;

When the loop control condition


is evaluated and has value false, the
loop is said to be “satisfied” and
control passes to the statement
following the for statement. 25

25

The output is

1Apple
2Apple
3Apple

26

26

Example 2: C++ Program

 Write an algorithm that print the first 10 positive integer


numbers

#include <iostream>
using namespace std;
void main ( )
{
for( I = 1;I<= 10;I++ )
cout << I<< “\n”;
}

27
Programming Fundamentals

27

9
Exercise

 Modify the above Example so that it prints the numbers


from 1 to n.

 Modify the above Example so that it prints the numbers


from m to n.

28
Programming Fundamentals

28

Example 2: C++ Program

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

Example: Create a table with a for loop


#include <iostream>
using namespace std;
void main ( )
{
int num;
cout << "NUMBER\tSQUARE\tCUBE\n";
cout <<"------\t------\t----\n";
for (num = 1; num < 11; num++) {
cout << num << "\t";
cout << num * num << "\t";
cout << num * num * num<< "\n";
}
} NUMBER SQUARE CUBE
---------- ---------- ------
1 1 1
2 4 8
. . .
. . .
10 100 1000

30

10
Assignment
1. Write an algorithm to calculate the following:
n
1
Y 1   for any given x , n
i 1 xi

2. Write an algorithm that reads 10 numbers and finds the


maximum and minimum numbers among them.

31
Programming Fundamentals

31

Example 4: C++ Program

1. /* The program calculates a series.*/


2. #include <iostream>
3. #include <cmath>
4. using namespace std;
5. void main ( )
6. { int i, n, x;
7. double y = ;
8. cout <<“ Enter value of x and the number n:”;
9. cin >> x >> n ;
10. for ( i = 1; i <= n ; i++ )
11. y = y + 1 / pow ( x , i ) ;
12. cout << “ y = “ << y << endl ;
13. }
32
Programming Fundamentals

32

Example 5: C++ Program

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

You might also like