Like this document? Why not share!

- 6 problem solving with decisions by Rheigh Henley Cal... 4841 views
- 5 problem solving with the sequenti... by Rheigh Henley Cal... 6445 views
- Problem Solving Flowchart!!! by Rahul Tiwari 92408 views
- Algorithms and Flowcharts by Deva Singh 36588 views
- Writing algorithms by Krishna Chaytaniah 25166 views
- 1 introduction to problem solving a... by Rheigh Henley Cal... 6801 views

8,756

Published on

No Downloads

Total Views

8,756

On Slideshare

0

From Embeds

0

Number of Embeds

0

Shares

0

Downloads

213

Comments

0

Likes

4

No embeds

No notes for slide

- 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. 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. 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. Data Structures and Algorithms Problem Solving with LoopsFlowchart Diagram of WHILE/WHILE-ENDProblem Solving with Loops *Property of STI Page 4 of 18
- 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. 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. 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. Data Structures and Algorithms Problem Solving with LoopsFlowchart Diagram of REPEAT/UNTILProblem Solving with Loops *Property of STI Page 8 of 18
- 9. Data Structures and Algorithms Problem Solving with LoopsDecision Equivalent to REPEAT/UNTILProblem Solving with Loops *Property of STI Page 9 of 18
- 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. 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. Data Structures and Algorithms Problem Solving with LoopsDecision Equivalent to Automatic-Counter LoopProblem Solving with Loops *Property of STI Page 12 of 18
- 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. 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. Data Structures and Algorithms Problem Solving with LoopsNested LoopsProblem Solving with Loops *Property of STI Page 15 of 18
- 16. Data Structures and Algorithms Problem Solving with LoopsProblem Solving with Loops *Property of STI Page 16 of 18
- 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. Data Structures and Algorithms Problem Solving with LoopsProblem Solving with Loops *Property of STI Page 18 of 18

Be the first to comment