Lecture 7. Iteration Structures “Loops” (Part One)
Lecture 7. Iteration Structures “Loops” (Part One)
Programming Languages
Lecture (7)
Submitted By:
2
Basic Control Structures
Statement 1
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:
7
Repetition Essentials Cont.
➢ Using For Loop Statement:
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.
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.
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 !
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!