7 problem solving with loops
DESCRIPTION
TRANSCRIPT
Problem Solving with Loops *Property of STIPage 1 of 18
Data Structures and Algorithms
Problem Solvingwith Loops
The Loop Logic StructureA third logic structure for designing decisions is theloop structure. The loop logic structure is the repeatingstructure.
Types of Loop Structures
@ WHILE / WHILE-END@ REPEAT / UNTIL@ AUTOMATIC COUNTER LOOP
Incrementing
The task of incrementing, or counting, is done byadding a constant, such as 1 or 2, to the value of avariable. To write the instruction to increment avariable, you use an assignment statement. Forexample:
COUNTER = COUNTER + 1 or C = C+ 1
Problem Solving with Loops *Property of STIPage 2 of 18
Data Structures and Algorithms
Problem Solvingwith Loops
Accumulating
Another task that a program must often perform isaccumulating, or summing a group of numbers. Theprocess of accumulating is similar to incrementing,except a variable instead of a constant is added toanother variable, which holds the value of the sum ortotal. The instruction for accumulating is thefollowing :
SUM = SUM + VARIABLEPRODUCT = PRODUCT * NUMBER
Problem Solving with Loops *Property of STIPage 3 of 18
Data Structures and Algorithms
Problem Solvingwith Loops
WHILE / WHILE - END
The first of the three types of loop structures is theWHILE/WHILE-END structure. This type of loop tellsthe computer that while the condition is TRUE, repeatall instructions between the WHILE and the WHILE-END. The form of the algorithm is the following:
WHILE <CONDITION(S)>INSTRUCTIONINSTRUCTION...
WHILE-END
Problem Solving with Loops *Property of STIPage 4 of 18
Data Structures and Algorithms
Problem Solvingwith Loops
Flowchart Diagram of WHILE/WHILE-END
Problem Solving with Loops *Property of STIPage 5 of 18
Data Structures and Algorithms
Problem Solvingwith Loops
Decision Equivalent to WHILE/WHILE-END
Problem Solving with Loops *Property of STIPage 6 of 18
Data Structures and Algorithms
Problem Solvingwith Loops
Problem : Create the algorithm and the flowchart tofind the average age of all the students in a class.
Problem Solving with Loops *Property of STIPage 7 of 18
Data Structures and Algorithms
Problem Solvingwith Loops
Repeat/Until
The second type of loop structure is the REPEAT/UNTILstructure. This type of loop tells the computer torepeat the set of instructions between the REPEAT andthe until, until a condition is TRUE. The format of theREPEAT/UNTIL algorithm is the following:
RepeatInstructionInstruction...Until <condition(s)>
Problem Solving with Loops *Property of STIPage 8 of 18
Data Structures and Algorithms
Problem Solvingwith Loops
Flowchart Diagram of REPEAT/UNTIL
Problem Solving with Loops *Property of STIPage 9 of 18
Data Structures and Algorithms
Problem Solvingwith Loops
Decision Equivalent to REPEAT/UNTIL
Problem Solving with Loops *Property of STIPage 10 of 18
Data Structures and Algorithms
Problem Solvingwith Loops
Problem : Create the algorithm and the flowchart tofind the average age of all the students in a class.
Problem Solving with Loops *Property of STIPage 11 of 18
Data Structures and Algorithms
Problem Solvingwith Loops
Automatic – Counter Loop
The third type of loop structure is the automatic-counter loop. This type of loop increments ordecrements a variable each time the loop is repeated.The form of the algorithm for the automatic counter-loop is the following :
LOOP:COUNTER = BEGIN TO END STEP SINSTRUCTIONINSTRUCTION...LOOP-END: COUNTER
Problem Solving with Loops *Property of STIPage 12 of 18
Data Structures and Algorithms
Problem Solvingwith Loops
Decision Equivalent to Automatic-Counter Loop
Problem Solving with Loops *Property of STIPage 13 of 18
Data Structures and Algorithms
Problem Solvingwith Loops
Problem : Create the algorithm and the flowchart tofind the average age of all the students in a class.
Problem Solving with Loops *Property of STIPage 14 of 18
Data Structures and Algorithms
Problem Solvingwith Loops
Nested Loops
Loops can be nested like decisions can. Each loop mustbe nested inside the loop just outside it. The generalrules regarding loops, such as where the condition isprocessed and how indentation and brackets are used,hold true for nested loops as well as single loops.
The inner loops do not have to be the same types ofloop structures as the outer loops; that is, a WHILE/WHILE-END may be nested inside a REPEAT/UNTIL loop,or vice versa.
Problem Solving with Loops *Property of STIPage 15 of 18
Data Structures and Algorithms
Problem Solvingwith Loops
Nested Loops
Problem Solving with Loops *Property of STIPage 16 of 18
Data Structures and Algorithms
Problem Solvingwith Loops
Problem Solving with Loops *Property of STIPage 17 of 18
Data Structures and Algorithms
Problem Solvingwith Loops
Indicators
Indicators – are logical variables that a programmersets within a program to change the processing pathor to control when the processing of a loop should end.
They are sometimes called flags, switches, or tripvalues.
Recursion
Another type of loop structure is recursion. Recursionoccurs when a module or a function calls itself.
Problem Solving with Loops *Property of STIPage 18 of 18
Data Structures and Algorithms
Problem Solvingwith Loops