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

Topic 4 - Looping Statements Student

The document discusses looping statements in programming. It defines for, while, and do-while loops and provides their structures and examples. Key differences between the loops are explained.

Uploaded by

haikalnizan1007
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Topic 4 - Looping Statements Student

The document discusses looping statements in programming. It defines for, while, and do-while loops and provides their structures and examples. Key differences between the loops are explained.

Uploaded by

haikalnizan1007
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

21-Aug-19

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

DEFINITION OF LOOPING STATEMENTS


• The versatility of the computer lies in its ability to
perform a set of instructions repeatedly. This
involves repeating some portion of the program
either a specified number of times or until a
particular condition is being satisfied.
• This repetitive operation is done through a loop
control instruction.
3

TYPES OF LOOPING STATEMENTS


• There are three types or methods by way of which
we can repeat a part of a program. They are:
• Using a for statement
• Using a while statement
• Using a do-while statement

2
21-Aug-19

DEFINITION OF FOR STATEMENT


• The for loop repeats the body of the loop as long as
the loop condition holds. The basic form of the for
loop as below:
for (initialization_expression; loop_condition; incrementation_expression)
{
statement A;
}

STRUCTURE OF FOR STATEMENT


for (initialization_expression; loop_condition; incrementation_expression)
{
statement A;
}

• initialization_expression is executed before


execution of the loop starts. The
initialization_expression is typically used to
initialize a counter for loop. 6

3
21-Aug-19

STRUCTURE OF FOR STATEMENT


for (initialization_expression; loop_condition; incrementation_expression)
{
statement A;
}

• The execution of the loop continues until the


loop_condition is false. This expression is checked at
the beginning of each loop iteration.
7

STRUCTURE OF FOR STATEMENT


for (initialization_expression; loop_condition; incrementation_expression)
{
statement A;
}

• The increment_expression is usually used to


increase (or decrease) the loop counter. This part is
executed at the end of each loop iteration.
8

4
21-Aug-19

EXAMPLE OF FOR STATEMENT


#include<stdio.h> 1. counter = 1.
main( )
2. Test if counter <= 10
{
if true  statement 1
int counter, x = 1;
for (counter = 1; counter <= 10; counter++) and statement 2 is
{ executed.
x = x * x; //statement 1 if false  terminated
printf(“\nLoop %d, x = %d.”, counter, x); //statement 2
loop.
}
} 3. increment counter.

EXAMPLE OF FOR STATEMENT


#include<stdio.h>
void main( )
counter = 1
{
int counter, x = 1;
counter <= 10 No
for (counter = 1; counter <= 10; counter++)
{ Yes
x = x * x; x=x*x
printf(“\nLoop %d, x = %d.”, counter, x);
Display number of
}
loop and value of x.
}
counter = counter + 1

10

5
21-Aug-19

DEFINITION OF WHILE STATEMENT


• The while loop repeats the body of the loop as long
as the loop condition holds. The basic form of the
while statement is as below:
while (expression)
{
statement;
}

11

STRUCTURE OF WHILE STATEMENT


while (expression)
{
statement;
}

• expression is the condition that hold the loop.


• statement is the instruction that will be repeated as
long as condition holds.
12

6
21-Aug-19

STRUCTURE OF WHILE STATEMENT


while (expression)
{
statement;
}

• In this loop, the expression is first evaluated. If it is


true (not zero), the statement which can be a block of
statements is executed; if it is false (zero), the
statement is bypassed. 13

EXAMPLE OF WHILE STATEMENT


#include<stdio.h>
void main( )
Read counter
{
int counter;
No
printf(“Enter the starting number:”); counter > 0
scanf(“%d”, &counter);
Yes
while (counter > 0)
Print counter
{
printf(“%d, ”,counter); counter = counter - 1
--counter;
}
Print FIRE! FIRE!
printf(“\nFIRE! FIRE!“);
}

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

Display number of counter = counter - 1


loop and value of x.

counter = counter + 1 Print FIRE! FIRE!

15

CHANGE WHILE TO FOR


#include<stdio.h> Answer:
void main( )
{
int counter;
printf(“Enter the starting number:”);
scanf(“%d”, & counter);
while (counter > 0)
{
printf(“%d, ”, counter);
--counter;
}
printf(“\nFIRE! FIRE!“);
}

