You are on page 1of 25

COMPUTER SCIENCE

12
(MS Access and C)

CHAPTER 12: Loop Constructs

Copyright @ IT Series www.itseries.com.pk


Topics
• Loop
• while Loop
• Infinite Loop
• do-while Loop
• Difference between while and do-while Loop
• for Loop
• Sentinel Controlled Loop
• continue Statement
• break Statement
• Nested Loops
• goto Statement

Copyright @ IT Series www.itseries.com.pk


 The control structure that executes a statement or set of statements repeatedly is called loop.
 Loops are also known as iteration or repetition structure
 Loops are basically used for two purposes:
 They are used to execute a statement or number of statements for a specified number of times
 Example: Apossible
user may
to display
performhis name on screen
mathematical for 10ontimes
operation character values
 The loops are also used to get a sequence of values
 Example: A user may display a set of natural numbers from 1 to 10
 There are three types of loops available in C:
 while loop
 do-while loop
 for loop

Copyright @ IT Series www.itseries.com.pk


while is a
keyword. It is
 while loop is the simplest loop of C always written in
lowercase.
 It executes one or more statements while the given condition remains true
 It is useful when the number of iterations is not known in advance An iteration is
an execution of
the loop body.
Syntax
Don’t put ; after (condition) Flowchart

possible to perform mathematical operation on character values


Loop body

while is a pretest
loop (the condition
is evaluated before
the loop executes)
 Condition is evaluated:
 if it is true, the statement(s) are executed, and then condition is evaluated again
 if it is false, the loop is exited

Copyright @ IT Series www.itseries.com.pk


