Loop logic structure
CSC1101 PLF
Lesson 7: PROBLEM SOLVING WITH LOOP LOGIC STRUCTURE Objectives: Develop problem solutions for the computer using the seven program development tools, the sequential logic structure, the decision logic structure, and the loop logic structure. Use the three types of loop instructions in problem solutions. Use nested loops in problem solutions. Several standard types of tasks are accomplished through the use of the loop structure. Two of these are counting (also called incrementing and decrementing), and accumulating (also called calculating a sum or a total).
For counting purpose, some languages use base-zero system where the first element of the array will start from zero not one. The programmer needs to add 1 to the system so that the first element in the array will start from one for easy reference. Some languages use base-one system and it is easier for the programmer to understand since the first element is the first box, second element is the second box, and so on. The counter The counter is used to count the loop repetitions and it is known as incrementing. It is used to count the number of items, people, temperatures and so on. To write the instruction to increment a variable, an assignment statement needs to be used. Initialize the counter before starting the loop Plus sign (+) used to increment the counter Remark: Counter is a variable.
Syntax: Counter = 0 Counter = Counter + 1
Prepared By Kavitha.S
Page 1 of 8
Loop logic structure
CSC1101 PLF
Negative numbers used to decrement the counter: Syntax: Counter = 5 Counter = Counter – 1
The accumulator The accumulator used to accumulate or sum up the values. Syntax: Totalsales = 0 Totalsales = Totalsales + Sales { Sales is the input variable} Example Totalsales 100 300 600
Sales 100 200 300
The Loop Logic Structure This is the repeating structure. There are two types of control loop: o Counter-Controlled loop Use when the number of times of repeating certain statement(s) is known. The number of times of looping is fixing by the programmer in the coding. It is programmer-defined. The loop is control by a counter statement in the coding. The types of looping logic structures used: While/while-end loop Repeat-until loop Automatic Counter loop
Prepared By Kavitha.S
Page 2 of 8
Loop logic structure
CSC1101 PLF
o Flag/sentinel controlled loop When the number of times of looping is unknown by the programmer, where it is defined by the . The looping is control through a sentinel/flag value enter by the . The types of loop logic structures use are: While/while-end loop Repeat-until loop Three types of loop logic structures: 1. WHILE/WHILE-END loop - repeats the instructions while a condition is TRUE, and stop repeating when it is FALSE. 2. REPEAT/UNTIL loop - repeat instructions while condition is FALSE or until a condition is TRUE. 3. Automatic-counter loop - repeat instructions in a fixed number of times that based on a given number.
While/While-End Loop Repeats instruction while a condition is TRUE and stops repeating when a condition is FALSE. Syntax: While
While-End Trip value / Flag used to control the loop or terminate the loop repetition
Prepared By Kavitha.S
Page 3 of 8
Loop logic structure
CSC1101 PLF
FLOWCHART ILLUSTRATES THE WHILE LOOP LOGIC STRUCTURE
Begin
F
While
T Instruction
Instruction
Repeat/ Until A set statements
of will
End
Loop
instructions between the repeat-until be repeated until the condition is TRUE.
Syntax: Repeat
< Instruction> Until
Prepared By Kavitha.S
Page 4 of 8
Loop logic structure
CSC1101 PLF
Questions: Use the above questions to develop the solutions by using REPEAT/UNTIL method. FLOWCHART ILLUSTRATES THE REPEAT/UNTIL LOOP LOGIC STRUCTURE Begin
Repeat Instruction
Instruction
F Until
T End
Automatic Counter Loop
Prepared By Kavitha.S
Page 5 of 8
Loop logic structure
CSC1101 PLF
Increments or decrements a variable each time the loop is repeated. To design this, the programmer uses a variable as a counter that starts counting at a specific number and increments the variable each time the loop is processed. Syntax: (incremental method) Loop: Counter = Begin to End Steps
Syntax: (decremental method) Loop: Counter = Begin downto End Steps
Loop-end: Counter
Loop-end: Counter
Example: Loop : c = 1 to 3 step 1 Display c End-loop: c
Loop : c = 3 downto 1 step 1 Display c End-loop: c
When incrementing the counter, the following rules: 1. When the computer executes the LOOP instruction, it sets the counter equal to the beginning number. 2. When the computer executes the LOOP-END, it increments the counter. 3. When the counter is less than or equal to the ending number, the processing continues at the instruction that follows the LOOP instruction. When the counter is greater than the ending number, the processing continues at the instruction that follows the LOOP-END instruction.
Precondition loop Referred as input assertions. They are comments that indicate what can be expected to be TRUE before the loop is Prepared By Kavitha.S
Page 6 of 8
Loop logic structure
CSC1101 PLF
entered Example, WHILE/WHILE-END E.g. Num = 56 While num > 0 Display num Enter num While-end Postcondition Loop Referred as output assertions They are comments that indicate what can be expected to be TRUE when the loop is exited. Example: REPEAT/UNTIL E.g. REPEAT ENTER num DISPLAY num UNTIL num < 0 Nested Loops Each loop must be nested inside the loop just outside it. Example: Loop: x = 1 to 4 step 1 Loop: y = 1 to 3 step 1 Print ‘*’ End-loop: y Print w/o End-loop: x
Prepared By Kavitha.S
Page 7 of 8
CSC1101 PLF
Loop logic structure
Indicators (trip value)
Indicators are logical variables that a programmer sets within a program to change the processing path or to control when the processing of a loop should end. They are sometimes called flag, switches or trip values. For example, an indicator for AGE might be 0 or 500 or –300. An indicator for an error could be a variable with the value TRUE when an error has occurred, and FALSE when there have been no errors.
Prepared By Kavitha.S
Page 8 of 8