1 / 45

Problem Solving with Loops

Problem Solving with Loops. Chapter 7. Concept of Repetition Structure Logic. It is a computer task, that is used for Repeating a series of instructions many times. Ex. The Process of calculating the Total Payment for more than one employee.

rian
Download Presentation

Problem Solving with Loops

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Problem Solving with Loops Chapter 7

  2. Concept of Repetition Structure Logic • It is a computer task, that is used for Repeating a series of instructions many times. • Ex. The Process of calculating the Total Payment for more than one employee. • The main process is identifying what instruction should be repeated. • Loops can be written using combination of previouslogic structures: • If/Then/Else structure.

  3. Example: Write an Algorithm that calculate the average of 7 students grade.

  4. Solution without looping structure • Read() • Enter St1_Grade • Enter St2_Grade • Enter St3_Grade • Enter St4_Grade • Enter St5_Grade • Enter St6_Grade • Enter St7_Grade • Exit • Real St1_Grade, St2_Grade, St3_Grade, St4_Grade, St5_Grade, St6_Grade, St7_Grade, • Control () • Process Read() • Process Average() • End Average() • Real Sum, Average • Sum = St1_Grade+St2_Grade+St3_Grade+ St4_Grade+St5_Grade+St6_Grade+ St7_Grade • Average = Sum/ 7 • Exit

  5. Solution with looping structure Average() • Integer Count=1 • Real Sum=0 • While ( count <= 7) • process Read() • Sum=Sum +Student_Grade • Count=Count+1 • WhileEnd • Real Average = Sum / count • Exit • Real Student_Grade • Control () • Process Average() • End • Read() • Enter Student_Grade • Exit

  6. Average Module Algorithm Flowchart What will Happen If we remove this? What will Happen If we remove this? Average() Sum has no initial value, its value is Unknown Integer Count=1 • Real Sum=0 Count has no data, its value is Unknown, So the value of the condition is unpredictable. T F While Count<=7 What will Happen If we remove this? Read() Real Average = Sum / 7 Sum=Sum +Student_Grade Exit Count=Count+1 This will lead to an infinite loop

  7. The loop logic structure • The loop logic structure is the repeating structure . • Through the loop structure , a program can process: • many payroll records . • inventory items . • put a mailing list in alphabetical or zip code order .

  8. The loop logic structure • There are three types of loop structures: • The While/WhileEndloop • Which repeats instructions while a condition is True and stops repeating when a condition arises that is not True . • The Repeat/Until loop • Which repeats instructions while a condition is False or until a condition is True . • The automatic-counter loop • Which a variable is set equal to a given number and increases in equal given increments until it is greater than an ending number .

  9. The loop logic structure • The algorithm and flowchart differ with each type of loop structure . • Several standard types of tasks are accomplished through the use of the loop structure . • Counting ( incrementing and decrementing) • Accumulating ( calculating a sum or a total) • In both task a number is added or subtracted from a variable and the result is stored back into the same variable . • In each case the resultant variable is assigned the value of zero before starting the loop( initializing the variable ) .

  10. Initialization • The Initialization • set to an initial value • usually zero but not all the time • Examples: • Count = 0 • Count = 1 • Count = 100

  11. Incrementing (or Decrement) • Is done by adding a constant , such as 1 or 2 to the value of a variable • Example : Counter = counter + 1 or c = c + 1 • The increment can be one , two , or any other constant , including negative number if you want to decrement rather than increment . • Example: Counter = counter -1 or c = c - 1 Note: Remember Variable Must be Initialized before starting the loop.

  12. The Accumulating • Or summing , a group of numbers • Similar to incrementing, except a variable instead of a constant is added to another variable. sum = sum + variable or s = s + v • Examples: Totalsales = Totalsales + Sales Note: Remember Variable Must be Initialized to zero.

  13. Product • Is similar to finding the sum of a series of number with two exceptions : • The plus sign is replaced by the multiplication sign (*). • The product variable must be initialized to 1 instead of 0. • Example : product = 1 Product = Product * Number

  14. While <Condition (s)> • Instruction • Instruction • . • . • . • WhileEnd While/ While End Loop. • Repeats the instructions between the While & While End, if the condition is true.

  15. While/ While End Loop. • Use the While/ While End Loop structure when you do not know the number of times an instruction to be repeated. • Or if there are cases when the instructions in the loop should not be processed .

  16. while/whileEnd • Use the While/ While End Loop structure when you do not know the number of times an instruction to be repeated. • Or if there are cases when the instructions in the loop should not be processed . • Primer read The value must be entered before the loop • It gives the while/whileEnd loop a valid value for the variable in order for the conditions to be true the first time through the loop . • The value of variable that allow to control when to stop the looping process and continue with the rest of the solution called a trip value .

  17. Example 1: Create the algorithm to find the average age of all the students in class UNKOWN How many Students? How can I solve this problem?

  18. Average Age of a Class – While/WhileEnd

  19. Repeat/Until • Repeat the set of instructions between the Repeat and the Until , until the condition is true. • Use it when you do not know the number of times the instruction will be executed .

  20. Repeat/Until • The Whil/WhilEnd • You must initialize the data so that the resultant of the condition is true the first time through the loop . • Otherwise , the loop instruction will never be processed . • The Repeat/Until • You can set the operand of the conditions anywhere within the loop since the condition is processed at the end . • So , if there any reason that the loop instruction should not be processed the first time the Whil/WhilEnd must be used .

  21. Repeat • Instruction • Instruction • . • . • . • Until< Condition(S)> Repeat/Until

  22. Example 1: Create the algorithm to find the average age of all the students in class UNKOWN How many Students? How can I solve this problem?

  23. Average Age of a Class – Repeat/Until

  24. Automatic-Counter Loop • Increments or decrements a variable each time the loop is repeated . • Use it when you know from the start the number of times the loop will be executed . • The beginning value , the ending value , and the increment value may be constant , variable , or expressions .

  25. Loop: counter=begin To End Step S • Instruction • Instruction • Instruction • . • . • Loop –End: Counter Automatic-Counter Loop Variable name

  26. Automatic-Counter Loop General Rules • When the computer executes the Loop instruction its seats the counter equal to the beginning number. • When the computer executes the Loop-End instruction it increments the counter. • When the counter is less than or equal to the ending number • The processing continues for the instructions that follows the loop. • When the counter is greater than the ending number. • The processing continues for the instructions that follows the loop-End instruction.

  27. Automatic-Counter Loop • When decrementing the counter : • The counter is decremented at the end of the loop . • When the counter is greater than or equal to the ending value , the loop continues. • Step value needs to be a negative number and begin must be greater than end

  28. Example 1: using Automatic-Counter Loop Create the algorithm to find the average age of all the students in class

  29. 2 3 ( J -1 ) 4 J , 5 ( J – 1) J,

  30. Example 2: • Create the algorithm & flowchart to: • Prints the even numbers. • The series of numbers will: • Start From 0 . • Ends to 50.

  31. Example 2 Solution

  32. Nested Loops • Loops can be nested like decisions can. • Cannot use the same counter-control variable for the inner loop as is used for the outer loop. • This is used when we want to do something multiple times , and then do that multiple times • The inner loops do not have to be the same types of loop structure as the outer loops.

  33. Example 1:

  34. Example 2

  35. Nested Loop Example Create the algorithm to find the average age of all the students in five classes

  36. Nested Loop Example Solution

  37. Loop : counter=1 to 5 Stnymber=0 Sum=0 Enter age While age >0 Stnumber=stnumber+1 Sum= sum + age Enter age Whileend Average=sum/stnumber Loop:counter

  38. Indicators • Logical variables that change the processing path or to control when the processing of a loop should end . • Called ( flags , switches , trip value ) • An error indicator designates that an error has occurred in the input or the output. • An end-of-data indicator designate that there are no more data to be entered . • They are set internally to change the processing

  39. Enter grade • If grade >0 • Then • Else • Print “ try agine , you are enter rong

  40. Indicators • an indicator can be a variable of logical data or a value that the variable can never equal . • Example : • Indicator for age = 0 or 500 or 300 • Indicator for a n error =true or false

  41. Algorithm Instructions and Flowchart Symbols

  42. Algorithm Instructions and Flowchart Symbols

  43. Algorithm Instructions and Flowchart Symbols

  44. Algorithm Instructions and Flowchart Symbols

  45. Recursion • Another type of loop structure . • A module or a function calls itself . • Example : Factorial(N) 1. If N > 1 then Factorial = N * Factorial (N-1) Else Factorial = 1 2. Exit

More Related