while Loop Example
int n;
n = 1; Don’t put ; after (condition)
while(n<=5)
{
Loop body
printf(“Pakistan\n"); while is a pretest
loop (the condition
n++; is evaluated before
the loop executes)
}

possible to perform mathematical operation on character values


n++; is the same as n= n + 1;

 Produces output:
Counter Variable
 A variable that is incremented or decremented each time a loop iterates
 It can be used to control the execution of the loop (as a loop control variable)
 It must be initialized before entering loop
 It may be incremented/decremented either inside the loop or in the loop test

Copyright @ IT Series www.itseries.com.pk


while Loop Programs

possible to perform mathematical operation on character values

Copyright @ IT Series www.itseries.com.pk


while Loop Programs (cont.)

possible to perform mathematical operation on character values

Copyright @ IT Series www.itseries.com.pk


Infinite loop
 The loop must contain code to allow the condition to eventually become false so the loop can be exited
 Otherwise, you have an infinite loop

A loop that does not


Example stop is known as an
infinite loop

int n; possible to perform mathematical operation on character values


n = 1;
Infinite loop because n is always < 10
while(n<10)
printf("%d \t",n);

Copyright @ IT Series www.itseries.com.pk


 This loop executes one or more statements while the given condition is true
 In this loop, the condition comes after the body of loop
 The loop body always executes at least once
Syntax:

possible to perform mathematical operation


do-while is aon character values
post
Loop body test loop (condition
is evaluated after
the loop executes)
An error occurs if the semicolon
is not used at the end of the
condition.

 Execution continues as long as the condition is true; the loop is exited when the condition becomes false
 ; after (condition) is also required
 The loop executes at least once even if the condition is false in the beginning

Copyright @ IT Series www.itseries.com.pk


do-while Loop Example
int n; do-while is a post
test loop (condition
n = 1; is evaluated after
do the loop executes)
{
printf(“Welcome to C\n”);
n++; Loop body
}
while(n<=5); possible toCondition
perform mathematical operation on character values

Copyright @ IT Series www.itseries.com.pk


do-while Loop Programs

possible to perform mathematical operation on character values

Copyright @ IT Series www.itseries.com.pk


do-while Loop Programs (cont.)

possible to perform mathematical operation on character values

Copyright @ IT Series www.itseries.com.pk


possible to perform mathematical operation on character values

Copyright @ IT Series www.itseries.com.pk


 It is a pretest loop that executes one or more statements for a specified number of times
 This loop is also called counter-controlled loop
 It is useful with counters or if precise number of iterations is known
Syntax / General Form Example
for (initialization; condition; increment/decrement)
{
statement 1; Flowchart
possible to perform mathematical operation on character values
statement 2;


statement N;
}

Copyright @ IT Series www.itseries.com.pk


 You can define variables in initialization code
for (int n=1; n <= 5; n++)
 Can omit initialization if already done
int n = 1;
for (; n <= 5; n++)

 Can omit update if done in loop body


possible to perform mathematical operation on character values
for (n = 1; n<= 5;)
n++;

 The condition in for loop is mandatory. It cannot be omitted


int n=1;
for (;n<= 5;)
n++;

Copyright @ IT Series www.itseries.com.pk


for Loop Programs

possible to perform mathematical operation on character values

Copyright @ IT Series www.itseries.com.pk


for Loop Programs (cont.)

possible to perform mathematical operation on character values

Copyright @ IT Series www.itseries.com.pk


Sentinel Controlled Loop
• Sentinel-controlled loop depends on special value known as sentinel value
• A sentinel value is commonly used with while and do-while loops
• The number of iterations of sentinel-controlled loop is unknown
• Depends on the input from the user
General Form
• Start the loop iteration
• Compare the data value with the
possible to sentinel
perform value
mathematical operation on character values
• Process the loop body if it is different from sentinel value
• Terminate the loop if the data value is a sentinel value
Examples
• A loop that inputs the marks of students until a negative number is entered
• A loop that inputs different characters until the character 'Z' is entered
• A loop that continues to read a string until a period "." is found
• A loop that inputs a number until a 0(zero) is entered

Copyright @ IT Series www.itseries.com.pk


possible to perform mathematical operation on character values

Copyright @ IT Series www.itseries.com.pk


 The continue statement is used in the body of the loop
 When this statement is executed in the loop body, the remaining statements of current iteration are not
executed
 The control directly moves to the next iteration
Example

The inner loop goes through all its iterations for each iteration of the outer loop

 The above example has two printf statements


 One statement is before the continue statement and one is after continue statement
 The second statement is never executed
 This is because each time continue statement is executed, the control moves back to the start of the body
 So “Knowledge is power” is never displayed

Copyright @ IT Series www.itseries.com.pk


 It can be used with while, for, do-while or switch structure
 When this statement is executed in the loop body, the remaining iterations of the loop are skipped
 The control directly moves outside the body and the statement that comes after the body is executed
 When used in an inner loop, break terminates that loop only and returns to the outer loop
Example

break can
The inner loop goes through all its iterations for each iteration of be
theused to loop
outer
terminate the
execution of a loop
iteration

 The counter variable x indicates that the loop should execute for five times
 But it is executed only once
 In first iteration, the printf statement in the loop displays “Questioning” and control moves to break
statement
 This statement moves the control out of the loop. So the message appears only once

Copyright @ IT Series www.itseries.com.pk


 A loop within a loop is called nested loop
 Nested loops consist of an outer loop with or more inner loops
 Any loop can be used as inner loop of another loop
 e.g. while loop can be used as outer loop and for loop can be used as inner loop in nested loop
Syntax / General Form

The inner loop goes through all its iterations for each iteration of the outer loop

 The inner loop goes through all its iterations for each iteration of the outer loop
 Total number of iterations for inner loop is product of number of iterations of the two loops
 In example, Outer loop executes two times and inner loop executes three times with each iteration of outer
loop
 The inner loop iterates 6 times in total

Copyright @ IT Series www.itseries.com.pk


Nested Loop Programs

possible to perform mathematical operation on character values

Copyright @ IT Series www.itseries.com.pk


Nested Loop Programs(cont.)

possible to perform mathematical operation on character values

Copyright @ IT Series www.itseries.com.pk


 Used to perform an unconditional transfer of control to a named label
General Form
goto label; Example
----------------
-----------------
label:
Statement;
The inner loop goes through all its iterations for each iteration of the outer loop
 The label is an identifier.
 When the goto statement is encountered, the program control
jumps to label and starts executing the statements
 if and goto can be used together to construct loop in C
 If statement is used to give specific condition
 goto statement is used to transfer control from one location to
another
 Control is transferred again and again until given condition become false
 In this way , loop can be constructed

Copyright @ IT Series www.itseries.com.pk

You might also like