Lecture 8. Iteration Structures “Loops” (Part Two)
Lecture 8. Iteration Structures “Loops” (Part Two)
Programming Languages
Lecture (8)
Submitted By:
2
Repetition Essentials
▪ Most Program involve Repetition or Lopping. A loop is a
group of instructions that the Computer executes repeatedly
while some loop continuation Condition remains true.
▪ There are two means of repetition:
1. Counter-Controlled 2. Sentinel-Controlled
Repetition Repetition
is sometimes called Definite is sometimes called Indefinite
Repetition because we know in Repetition because it’s not
advance exactly how many known in advance exactly how
times the loop will be executed. many times the loop will be
executed.
3
Repetition Essentials Cont.
➢ C++ provides three types of loop statements: For loops and
While loops, do-while loops. Which classified into:
1. Entry Controlled Loops 2. Exit Controlled Loops
▪ In this of loops the test ▪ In this of loops the test Condition
Condition is tested or evaluated is tested or evaluated at the End of
Before entering the loop body. the loop body.
▪ Therefore, the loop body will
▪ For Loop and While Loop are execute at least once, irrespective
Entry Controlled Loops. of whether the test condition is
true or false.
▪ Do-While Loop is Exit Controlled
Loop.
4
Examples of using for statement
➢ Write a Program to Print the Factorial Num ! fact i
1 5
n 5
4
3
20 2
60
5 1
120 0
5
Examples of using for statement
6
Examples of using while statement
➢ Write a Program to Print the Factorial of a number !
7
Examples of using for statement
➢ Write a Program to read from Student the degrees of 5 Subjects.
➢ Then Print the following:
1. No. of Subjects that the student has Passed,
2. No. of Subjects that the student has Failed (when the degree less than 50),
3. Total degrees & the Average of these degrees.
8
Examples of using for statement
degree total avg
90 0
90 0
88.5
178.5
46.5 255 80.6
85.5 310.5
92.5 403
pass fail
i
1
0 0 2
1 1 3
2 4
3 5
4 6
9
Examples of using for statement
10
Examples of using for statement
➢ Write a Program to read from user a set of Positive integer
numbers that divisible by 3 or Enter Zero to End.
11
Examples of using for statement
12
Examples of using while statement
13
Trace the following Statement
Memory
x y
1 20
6 17
11 14
16 11
14
Do … While Loop
15
Nested Loops
➢ Write a Program to Print the Multiplication Table
16
Nested Loops
➢ Write a Program to Print the Multiplication Table
17
18
Break and Continue Statement
19
Break and Continue Statement
20
Thanks!