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

07 Looping

Uploaded by

dumpskie1
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

07 Looping

Uploaded by

dumpskie1
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 34

Ice Breaker

1. If you could repeat one year of your life in


an endless loop, which year would it be and
why?

2. If you were stuck in a time loop for a day,


what’s the one activity you wouldn’t mind
repeating?
Looping
 Identify what iteration
structures are
 Explain the different types of
iteration structures
Topics  Describe the iteration control in
programming
 Create a program that will
demonstrate iteration control
Control Structures - Iteration
• Most useful and powerful structure
• Allows the repetition of instructions or statements
in the loop body
Control Structures - Iteration

• Parts of the iteration structure


— Loop body
instruction(s) or statements which are repeated
— Loop-exit condition
the condition to be tested before each repetition

• Types
— while loop
— do-while loop
— for loop
Control Structures - Iteration

Format: while loop

while ( condition ) {
statement-1
statement-2
...
}

- The statements inside the while loop are executed as long as


the condition remains true
Control Structures - Iteration

Example 1:
int x = 0;
while (x < 10) {
printf(“%d”, x);
x++;
}
Control Structures - Iteration

• Loop control variable


 Used to control the loop’s execution
 The loop control variable is declared and initialized
outside the loop
 The loop control variable is tested and if the result is
true, the loop body is entered
 Inside the loop, the value of the loop control
variable must be changed
 The loop control variable must reach a value that
will render the loop-exit condition false
Control Structures - Iteration
Example 2:
Control Structures - Iteration
• Example 3:
Control Structures - Iteration

• Infinite loops
 An infinite loop results when the loop-exit
condition never becomes false.
 To control the execution of a loop, the following can
be used as loop control variables:
• Counters
• Sentinel value or Indicators

A sentinel value signals a stop in the loop


Control Structures - Iteration
• Example 4: Using a counter
Control Structures - Iteration
• Example 5: Using a sentinel value
Control Structures - Iteration

• Format: do-while loop


 Is similar to the while-loop
 Statements inside a do-while loop are executed
several times as long as the condition is satisfied
 The main difference between a while and a do-
while loop:
• The statements inside a do-while loop are

executed at least once


Control Structures - Iteration

do-while loop has the form:

do {
statement-1
statement-1
...
} while (boolean
expression);
Control Structures - Iteration

Example:

int x = 0;
do {
printf(“%d”, x);
x++;
} while (x < 10);
Control Structures - Iteration

for loop
 Allows execution of the same code a number of times
 Format:
for (InitializationExpression; LoopCondition;
StepExpression){
statement-1;
statement-2;
...
}
Control Structures - Iteration

• for loop
 Initialization expression
• Declaration of loop variable and setting of its
initial value
• Example: count = 0
 Loop condition
• Condition to be tested before repetition
• Example: count <= 5
Control Structures - Iteration

• for loop
 step expression
• An expression that would alter the value of the
loop control variable
• Example: count = count + 1
Control Structures - Iteration

• for loop
 After the loop control variable is increased, the
loop condition is tested again
 If the loop condition results in false, the for loop is
exited.
Control Structures - Iteration

Example: for loop


int i;
for (i = 0; i < 10; i++) {
printf(“%d”, i);
}
The code above is equivalent to the following while loop
int i = 0;
while (i < 10) { printf(“%d”,
i);
i++;
}
Control Structures - Iteration

• When to use the while loop and the for loop


 The for loop is more compact than the while loop.
There is no need to write statements to alter the
loop control variable, thus reducing errors that
might result when these statements are missed out.
 When the number of times that the loop will be
executed is known, the for loop provides a
convenient shorthand.
Control Structures - Iteration

• When to use the while loop and the for loop


 Loops that are dependent on a sentinel value (or
indicator) are better coded using a while loop

 The for loop is generally used for traversing and


manipulating arrays
Control Structures - Iteration

• Common Loop Applications


 Using a loop to accumulate totals
• An accumulator is a variable that “sums up” or
accumulates values
• It is similar to a counter whose values change
each time the loop is entered. Counters,
however, add a fixed value while accumulators
accumulate undetermined values.
Control Structures - Iteration

• Common Loop Applications


 Using a loop to validate user entry
• Data entered by a user usually needs validation. It
needs to be checked for errors. Incorrect data can
lead to unwanted results and could end a program
abnormally
• Usual checks for data are:
 If it is the correct data type
 For numeric data, if it is within an acceptable
range of values
Control Structures – JUMP
STATEMENTS
• Transfer control to another part of the program
• 3 jump statements
 break
 continue
 return
Control Structures – JUMP
STATEMENTS
break Statement
 force immediate termination of a loop, bypassing the
conditional expression and any remaining code in the body of
the loop
 The loop is terminated and program control resumes at
the next statement following the loop
Control Structures – JUMP
STATEMENTS
break Statement – Example
Control Structures – JUMP
STATEMENTS
continue Statement
 In while and do-while loops, a continue statement cause control
to be transferred directly to the conditional expression that
controls the loop
 In a for loop, control goes first to the iteration portion of the for
statement and then to the conditional expression
 continue statement may specify a label to describe which
enclosing loop to continue
Control Structures – JUMP
STATEMENTS
continue Statement – Example
Control Structures – JUMP
STATEMENTS
return Statement
 Use to explicit return from a method.
 Causes program control to transfer back to the caller of the
method.
 Can be used at any time to cause execution to branch back to the
caller of the method.
Control Structures – JUMP
STATEMENTS
return Statement – Example
Exercise

Using a loop to validate user entry


 Write a program that would ask a user for his or her birth month.
The user enters a value from 1 to 12, corresponding to the 12
months of the year.
 Using a loop, validate the data entered. If the data is incorrect,
continue to prompt the user for an entry until the data entered is
valid. If the data is valid, display the following:
 - Birth month is value entered
?
THANK YOU

You might also like