0% found this document useful (0 votes)
2 views

Lecture 7. Iteration Structures “Loops” (Part One)

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lecture 7. Iteration Structures “Loops” (Part One)

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Lectures Notes in

Programming Languages

Lecture (7)
Submitted By:

Dr. Ahmed Mohamed Abd-Elwahab


Lecturer in BIS Department,
Faculty of Commerce & Business Administration,
Helwan University, Cairo, Egypt.
CHAPTER (4)

Iteration Structures “Loops”

2
Basic Control Structures

Statement 1
Condition ?

Statement 2 Statement Condition ?

Statement 1 Statement 2
Statement 3

3
Iteration Structures “Loops”

4
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.
5
Repetition Essentials Cont.
➢ Suppose that we need to display a string message
(e.g., Welcome to C++) a hundred times. It would be tedious to
have to write the following statement a hundred times:

➢ So, How do we solve this problem?


C++ provide a Powerful Construct called a Loop that
controls how many times an operation or sequence of
operations is Performed in Succession. 6
Repetition Essentials Cont.
➢ Using While Loop Statement:

7
Repetition Essentials Cont.
➢ Using For Loop Statement:

▪ Loops are constructs that control repeated executions of


a block of statements. 8
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.
9
Repetition Essentials Cont.
➢ C++ provides three types of loop statements: For loops and
While loops, do-while loops. Which classified into:

10
For Loop
➢ A for loop is a repetition control structure which allows us to
write a loop that is executed a specific number of times.
➢ The loop enables us to perform n number of steps together in
one line. Syntax of for Loop:

11
For Loop
➢ The various parts of the For loop are:

12
Examples of using for statement

13
Examples of using for statement
➢ The following example show methods of varying the control
variable in a for statement.
1. Vary the control variable from 1 to 100 increment by 1.

2. Vary the control variable from 100 to 1 decrement by 1

3. Vary the control variable from 7 to 77 in steps of 7.

14
Examples of using for statement
➢ The following example show methods of varying the control
variable in a for statement.
4. Vary the control variable from 20 to 2 in steps of 2.

5. Vary the control variable over the following sequence of


values: 2, 5, 8, 11, 14, 17.

6. Vary the control variable from 5 to 100 in divisible be 5 .

15
Examples of using for statement
➢ Write a program to Print the Numbers from 1 to 20 each
number in separate line.

16
Examples of using for statement
➢ Write a program to Print the Numbers from -3 to 3 in
increment by 0.5 each number in separate line.

17
Examples of using for statement
➢ Write a program to Print the Summation of Even Numbers
from 1 to 20.

18
Examples of using for statement
➢ Write a program to Print the Sum Even numbers and the
Sum Odd numbers from 1 to 10.

19
Assignment
➢ Write a program to Print the
Factorial of a number !

For Example: Factorial num 5 ! is =


5 * 4 * 3 * 2 * 1 = 120

23
Assignment
➢ 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.
23
Thanks!

Ahmed Mohamed Abd-Elwahab


22

You might also like