Upcoming SlideShare
×

# 7 problem solving with loops

14,925 views

Published on

Published in: Technology, Education
5 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

Views
Total views
14,925
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
292
0
Likes
5
Embeds 0
No embeds

No notes for slide

### 7 problem solving with loops

1. 1. Data Structures and Algorithms Problem Solving with LoopsThe 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 LOOPIncrementingThe 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+ 1Problem Solving with Loops *Property of STI Page 1 of 18
2. 2. Data Structures and Algorithms Problem Solving with LoopsAccumulatingAnother 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 + VARIABLE PRODUCT = PRODUCT * NUMBERProblem Solving with Loops *Property of STI Page 2 of 18
3. 3. Data Structures and Algorithms Problem Solving with LoopsWHILE / WHILE - ENDThe 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)> INSTRUCTION INSTRUCTION . . . WHILE-ENDProblem Solving with Loops *Property of STI Page 3 of 18
4. 4. Data Structures and Algorithms Problem Solving with LoopsFlowchart Diagram of WHILE/WHILE-ENDProblem Solving with Loops *Property of STI Page 4 of 18
5. 5. Data Structures and Algorithms Problem Solving with LoopsDecision Equivalent to WHILE/WHILE-ENDProblem Solving with Loops *Property of STI Page 5 of 18
6. 6. Data Structures and Algorithms Problem Solving with LoopsProblem : Create the algorithm and the flowchart tofind the average age of all the students in a class.Problem Solving with Loops *Property of STI Page 6 of 18
7. 7. Data Structures and Algorithms Problem Solving with LoopsRepeat/UntilThe 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: Repeat Instruction Instruction . . . Until <condition(s)>Problem Solving with Loops *Property of STI Page 7 of 18
8. 8. Data Structures and Algorithms Problem Solving with LoopsFlowchart Diagram of REPEAT/UNTILProblem Solving with Loops *Property of STI Page 8 of 18
9. 9. Data Structures and Algorithms Problem Solving with LoopsDecision Equivalent to REPEAT/UNTILProblem Solving with Loops *Property of STI Page 9 of 18
10. 10. Data Structures and Algorithms Problem Solving with LoopsProblem : Create the algorithm and the flowchart tofind the average age of all the students in a class.Problem Solving with Loops *Property of STI Page 10 of 18
11. 11. Data Structures and Algorithms Problem Solving with LoopsAutomatic – Counter LoopThe 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 S INSTRUCTION INSTRUCTION . . . LOOP-END: COUNTERProblem Solving with Loops *Property of STI Page 11 of 18
12. 12. Data Structures and Algorithms Problem Solving with LoopsDecision Equivalent to Automatic-Counter LoopProblem Solving with Loops *Property of STI Page 12 of 18
13. 13. Data Structures and Algorithms Problem Solving with LoopsProblem : Create the algorithm and the flowchart tofind the average age of all the students in a class.Problem Solving with Loops *Property of STI Page 13 of 18
14. 14. Data Structures and Algorithms Problem Solving with LoopsNested LoopsLoops 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 STI Page 14 of 18
15. 15. Data Structures and Algorithms Problem Solving with LoopsNested LoopsProblem Solving with Loops *Property of STI Page 15 of 18
16. 16. Data Structures and Algorithms Problem Solving with LoopsProblem Solving with Loops *Property of STI Page 16 of 18
17. 17. Data Structures and Algorithms Problem Solving with LoopsIndicatorsIndicators – 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.RecursionAnother type of loop structure is recursion. Recursionoccurs when a module or a function calls itself.Problem Solving with Loops *Property of STI Page 17 of 18
18. 18. Data Structures and Algorithms Problem Solving with LoopsProblem Solving with Loops *Property of STI Page 18 of 18