0% found this document useful (0 votes)
51 views25 pages

Lecture 4 Control Structures - Repetitive Statements Loops 14032023 102037am

The document is a lecture slide presentation on loops in computer programming. It introduces the three main types of loops: for loops, while loops, and do-while loops. It explains that loops allow code to be repeated until a condition is met and make decisions each repetition on whether to continue repeating. The presentation provides examples of problems that require repetitive solutions and how loops can provide a better way to repeatedly print a message instead of copying and pasting code. It then dives into each loop type.

Uploaded by

ayan khan
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)
51 views25 pages

Lecture 4 Control Structures - Repetitive Statements Loops 14032023 102037am

The document is a lecture slide presentation on loops in computer programming. It introduces the three main types of loops: for loops, while loops, and do-while loops. It explains that loops allow code to be repeated until a condition is met and make decisions each repetition on whether to continue repeating. The presentation provides examples of problems that require repetitive solutions and how loops can provide a better way to repeatedly print a message instead of copying and pasting code. It then dives into each loop type.

Uploaded by

ayan khan
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/ 25

3/14/2023

LECTURE

4 Control Structures – Repetitive Statements/Loops


CSC 113
Computer Programming
Spring 2023

Abrar Ahmed
[email protected]

Department of Computer Science


Bahria University, Islamabad

CSC 113 – Computer Programming


Department of Computer Sciences
4. Control Structures – Repetitive Statements/Loops
Bahria University, Islamabad
Slide: 1

Outline

• for Loop
• while Loop
• do-while Loop

CSC 113 – Computer Programming


Department of Computer Sciences
4. Control Structures – Repetitive Statements/Loops
Bahria University, Islamabad
Slide: 2

1
3/14/2023

• 3 control structures
– Sequence structure
• Programs executed sequentially by default
– Selection structures
• if, if/else, switch
– Repetition structures - Loops
• while, do/while, for

CSC 113 – Computer Programming


Department of Computer Sciences
4. Control Structures – Repetitive Statements/Loops
Bahria University, Islamabad
Slide: 3

Loops

CSC 113 – Computer Programming


Department of Computer Sciences
4. Control Structures – Repetitive Statements/Loops
Bahria University, Islamabad
Slide: 4

2
3/14/2023

Loops
• The following "non-computer" algorithms involve repetition:
• Compute the course grade for every student in a class.
• Microwave the food until the timer reaches 0, the cancel button is hit, or the door is opened.
• While the ATM is running, process another customer.

CSC 113 – Computer Programming


Department of Computer Sciences
4. Control Structures – Repetitive Statements/Loops
Bahria University, Islamabad
Slide: 5

Loops
• The loop logic structure is the repeating structure. It allows code to be repeated until a condition in the data
is met.
• Loops also allow the programmer to return to an earlier portion of the code.
• A looping structure makes a decision every time the loop is repeated.
• As long as the decision is true, the statements in the loop repeat. A loop stops when the loop test is false.

CSC 113 – Computer Programming


Department of Computer Sciences
4. Control Structures – Repetitive Statements/Loops
Bahria University, Islamabad
Slide: 6

3
3/14/2023

Naïve Example
• Print ‘Hello World’ 5 times

void main()
{
cout << "Hello World" << endl;
cout << "Hello World" << endl;
cout << "Hello World" << endl;
cout << "Hello World" << endl;
cout << "Hello World" << endl;
}

• What if you have to print this 50 or 500 times or even more….. ?

CSC 113 – Computer Programming


Department of Computer Sciences
4. Control Structures – Repetitive Statements/Loops
Bahria University, Islamabad
Slide: 7

Better way

void main()
{

Repeat this part n-times


{
cout << "Hello World" << endl;
}

CSC 113 – Computer Programming


Department of Computer Sciences
4. Control Structures – Repetitive Statements/Loops
Bahria University, Islamabad
Slide: 8

4
3/14/2023

Loops

for (start; test; action) //the for loop


{
//Statements to be repeated
}

while (T / F) // The while loop


{
//Statements to be repeated
}

do//The do - while loop


{
//Statements to be repeated
} while (T / F); //repeats on True

CSC 113 – Computer Programming


Department of Computer Sciences
4. Control Structures – Repetitive Statements/Loops
Bahria University, Islamabad
Slide: 9

for Loop

CSC 113 – Computer Programming


Department of Computer Sciences
4. Control Structures – Repetitive Statements/Loops
Bahria University, Islamabad
Slide: 10

10

5
3/14/2023

The for loop

• for loop executes a section of code a fixed number of times. Its usually used when you
know, before entering the loop, how many times you want to execute the code.
• The for allows us to specify three things about a loop in a single line.
a) Setting a loop counter to an initial value.
b) Testing the loop counter to determine whether its value has reached the number of
repetitions desired.
c) Increasing the value of loop counter each time the program segment within the loop
has been executed.