16

8
21-Aug-19

DEFINITION OF DO-WHILE STATEMENT


• The do-while loop repeats the body of the loop as
long as the loop condition holds. The basic form of
the while statement is as below:
do
{
statement;
}
while (expression)
17

STRUCTURE OF DO-WHILE STATEMENT


do
{
statement;
}
while (expression)
• This do-while loop is quite similar to the while loop except
that the expression is evaluated after the statement is
executed. This means the statement in the do-while will be
executed at least once. In the while statement, the statement
will not be executed if the expression is false. 18

9
21-Aug-19

EXAMPLE OF DO-WHILE STATEMENT


#include<stdio.h>
void main( )
{ Read counter
int counter;
Print counter
printf(“Enter the starting number:”);
scanf(“%d”, & counter);
counter = counter - 1
do
{
printf(“%d, ”, counter); counter > 0 Yes
--counter;
} No
while (counter > 0) Print FIRE! FIRE!

printf(“\nFIRE! FIRE!“);
}
19

DIFFERENCE
WHILE DO-WHILE
Read counter Read counter

No Print counter
counter > 0

counter = counter - 1
Yes
Print counter

counter > 0 Yes


counter = counter - 1

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”);
}

Question : Get the output.


24

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

FLOW CHART FOR BREAK

27

EXAMPLE OF BREAK IN FOR LOOP


#include<stdio.h> Trace the output.
void main( )
{
int i;
for( i = 10 ; i > 0 ; i-- )
{
if( i == 6 )
{
printf("\n Coming out from for loop Where i = %d\n", i);
break;
}
printf("%d", i);
}
}
28

14
21-Aug-19

EXAMPLE OF BREAK IN WHILE LOOP


#include<stdio.h> Trace the output.
void main( )
{
int i =0;
while(i<=10)
{
printf("\n The Value of the Variable = %d \n", i);
i++;
if (i==4)
{
break;
}
}
printf("\n This statement is from Outside the while Loop ");
}
29

EXAMPLE OF BREAK WHILE LOOP


#include<stdio.h>
void main( ) //This program for determine the number that user key in is prime number or not.
{
int num, i = 2;
Trace the output when user key in 2,3,4,5.
printf ( "Enter a number " ) ;
scanf ( "%d", &num ) ;
while ( i <= num - 1 )
{
if ( num % i == 0 )
{
printf ( "Not a prime number" ) ;
break ;
}
i++ ;
}
if ( i == num )
printf ( "Prime number" ) ;
}
30

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

FLOW CHART FOR CONTINUE

32

16
21-Aug-19

EXAMPLE OF CONTINUE IN FOR LOOP


#include<stdio.h> Trace the output.
void main( )
{
for (int j = 0; j <= 8; j++)
{
if (j == 4)
{
continue;
}
printf("%d ", j);
}
}

33

EXAMPLE OF CONTINUE IN WHILE LOOP


#include<stdio.h> When using while or do-while loop you need to place an
void main( ) increment or decrement statement just above the
{ continue so that the counter value is changed for the next
int counter = 10; iteration.
while (counter >= 0) For example, if we do not place counter-- statement in the
{ body of “if” then the value of counter would remain 7
if (counter == 7) forever.
{ Trace the output.
counter--;
continue;
}
printf("%d ", counter);
counter--;
}
}
34

17
21-Aug-19

EXAMPLE OF CONTINUE IN DO-WHILE LOOP


#include<stdio.h> When using while or do-while loop you need to place an
void main( ) increment or decrement statement just above the
{ continue so that the counter value is changed for the next
int j = 0; iteration.
do For example, if we do not place j++ statement in the body
{ of “if” then the value of counter would remain 7 forever.
if (j == 7) Trace the output.
{
j++;
continue;
}
printf("%d ", j);
j++;
}while(j < 10);
}
35

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

FLOW CHART FOR GOTO

37

EXAMPLE OF GOTO IN FOR LOOP


#include<stdio.h> When the value of i (inside loop) is equal to
void main( ) 5, the control will jump to addition label
{ using goto.
int sum=0; Trace the output.
for(int i = 0; i<=10; i++)
{
sum = sum+i;
if(i==5)
{
goto addition;
}
}
addition:
printf("%d", sum);
}
38

19

You might also like