Download presentation
Presentation is loading. Please wait.
1
( Iteration / Repetition / Looping )
TOPIC 5 CONTROL STRUCTURE PART 2 ( Iteration / Repetition / Looping )
2
TOPICS TO BE COVERED Nested iteration Simple iteration While statement
3
At the end, student be able to :-
OBJECTIVE At the end, student be able to :- define the type of repetition control structure write pseudocode using various repetition control structures draw flow chart for different type of repetition control structure Trace the output based on algorithm
4
REPETITION Repetition Control Structure also known as Looping or Iteration.The control structure that directs the computer to repeat one or more instructions while certain condition remains true.
5
THE ADVANTAGES OF LOOPING:
When you use a loop within a computer program, you can write one set of instructions that operates on multiple, separate sets of data. A major advantage of having a computer perform complicated tasks is the ability to repeat them. THE ADVANTAGES OF LOOPING:
6
LOOPS IN PROGRAMMING pretest loop CSC118 Syllabus Loop statements
repeat … until while for do…while pretest loop CSC118 Syllabus pretest loop posttest loop posttest loop
7
PRETEST vs POSTTEST LOOPS
POSTTEST LOOP (action before condition) In each iteration, the loop action(s) is executed. Next, the loop control expression is tested. If it is true, a new iteration is started; otherwise, the loop terminates. PRETEST LOOP (condition before action) In each iteration, the loop control expression is tested first. If it is true, the loop action(s) is executed; if it is false, the loop is terminated. Action(s) True False True Action(s) False Posttest loop Pretest loop
8
REQUIREMENTS OF A REPETITION STRUCTURE
An Iteration/ Repetition structure requires: Loop Control Variable (LCV) Loop condition Loop body The loop body is controlled by 3 operations: Initialization of the Loop Control Variable (LCV) Example: set the variable to 0 Evaluation of LCV in the loop condition Eg: Variable < 10 Update of the LCV Eg: counter = counter + 1 A value of variable determines whether the loop body will be executed or not. It also identify the number of loops to be performed Example : x < 10 If the condition is true, the loop body is executed; otherwise the loop exits. Block of statement that had to be repeated.
9
It sets the stage for the loop actions.
AND In addition to the loop control expression, there are two other processes associated with almost all loop: initialization and updating. LOOP INITIALIZATION Before a loop can start, some preparation is usually required. Such preparation is called loop initialization. Initialization must be done before the first execution of the body. It sets the stage for the loop actions. LOOP UPDATE How can the condition that controls the loop be true for a while and then change to false…? The answer is that something must happen inside the body of the loop to change the condition. Otherwise, we would have an infinite loop.This changes the resulting condition from true to false (are known as loop update). Updating is done in each iteration, usually as the last action. If the body of the loop is repeated m times, then the updating is also done m times. When you write a loop, you must control the number of repetition it performs. If you don’t, you run the risk of creating an infinite loop. Commonly, you control a loop’s repetitions by using either a counter or a sentinel value.
10
… AND Initialization Initialization Action(s) Updating Action(s)
Figure 3: Pretest loop Figure 4: Posttest loop 10
11
LOOPING STRUCTURE
12
REPETITIONS BY USING EITHER A COUNTER OR A SENTINEL VALUE
MUST CONTROL A LOOP’S REPETITIONS BY USING EITHER A COUNTER OR A SENTINEL VALUE
13
COUNTER- CONTROLLED LOOP
Used when the number of time a segment code needs to be repeated is known in advance. Example: Repeat a process for 10 times
14
SENTINEL-CONTROLLED LOOP
Used when the number of repetition depends on a certain condition. It uses a special end-of-data value called sentinel value that serves as a signal for loop termination. Also called event-controlled loops.The number of repetition is not known. For example :Write a program display ‘Incorrect pin number, please try again’ as long as the code entered is not 877.
16
EXAMPLE 1 : A WHILE LOOP THAT PRINTS “HELLO” FOUR TIMES COUNTER
Start Start count = 0 WHILE count < 4 print “Hello” count = count + 1 ENDWHILE num count = 0 End Yes count < 4? Print “Hello” No End count = count + 1
17
EXAMPLE 2 : COMPUTE THE SUM OF 3 NUMBERS
18
TRY THIS OUT Write a pseudocode (from the given flowchart) to find the sum of all the even numbers from 2 to 20.
19
EXERCISE 2 TRY THIS OUT Given the program segment below:
set n = 1; while (n <= 5) n = n + 1; cout << n << “ ”; Write the statement/expression that corresponds to the loop operation. Answer: Initialization: Evaluation : Update :
20
EXERCISE 3 TRY THIS OUT - TRACING TRACING 1: i = 0 WHILE (i <= 20)
Print i i = i + 5 ENDWHILE Output: TRACING 2: i = 0 WHILE (i <= 20) i = i + 5 Print i ENDWHILE Output: 20
21
Print “Do you want to see next year’s balance?
EXAMPLE 3 : LOOPING BANK BALANCE PROGRAM SENTINEL Get response WHILE (response == ‘Y’) Print bankBal bankBal = bankBal + bankBal *intRate Print “Do you want to see next year’s balance? Y or No…” ENDWHILE Print “Have a nice day” Get response response == ‘Y’? Yes Print bankBal No bankBal = bankBal + bankBal * intRate Print “Have a nice day” Print “Do you want to see next year’s balance? Y or N…” End Get response
22
LOOP
23
INTRODUCTORY TO NESTED LOOP
Program logic gets more complicated when you must use loop within loops, or nested loop. When one loop appears inside another, the loop that contains the other loop is called the outer loop, and the loop that is contained is called the inner loop. You need to create nested loops when the values of two (or more) variables repeat to produce every combination of values. For example: Suppose you want to write a program that produces a quiz answer sheet like the one shown in Figure 11. The quiz has five parts with three questions in each part, and you want a fill-in-the-blank line for each question. You could write a program that uses 21 separate print statements to produce the sheet, but it is more efficient to use nested loop.
24
NESTED LOOP EXAMPLE START SET PARTS = 5 SET QUESTIONS = 3
SET PART_LABEL = “Part “ SET LINE = “. ” SET partCounter = 1 WHILE partCounter <= PARTS PRINT PART_LABEL, partCounter questionCounter = 1 WHILE questionCounter < = QUESTIONS PRINT questionCounter, LINE questionCounter = questionCounter + 1 ENDWHILE partCounter = partCounter + 1 ENDWHILE END 1. 2. 3. Part 2 1. 2. 3. Part 3 1. 2. 3. Part 4 1. 2. 3. Part 5 1. 2. 3. A quiz sheet answer Pseudocode for AnswerSheet program
25
EXERCISE 4 NESTED LOOP The number of times a loop executes can depend on a constant or a value that varies. Suppose you own a factory and have decided to place a label on every product you manufacture. The label contains the words “Made for you personally by” followed by the first name of one of your employees. Assume that for one week’s production, you need 100 personalized labels for each employee. (Note: the user is prompted for an employee’s name, while the user does not type the QUIT value (“ZZZ”), the program continues). Try to solve it by your own self…
26
EXERCISE 5 NESTED LOOP Sometimes, you don’t want to be forced to repeat every pass through a loop the same number of times… For example, instead of printing 100 labels for each employee, you might want to vary the number of labels based on how many items a worker actually produces. That way, high-achieving workers won’t run out of labels, and less productive workers won’t have too many. Instead of printing the same, constant number of labels for every employee, a more sophisticated program prints a different number of labels for each employee, depending on that employee’s usual production level. Try to solve it by your own self… 26
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.