Topic 4 - Looping Statements Student
Topic 4 - Looping Statements Student
TOPIC 5
LOOPING STATEMENTS
AZHANI BINTI HASHIM / BILIK PENYELIA ECOM1
LEARNING OUTCOMES
Student should be able to:
• Define Looping statements.
• List types of Looping statements.
• Define FOR, WHILE, DO-WHILE loop statements.
• Describe structure of FOR, nested FOR, WHILE, DO-WHILE
loop statements.
• Identify need for BREAK, CONTINUE and GOTO statement.
• Differentiate FOR, WHILE, DO-WHILE loop statement.
2
1
21-Aug-19
2
21-Aug-19
3
21-Aug-19
4
21-Aug-19
10
5
21-Aug-19
11
6
21-Aug-19
14
7
21-Aug-19
SIMILARITY
FOR WHILE
counter = 1 Read counter
No No
counter <= 10 counter > 0
Yes Yes
x=x*x Print counter
15
16
8
21-Aug-19
9
21-Aug-19
printf(“\nFIRE! FIRE!“);
}
19
DIFFERENCE
WHILE DO-WHILE
Read counter Read counter
No Print counter
counter > 0
counter = counter - 1
Yes
Print counter
No
Print FIRE! FIRE! Print FIRE! FIRE!
20
10
21-Aug-19
EXERCISE
#include<stdio.h> 1. Draw the flowchart.
void main( )
2. What is the output of
{
the program if user key-
int number; in:
printf(“Enter any number :”); i. 5
scanf(“%d”,&number); ii. 22
if (number %2 == 0)
printf(“\nYou are entered even number!”);
}
21
EXERCISE
#include<stdio.h> 1. Draw the
void main( ) flowchart.
{ 2. What is the
int age; output of the
printf(“Welcome to Suruhanjaya Pilihan Raya Malaysia!”); program if user
printf(“\nEnter your age: ”; key-in:
i. 18
scanf(“%d”,&age);
ii. 21
if (age < 21)
iii. 20
printf(“\nSorry, you are under age to vote!”);
else
printf(“\nVote With Your Brain!”);
}
22
11
21-Aug-19
EXERCISE
#include<stdio.h>
void main( )
{
int age;
char destination; 1. Draw the flowchart.
printf(“\nWELCOME TO AIR ASIA HOLIDAY PACKAGE\n”);
printf(“\nEnter your age:”;
scanf(“%d”,&age);
2. What is the output of the
printf(“\nEnter your destination ‘B’ for Bandung, ‘J’ for Jakarta):"; program if user key-in:
scanf(“%d”,&destination);
if (destination == ‘B’) i. 2 , B
{
if (age < 2) ii. 1 , J
printf(“\nYou deserve to have 20% discount from usual rate!”; iii. 3 , B
else
printf(“\nYou should pay RM855!”;
}
else
{
if (destination == 'J')
{
if (age < 2)
printf(“\nYou deserve to have 20% discount from usual rate!”);
else
printf(“\nYou should pay RM925!”);
}
}
}
23
EXERCISE
#include<stdio.h>
void main( )
{
int n;
for ( n = 10 ; n > 0 ; n-- )
{
printf(“%d,”, n);
}
printf(“SURPRISE!\n”);
}
12
21-Aug-19
EXERCISE
#include<stdio.h> 1.Complete the flowchart.
void main( )
{
int n;
do
{
printf(“Enter any number, key-in 0 to exit : ”;
Yes
scanf(“%d”,&n);
printf(“You entered: %d.\n”,n);
No
}
while (n != 0); 2. Modify the program
} using while statement.
25
BREAK STATEMENT
• The keyword break allows jumping out of a loop
instantly, without waiting to get back to the
conditional test.
• When break is encountered inside any loop, control
automatically passes to the first statement after the
loop.
26
13
21-Aug-19
27
14
21-Aug-19
15
21-Aug-19
CONTINUE STATEMENT
• In some programming situations we want to take the
control to the beginning of the loop, bypassing the
statements inside the loop, which have not yet been
executed.
• When continue is encountered inside any loop,
control automatically passes to the beginning of the
loop.
31
32
16
21-Aug-19
33
17
21-Aug-19
GOTO STATEMENT
• The goto statement is rarely used because it makes
program confusing, less readable and complex.
• When this is used, the control of the program won’t
be easy to trace, hence it makes testing and
debugging difficult.
• When a goto statement is encountered in a C
program, the control jumps directly to the label
mentioned in the goto statement. 36
18
21-Aug-19
37
19