Professional Documents
Culture Documents
Problem Solving
(CSC415)
by
PUAN AFIZA ISMAIL
Faculty of Computer & Mathematical Sciences
UiTM MALAYSIA
1
Repetition Structures
Chapter 5
2
Contents
5.1 Introduction
5.2 The for loops
5.2.1 Nested for loops
5.3 The while loops
5.3.1 Counter-controlled while loops
5.3.2 Sentinel-controlled while loops
5.4 The do..while loops
5.5 break and continue statement
November 23, 2020 CSC425 : INTRODUCTION TO COMPUTER 3
PROGRAMMING
5.1
Introduction
The repetition structure is a section of
repeating code in a program a.k.a a loop
The loop contains a set of statements to
be executed repeatedly based on a
condition
3 basic types of loops :
for
while
do..while
November 23, 2020 CSC425 : INTRODUCTION TO COMPUTER 4
PROGRAMMING
5.2
The for loops
The general form of the for statement is:
for (initial statement; loop condition;
update statement)
statement;
false.
otherwise the for loop terminates and control transfers to the
next statement following it.
Example :
for (i = 10; i <= 40; i += 5)
the counts is
(b – a)/c+1 = (40-10)/5 + 1 = 7
Asymmetric loop (a <= n < b)
Example :
for (variable=initial_value; variable <=
final_value; increment expression)
e.g : for (i = 10; i <= 40; i += 5)
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7
1 2 3 4 5 6
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
while (condition)
statement;
See Example 7.
November 23, 2020 CSC425 : INTRODUCTION TO COMPUTER 21
PROGRAMMING
5.3
The while loops (cont.)
do
statement
while (expression);
5.4
‘break’ & ‘continue’ statement