CSC 113 – Computer Programming


Department of Computer Sciences
4. Control Structures – Repetitive Statements/Loops
Bahria University, Islamabad
Slide: 11

11

for Loop
• The general form of for statement is as under.

for ( initialization expression; test expression; increment/decrement expression)


{
statement 1;
statement 2;
.
.
statement n;
}

CSC 113 – Computer Programming


Department of Computer Sciences
4. Control Structures – Repetitive Statements/Loops
Bahria University, Islamabad
Slide: 12

12

6
3/14/2023

The for loop


• Executed a fixed number of times

int main ()
{
int i;

for( i = 0; i < 10; i++)


cout << i << endl;
}

Initialization expression Test expression Increment Expression

for( i = 0; i < 10; i++)

CSC 113 – Computer Programming


Department of Computer Sciences
4. Control Structures – Repetitive Statements/Loops
Bahria University, Islamabad
Slide: 13

13

The for loop


• Executed a fixed number of times

int main()
{
for (int loop = 0; loop < 10; loop++)
cout << loop << endl;
}
int main()
{
for (int loop = 9; loop >= 0; loop--)
cout << loop << endl;
}
Initialization expression Test expression Increment Expression

for (int loop = 0; loop < 10; loop++)

CSC 113 – Computer Programming


Department of Computer Sciences
4. Control Structures – Repetitive Statements/Loops
Bahria University, Islamabad
Slide: 14

14

7
3/14/2023

Example

int main()
{
for (int loop = 0; loop <= 5; loop++)
{
cout << "Hello World" << endl;
}
}

int main()
{
for (int loop = 1; loop <= 5; loop++)
{
cout << loop << endl;
}
}

CSC 113 – Computer Programming


Department of Computer Sciences
4. Control Structures – Repetitive Statements/Loops
Bahria University, Islamabad
Slide: 15

15

for Loop Example


void main ( )
{
int j;
for ( j=0; j < 15; j++ )
cout<< j;
}

CSC 113 – Computer Programming


Department of Computer Sciences
4. Control Structures – Repetitive Statements/Loops
Bahria University, Islamabad
Slide: 16

16

8
3/14/2023

for loop - Example

// displays numbers From 0 to 5

#include<iostream>
using namespace std;
void main()
{
for(int i=0;i<=5;i++)
cout<<i<<endl;
}

CSC 113 – Computer Programming


Department of Computer Sciences
4. Control Structures – Repetitive Statements/Loops
Bahria University, Islamabad
Slide: 17

17

for loop - Example

Write a program to display odd numbers from 0 to less than 20.

void main ( )
{

for ( int j=1; j < 20; j+=2 )


cout<< j;
}

CSC 113 – Computer Programming


Department of Computer Sciences
4. Control Structures – Repetitive Statements/Loops
Bahria University, Islamabad
Slide: 18

18

9
3/14/2023

for Loop

• The Initialization Expression


• The initialization expression is executed only once, when the loop first starts. It gives
the loop variable an initial value.
• The Test Expression
• The test expression usually involves a relational operator. It is evaluated each time
through the loop, just before the body of the loop is executed. It determine whether
the loop will be executed again or not.
• The Increment/Decrement Expression
• The expression changes the value of the loop variable, often by
incrementing/decrementing it. It is always executed at the end of the loop, after the
loop body has been executed.

CSC 113 – Computer Programming


Department of Computer Sciences
4. Control Structures – Repetitive Statements/Loops
Bahria University, Islamabad
Slide: 19

19

The for Loop

When a for loop is encountered, the initial-statement is


