7 problem solving with loops
Upcoming SlideShare
Loading in...5
×
 

7 problem solving with loops

on

  • 5,901 views

 

Statistics

Views

Total Views
5,901
Slideshare-icon Views on SlideShare
5,901
Embed Views
0

Actions

Likes
1
Downloads
111
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    7 problem solving with loops 7 problem solving with loops Document Transcript

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