Presentation is loading. Please wait.

Presentation is loading. Please wait.

CHAPTER #7 Problem Solving with Loop. Overview Loop logical structure Incrementing Accumulating WHILE/WHILE-END FOR Nested loop Pointer Algorithmic instruction.

Similar presentations


Presentation on theme: "CHAPTER #7 Problem Solving with Loop. Overview Loop logical structure Incrementing Accumulating WHILE/WHILE-END FOR Nested loop Pointer Algorithmic instruction."— Presentation transcript:

1 CHAPTER #7 Problem Solving with Loop

2 Overview Loop logical structure Incrementing Accumulating WHILE/WHILE-END FOR Nested loop Pointer Algorithmic instruction and symbol Recursion

3 Objectives Develop problems using the loop logic structure Use the problem-solving tools when developing a solution using the loop logic structure Use counters and accumulators in a problem solution Use nested loop instructions Distinguish the different uses of three types of loop logic structures. Use recursion in a simple problem.

4 Background The loop logic structure is the repeating structure Some ‘process’ is repeated several times (known) or until a condition is reached (unknown) for different sets of data So this structure is extremely important

5 Example(s) Emptying a pool with small pail Cut a cucumber into small piece Sharpening a pencil Push up 20 times Etc...

6 Increment The task of incrementing, or counting, as we said, 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 when counting by ones

7 What’s happen? No of iteration Expression/Statemen t New Value 0 (init)Counter = 1 (example, its called initialization) 1Counter = Counter + 1Counter = 1 + 1 = 2 2Counter = Counter + 1Counter = 2 + 1 = 3 3Counter = Counter + 1Counter = 3 + 1 = 4 etc Red written indicates old value

8 Accumulating The process of accumulating is similar to incrementing, except a variable instead of a constant is added to another variable, which holds the value of the sum or total The instruction for accumulating is the following: Sum = Sum + Variable

9 What’s happen? No of iteratio n Old value of sum Example of ‘Value’ Sum = Sum + Value 1Sum = 0Value = 1Sum = 0 + 1 = 1 2 Sum = 1 Value = 2Sum = 1 + 2 = 3 3 Sum = 3 Value = 3Sum = 3 + 3 = 6 4 Sum = 6 Value = 4Sum = 6 + 4 = 10 etc Red written indicates old value Initialization : Sum = 0

10 Product Calculating the product of a series of numbers is similar to finding the sum of a series of numbers with two exceptions First, the plus sign is replaced by the multiplication sign (*) Second, the product variable must be initialized to 1 instead of 0. For example Product = Product * Number

11 What’s happen? No of iteratio n Old value of Product Example of ‘Number’ Product = Product * Number 1Product = 1Number = 1Product = 1 * 1 = 1 2 Product = 1 Number = 2Product = 1 * 2 = 2 3 Product = 2 Number = 3Product = 2 * 3 = 6 4 Product = 6 Number = 4Product = 6 * 4 = 24 etc Red written indicates old value Initialization : Product = 1

12 Loop Logic Structure

13 Types of Loop Structures While/While End For/End For Repeat/Until

14 Case study Develop a problem-solving solution for counting an average for 10 numbers.

15 While/WhileEnd Pretest Looping (While Loop) 0-15

16 Figure 7.1 Flowchart Diagram of While/WhileEnd 0-16

17 Algorithm #1  using WHILE Set Sum = 0 Set count = 0 While (count < 10) –Enter Number –Sum = Sum + Number –Count = Count + 1 End While //finish when count = 10 or more (>= 10) Average = Sum / Count //count = 10 Display Average

18 Automatic Counter Loop Format Pretest Looping (For / Counter Loop)

19 Flowchart of Automatic- Counter Loop

20 Algorithm #2  using FOR Set Sum = 0 For count = 1 to 10 –Enter Number –Sum = Sum + Number End For //finish for (10 – 1 + 1) repetitions Average = Sum / Count //count = 10 Display Average

21 Repeat/Until Algorithm Format Posttest Looping (Do-While Loop)

22 Flowchart Diagram of Repeat/Until

23 Algorithm #3  using REPEAT/UNTIL Set Sum = 0 Set count = 0 Repeat –Enter Number –Sum = Sum + Number –Count = Count + 1 Until (count >= 10) //finish when count = 10 or more Average = Sum / Count //count = 10 Display Average

24 Exercise Develop a problem-solving solution for counting an average for determined numbers (entered by user).

25 Nested Loops

26

27

28

29 Exercise – Nested Loop Write a problem-solving solution for a problem that ask user for an input (called n), then display a ‘triangle’ with this format * * * * * * *... (depends on n)

30 Review Mention about problem solving that involves three logic structures

31 Indicators Logical variables that a programmer sets within a program to change the processing path or to control when the processing of a loop should end  Therefore you will get unstopped loop The user has no knowledge of these indicators during processing

32

33

34

35 Recursion Recursion occurs when a module or a function calls itself The condition that ends the loop must be within the module Recursive procedures can be replaced by conventional loop structures; however, recursive procedures are usually faster

36 When we using recursion? Recursion is difficult to use in a solution However, it is needed more and more in today’s programming A recursive technique is used in finding the power of a number, sorting large amounts of data, and selecting the correct form from many report forms, and find factorial for a number

37 Assignment Write a problem-solving solution to solve a problem that takes an integer value and returns the number with its reversed digits. For example, given the number 7631, the solution should return 1367 Hint : Use the loop structure and modulo operation %


Download ppt "CHAPTER #7 Problem Solving with Loop. Overview Loop logical structure Incrementing Accumulating WHILE/WHILE-END FOR Nested loop Pointer Algorithmic instruction."

Similar presentations


Ads by Google