executed. The loop-test is executed. If loop-test is false,
the for loop is terminated. If loop-test is true, the
iterative-part is executed and the increment-statement is
executed. The process repeats by evaluating loop-test,
and so on ...

CSC 113 – Computer Programming


Department of Computer Sciences
4. Control Structures – Repetitive Statements/Loops
Bahria University, Islamabad
Slide: 20

20

10
3/14/2023

The for loop


Initialization Exp

false
Test Exp

true

Body of Loop

Increment Exp

CSC 113 – Computer Programming


Department of Computer Sciences
4. Control Structures – Repetitive Statements/Loops
Bahria University, Islamabad
Slide: 21

21

The for loop


for (initialization; Test; increment)
statement;

for (initialization; Test; increment)

{
statement 1;
statement 2;
statement 3;

CSC 113 – Computer Programming


Department of Computer Sciences
4. Control Structures – Repetitive Statements/Loops
Bahria University, Islamabad
Slide: 22

22

11
3/14/2023

For Repetition Structure


• Pretest loop
• Syntax:

CSC 113 – Computer Programming


Department of Computer Sciences
4. Control Structures – Repetitive Statements/Loops
Bahria University, Islamabad
Slide: 23

23

The for loop


• Blocks

void main(void)
{
for (int numb = 0; numb < 10; numb++)
{
int square = numb * numb;
cout << "Number: " << numb << "Square : " << square << endl;
}
}

CSC 113 – Computer Programming


Department of Computer Sciences
4. Control Structures – Repetitive Statements/Loops
Bahria University, Islamabad
Slide: 24

24

12
3/14/2023

The for loop

• Variations
• Increment Expression – May perform any operation

for (int i = 10; i >= 0; i--)

for (int i = 0; i <= 10; i += 2)

for (int i = 1; i < 100; i *= 2)

◼ Variables defined in for Statements

int i;
for (i = 10; i >= 0; i--)

for (int i = 10; i >= 0; i--)

CSC 113 – Computer Programming


Department of Computer Sciences
4. Control Structures – Repetitive Statements/Loops
Bahria University, Islamabad
Slide: 25

25

The for Loop

◼ Multiple Initialization and Increment Expressions


• Can have more than one initialization expression.
• Can also have more than one increment expression.
• Only one test expression.
• Different Expression separated by commas.
for (int j = 0, int k = 100; j < 50; j++, k--)
{
// body of for loop.
}
for (int i = 0, int j = 0; j + i <= 10; j++, i++)
{
cout << j + i << endl;
}
CSC 113 – Computer Programming
Department of Computer Sciences
4. Control Structures – Repetitive Statements/Loops
Bahria University, Islamabad
Slide: 26

26

13
3/14/2023

The for Loop

Degrees to Radians

int main(void)
{
const float PI = 3.141593;
double radians;

cout << “Degrees to Radians “;


for (int degrees = 0; degrees <= 360; degrees += 10)
{
radians = degrees * PI / 180;
cout << radians << endl;
}
return 0;
}

CSC 113 – Computer Programming


Department of Computer Sciences
4. Control Structures – Repetitive Statements/Loops
Bahria University, Islamabad
Slide: 27

27

CSC 113 – Computer Programming


Department of Computer Sciences
4. Control Structures – Repetitive Statements/Loops
Bahria University, Islamabad
Slide: 28

28

14
3/14/2023

The for loop


Sum of all even numbers from 2 to 100
1
2 // Summation with for.
3 #include <iostream.h>
4
5 // function main begins program execution
6 void main(void)
7 {
8 int sum = 0; // initialize sum
9
10 // sum even integers from 2 through 100
11 for ( int number = 2; number <= 100; number += 2 )
12 sum += number; // add number to sum
13
14 cout << "Sum is " << sum << endl; // output sum
15
16 } // end function main

Output: Sum is 2550

CSC 113 – Computer Programming


Department of Computer Sciences
4. Control Structures – Repetitive Statements/Loops
Bahria University, Islamabad
Slide: 29

29

The for loop //Example of a for loop that produces an average


of user-supplied values:
int n = 0;
int Value = 0;
int Sum;
float Average = 0;
cout << "Enter n: ";// get a value for the number of iterations
cin >> n;
Sum = 0; // Initialize sum to 0 to ensure the correct average

for (int Counter = 0; Counter < n; Counter = Counter + 1)


{
cout << "Enter number: ";
cin >> Value;
Sum = Sum + Value;//accumulate
}

Average = (float)Sum / n; // Compute and display the average


Average = static_cast<float>(Sum) / n; // Compute and display the average
cout << "Average: " << Average;
CSC 113 – Computer Programming
Department of Computer Sciences
4. Control Structures – Repetitive Statements/Loops
Bahria University, Islamabad
Slide: 30

30

15
3/14/2023

Example

for (int loop = 1; loop <= 10; loop++)


{
if (loop % 2 == 0)
{
cout << loop << endl;
}
}

CSC 113 – Computer Programming


Department of Computer Sciences
4. Control Structures – Repetitive Statements/Loops
Bahria University, Islamabad
Slide: 31

31

Factorial

int value;
int factorial = 1;
cout << "Enter a positive value : ";
cin >> value;

for (int loop = value; loop>1; loop--)


{
factorial *= loop;
}
cout << "Factorial of value " <<value<<" is : "<<factorial<<endl;

CSC 113 – Computer Programming


Department of Computer Sciences
4. Control Structures – Repetitive Statements/Loops
Bahria University, Islamabad
Slide: 32

32

16
3/14/2023

Nested for loop

CSC 113 – Computer Programming


Department of Computer Sciences
4. Control Structures – Repetitive Statements/Loops
Bahria University, Islamabad
Slide: 39

39

NESTED LOOPS

• The placing of one loop inside the body of another loop is called nesting.

• When you "nest" two loops, the outer loop takes control of the number of complete
repetitions of the inner loop.

• When working with nested loops, the outer loop changes only after the inner loop is
completely finished (or is interrupted.). Therefore, inner loop is executed the maximum
number of times.

• While all types of loops may be nested, the most commonly nested loops are for loops.

CSC 113 – Computer Programming


Department of Computer Sciences
4. Control Structures – Repetitive Statements/Loops
Bahria University, Islamabad
Slide: 40

40

17
3/14/2023

Nested For Loops


for ( init; condition; increment )
{
for ( init; condition; increment )
{ Inner Outer
statement(s);
}
statement(s); // you can put more statements.
}

CSC 113 – Computer Programming


Department of Computer Sciences
4. Control Structures – Repetitive Statements/Loops
Bahria University, Islamabad
Slide: 41

41

Example 1
for(num2 = 0; num2 <= 3; num2++)
{
for(num1 = 0; num1 <= 2; num1++)
{
cout<< num2<< " " << num1<< endl;
}
}

CSC 113 – Computer Programming


Department of Computer Sciences
4. Control Structures – Repetitive Statements/Loops
Bahria University, Islamabad
Slide: 42

42

18
3/14/2023

int num1 int num2

0 0
0 0
1 0 1
2

3 (end loop)
0 2
1 0 1 0
1
1 1
2

3 (end loop)
1 2 Screen
Memory 2 0 2 0
1

2
2 1
3 (end loop) 2 2
3 0

1
3 0
2 3 1
3 (end loop) 3 2
4 (end loop)

CSC 113 – Computer Programming


Department of Computer Sciences
4. Control Structures – Repetitive Statements/Loops
Bahria University, Islamabad
Slide: 43

43

Example 2
#include<iostream>
using namespace std;
int main()
{
for(int i=0;i<=5;i++){

for(int j=0;j<=i;j++)
{
cout<<j;
}
cout<<endl;
}
return 0;
}

CSC 113 – Computer Programming


Department of Computer Sciences
4. Control Structures – Repetitive Statements/Loops
Bahria University, Islamabad
Slide: 44

44

19
3/14/2023

Line by line variable values and output


i=0 i=4
j=0 j=0 to j=4
output
0
output

i=1 0
j=0 to j=1 0 1
output 0 1 2
0 1 2 3
0
0 1 0 1 2 3 4
i=2
j=0 to j=2
output

0 i=5
0 1 j=0 to j=5
0 1 2
final output:

i=3 0
j=0 to j=3
output 0 1
0 1 2
0 1 2 3
0
0 1 0 1 2 3 4
0 1 2
0 1 2 3 0 1 2 3 4 5

CSC 113 – Computer Programming


Department of Computer Sciences
4. Control Structures – Repetitive Statements/Loops
Bahria University, Islamabad
Slide: 45

45

int value;
cout << "Enter a positive value : ";
cin >> value; // 5

for (int line = 1; line <= value; line++)


{//5
for (int star = 1; star <= value; star++)
{
cout << “ ”<< star;
}
cout << endl;
}

CSC 113 – Computer Programming


Department of Computer Sciences
4. Control Structures – Repetitive Statements/Loops
Bahria University, Islamabad
Slide: 46

46

20
3/14/2023

Tables Example

for (int tableOf = 1; tableOf <= 20; tableOf++)


{
for (int tableRange = 1; tableRange <= 10; tableRange++)
{
cout << setw(5) << tableOf * tableRange;
}
cout << endl;
}

CSC 113 – Computer Programming


Department of Computer Sciences
4. Control Structures – Repetitive Statements/Loops
Bahria University, Islamabad
Slide: 47

47

Square Example
#include<iomanip>
int squareValue;
cout << "Enter a positive value for creating a star square : ";
cin >> squareValue;

for (int lines = 1; lines <= squareValue; lines++)


{
for (int stars = 1; stars <= squareValue; stars++)
{
cout << setw(2) << "*";
//cout << setw(2) << stars;
}
cout << endl;
}

CSC 113 – Computer Programming


Department of Computer Sciences
4. Control Structures – Repetitive Statements/Loops
Bahria University, Islamabad
Slide: 48

48

21
3/14/2023

Triangle Example

int triangleValue;
cout << "Enter a positive value for creating a star triangle : ";
cin >> triangleValue;

for (int lines = 1; lines <= triangleValue; lines++)


{
for (int stars = 1; stars <= lines; stars++)
{
cout << setw(2) << "*";
//cout << setw(2) << stars;
}
cout << endl;
}

CSC 113 – Computer Programming


Department of Computer Sciences
4. Control Structures – Repetitive Statements/Loops
Bahria University, Islamabad
Slide: 49

49

Triangle Example

Find the output


int triangleValue;
cout << "Enter a positive value for creating a star triangle : ";
cin >> triangleValue;

for (int lines = triangleValue; lines >= 1; lines--)


{
for (int stars = 1; stars <= lines; stars++)
{
cout << setw(2) << "*";
//cout << setw(2) << stars;
}
cout << endl;
}

CSC 113 – Computer Programming


Department of Computer Sciences
4. Control Structures – Repetitive Statements/Loops
Bahria University, Islamabad
Slide: 50

50

22
3/14/2023

char triangle

char lastChar;
char startingChar = 'A';
cout << "Enter the uppercase last character you want to print : ";
cin >> lastChar;

for (int lines = 1; lines <= (lastChar-'A'+1); lines++)


{
for (int stars = 1; stars <= lines; stars++)
{
//cout << setw(2) << "*";
cout << setw(2) << startingChar;
}
startingChar++;
cout << endl;
}

CSC 113 – Computer Programming


Department of Computer Sciences
4. Control Structures – Repetitive Statements/Loops
Bahria University, Islamabad
Slide: 51

51

Another
Example

CSC 113 – Computer Programming


Department of Computer Sciences
4. Control Structures – Repetitive Statements/Loops
Bahria University, Islamabad
Slide: 52

52

23
3/14/2023

Another
Example

CSC 113 – Computer Programming


Department of Computer Sciences
4. Control Structures – Repetitive Statements/Loops
Bahria University, Islamabad
Slide: 53

53

Variable Visibility

• The loop body, which consists of braces delimiting several statements, is


called a block of code.

• Variable defined inside the block is not visible outside it.

• Visible means that program statements can access the variable.

CSC 113 – Computer Programming


Department of Computer Sciences
4. Control Structures – Repetitive Statements/Loops
Bahria University, Islamabad
Slide: 54

54

24
3/14/2023

Variable Visibility Example


void main ( )
{
for ( int i=0; i < 10; i++)
{
int j=i;
cout<< “hello world”<<j<<endl;
}
cout<< j; //Error: j is undefined/undeclared.
}

CSC 113 – Computer Programming


Department of Computer Sciences
4. Control Structures – Repetitive Statements/Loops
Bahria University, Islamabad
Slide: 55

55

25

You might also like