Lecture 4 Control Structures - Repetitive Statements Loops 14032023 102037am
Lecture 4 Control Structures - Repetitive Statements Loops 14032023 102037am
LECTURE
Abrar Ahmed
[email protected]
Outline
• for Loop
• while Loop
• do-while Loop
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
Loops
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.
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.
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;
}
Better way
void main()
{
4
3/14/2023
Loops
for Loop
10
5
3/14/2023
• 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.
11
for Loop
• The general form of for statement is as under.
12
6
3/14/2023
int main ()
{
int i;
13
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
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;
}
}
15
16
8
3/14/2023
#include<iostream>
using namespace std;
void main()
{
for(int i=0;i<=5;i++)
cout<<i<<endl;
}
17
void main ( )
{
18
9
3/14/2023
for Loop
19
20
10
3/14/2023
false
Test Exp
true
Body of Loop
Increment Exp
21
{
statement 1;
statement 2;
statement 3;
22
11
3/14/2023
23
void main(void)
{
for (int numb = 0; numb < 10; numb++)
{
int square = numb * numb;
cout << "Number: " << numb << "Square : " << square << endl;
}
}
24
12
3/14/2023
• Variations
• Increment Expression – May perform any operation
int i;
for (i = 10; i >= 0; i--)
25
26
13
3/14/2023
Degrees to Radians
int main(void)
{
const float PI = 3.141593;
double radians;
27
28
14
3/14/2023
29
30
15
3/14/2023
Example
31
Factorial
int value;
int factorial = 1;
cout << "Enter a positive value : ";
cin >> value;
32
16
3/14/2023
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.
40
17
3/14/2023
41
Example 1
for(num2 = 0; num2 <= 3; num2++)
{
for(num1 = 0; num1 <= 2; num1++)
{
cout<< num2<< " " << num1<< endl;
}
}
42
18
3/14/2023
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)
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;
}
44
19
3/14/2023
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
45
int value;
cout << "Enter a positive value : ";
cin >> value; // 5
46
20
3/14/2023
Tables Example
47
Square Example
#include<iomanip>
int squareValue;
cout << "Enter a positive value for creating a star square : ";
cin >> squareValue;
48
21
3/14/2023
Triangle Example
int triangleValue;
cout << "Enter a positive value for creating a star triangle : ";
cin >> triangleValue;
49
Triangle Example
50
22
3/14/2023
char triangle
char lastChar;
char startingChar = 'A';
cout << "Enter the uppercase last character you want to print : ";
cin >> lastChar;
51
Another
Example
52
23
3/14/2023
Another
Example
53
Variable Visibility
54
24
3/14/2023
55
25