COMPUTER SCIENCE PROGRAM Chapter 5

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 29

Introduction To

Computer Programming
(CSC425)

by
PUAN AFIZA ISMAIL
Faculty of Computer & Mathematical Sciences
UiTM MALAYSIA
2013

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
October 1, 2013 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
October 1, 2013 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;

 The initial statement, loop condition, and


update statement are called for loop
control statements
October 1, 2013 CSC425 : INTRODUCTION TO COMPUTER 5
PROGRAMMING
5.2
The for loops (cont.)

October 1, 2013 CSC425 : INTRODUCTION TO COMPUTER 6


PROGRAMMING
5.2
The for loops (cont.)
 The initial statement consists of a
single statement used to set the initial or
starting value of a variable called counter
variable
 The loop condition tests the value of the
counter variable and determines when the
loop is to stop
 The expression provides the increment
value added or subtracted from the counter
variable each time the loop is executed
October 1, 2013 CSC425 : INTRODUCTION TO COMPUTER 7
PROGRAMMING
5.2
The for loops (cont.)
 During the execution of the for statement, the following steps of action
occurs :
1. The initial statement is evaluated.
2. The loop condition is evaluated.
3. If the loop condition evaluates to true, then
i. statement is executed
ii. execute the update statement (the third
expression in the parentheses).
iii. repeat Step 2 until the loop condition evaluates to

false.
otherwise the for loop terminates and control transfers to the
next statement following it.

October 1, 2013 CSC425 : INTRODUCTION TO COMPUTER 8


PROGRAMMING
5.2
The for loops (cont.)
The use of for loops are shown as follows :
Example 1 :

October 1, 2013 CSC425 : INTRODUCTION TO COMPUTER 9


PROGRAMMING
5.2
The for loops (cont.)
Example 2 :

October 1, 2013 CSC425 : INTRODUCTION TO COMPUTER 10


PROGRAMMING
5.2
The for loops (cont.)
 Example 3 :

October 1, 2013 CSC425 : INTRODUCTION TO COMPUTER 11


PROGRAMMING
5.2
The for loops (cont.)
Counting the number of repetition :
 Symmetric loop (a <= n <= b)

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)
for (i = 10; i < 40; i += 5)
the counts is
(b – a)/c = (40-10)/5 = 6
October 1, 2013 CSC425 : INTRODUCTION TO COMPUTER 12
PROGRAMMING
5.2
The for loops (cont.)
2 forms of the for statement :
 Ascending for loop
Example :
for (variable=initial_value; variable <=
final_value; increment expression)
e.g : for (i = 10; i <= 40; i += 5)

 Descending for loop


for (variable=initial_value; variable >=
final_value; decrement expression)
e.g : for (i = 40; i >= 10; i -= 5)
October 1, 2013 CSC425 : INTRODUCTION TO COMPUTER 13
PROGRAMMING
5.2.1
The nested for loops
 Loop contained within another loop
 Example 4 :

October 1, 2013 CSC425 : INTRODUCTION TO COMPUTER 14


PROGRAMMING
5.2.1
The nested for loops (cont.)
 Example 5 :
How to display a pattern of numbers like this :

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

October 1, 2013 CSC425 : INTRODUCTION TO COMPUTER 15


PROGRAMMING
5.2.1
The nested for loops (cont.)
 Example 5 :
The program :

October 1, 2013 CSC425 : INTRODUCTION TO COMPUTER 16


PROGRAMMING
5.3
The while loops
 Required for repetitive execution that cannot be determined in
advance
 The syntax :

while (condition)
statement;

 statement can be simple or compound; the body of the loop


 condition acts as a decision maker and is usually a logical
expression
 The parentheses are part of the syntax

October 1, 2013 CSC425 : INTRODUCTION TO COMPUTER 17


PROGRAMMING
5.3
The while loops (cont.)
 condition provides an entry condition
 statement executes if the expression initially evaluates to true
 Loop condition is then reevaluated
 Statement continues to execute until the expression is no longer
true (false)

October 1, 2013 CSC425 : INTRODUCTION TO COMPUTER 18


PROGRAMMING
5.3
The while loops (cont.)
5.3.1 Counter-Controlled while Loops :
If you know exactly how many pieces of data need to
be read, the while loop becomes a counter-
controlled loop

October 1, 2013 CSC425 : INTRODUCTION TO COMPUTER 19


PROGRAMMING
5.3
The while loops (cont.)
 Counter-Controlled while Loops :
Example 6 :

October 1, 2013 CSC425 : INTRODUCTION TO COMPUTER 20


PROGRAMMING
5.3
The while loops (cont.)
5.3.2 Sentinel-Controlled while Loops :
Sentinel variable is tested in the condition and loop ends when
sentinel is encountered

See Example 7.
October 1, 2013 CSC425 : INTRODUCTION TO COMPUTER 21
PROGRAMMING
5.3
The while loops (cont.)

October 1, 2013 CSC425 : INTRODUCTION TO COMPUTER 22


PROGRAMMING
5.3
The do..while loops (cont.)
 The general form of a do...while statement is:

do
statement
while (expression);

 The statement executes first, and then the


expression is evaluated
 If the expression evaluates to true, the
statement executes again
 As long as the expression in a do...while
statement is true, the statement executes
October 1, 2013 CSC425 : INTRODUCTION TO COMPUTER 23
PROGRAMMING
5.3
The do..while loops (cont.)

 See Example 8, Example 9 & Example 10.


October 1, 2013 CSC425 : INTRODUCTION TO COMPUTER 24
PROGRAMMING
Example 8 :
5.3
The do..while loops

October 1, 2013 CSC425 : INTRODUCTION TO COMPUTER 25


PROGRAMMING
5.3
Example 9 :

The do..while loops

October 1, 2013 CSC425 : INTRODUCTION TO COMPUTER 26


PROGRAMMING
Example 10 :
5.3
The do..while loops

October 1, 2013 CSC425 : INTRODUCTION TO COMPUTER 27


PROGRAMMING
5.4
‘break’ & ‘continue’ statement

 A break statement forces an immediate exit


when executed in a while, for, do while or
switch statement
 A continue statement skips the remaining
statements in the body of the structure and
proceeds with the next iteration of the loop
 See Example 12.

October 1, 2013 CSC425 : INTRODUCTION TO COMPUTER 28


PROGRAMMING
Example 12 :

5.4
‘break’ & ‘continue’ statement

October 1, 2013 CSC425 : INTRODUCTION TO COMPUTER 29


PROGRAMMING

You might also like