7 problem solving with loops

18
Problem Solving with Loops *Property of STI Page 1 of 18 Data Structures and Algorithms Problem Solving with Loops The Loop Logic Structure A third logic structure for designing decisions is the loop structure. The loop logic structure is the repeating structure. Types of Loop Structures @ WHILE / WHILE-END @ REPEAT / UNTIL @ AUTOMATIC COUNTER LOOP Incrementing The task of incrementing, or counting, is done by adding a constant, such as 1 or 2, to the value of a variable. To write the instruction to increment a variable, you use an assignment statement. For example: COUNTER = COUNTER + 1 or C = C+ 1

Upload: rheigh-henley-calderon

Post on 02-Nov-2014

19 views

Category:

Technology


3 download

DESCRIPTION

 

TRANSCRIPT

Page 1: 7 problem solving with loops

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

Page 2: 7 problem solving with loops

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

Page 3: 7 problem solving with loops

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

Page 4: 7 problem solving with loops

Problem Solving with Loops *Property of STIPage 4 of 18

Data Structures and Algorithms

Problem Solvingwith Loops

Flowchart Diagram of WHILE/WHILE-END

Page 5: 7 problem solving with loops

Problem Solving with Loops *Property of STIPage 5 of 18

Data Structures and Algorithms

Problem Solvingwith Loops

Decision Equivalent to WHILE/WHILE-END

Page 6: 7 problem solving with loops

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.

Page 7: 7 problem solving with loops

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)>

Page 8: 7 problem solving with loops

Problem Solving with Loops *Property of STIPage 8 of 18

Data Structures and Algorithms

Problem Solvingwith Loops

Flowchart Diagram of REPEAT/UNTIL

Page 9: 7 problem solving with loops

Problem Solving with Loops *Property of STIPage 9 of 18

Data Structures and Algorithms

Problem Solvingwith Loops

Decision Equivalent to REPEAT/UNTIL

Page 10: 7 problem solving with loops

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.

Page 11: 7 problem solving with loops

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

Page 12: 7 problem solving with loops

Problem Solving with Loops *Property of STIPage 12 of 18

Data Structures and Algorithms

Problem Solvingwith Loops

Decision Equivalent to Automatic-Counter Loop

Page 13: 7 problem solving with loops

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.

Page 14: 7 problem solving with loops

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.

Page 15: 7 problem solving with loops

Problem Solving with Loops *Property of STIPage 15 of 18

Data Structures and Algorithms

Problem Solvingwith Loops

Nested Loops

Page 16: 7 problem solving with loops

Problem Solving with Loops *Property of STIPage 16 of 18

Data Structures and Algorithms

Problem Solvingwith Loops

Page 17: 7 problem solving with 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.

Page 18: 7 problem solving with loops

Problem Solving with Loops *Property of STIPage 18 of 18

Data Structures and Algorithms

Problem Solvingwith Loops