Presentation is loading. Please wait.

Presentation is loading. Please wait.

CHAPTER 5A Loop Structure

Similar presentations


Presentation on theme: "CHAPTER 5A Loop Structure"— Presentation transcript:

1 CHAPTER 5A Loop Structure

2 Objectives: In this chapter, you will learn about:
The advantages of looping Using a loop control variable Nested loops Avoiding common loop mistakes Using a for loop Common loop applications

3 5.1 Advantages of Looping Loop Advantages
Set of instructions that is executed repetitively based on a condition Advantages Write one set of instructions to operate on multiple, separate sets of data Allows processing on large sets of data, such as complex payroll and benefits processing Example: while, do…while, for loop

4 5.2 While loop with Loop Control Variable
Definite Loop with a Counter Definite loop Executes a definite, predetermined number of times Counter-controlled loop Program counts loop repetitions Loop control variables altered by: Incrementing Decrementing

5 5.2 While loop with Loop Control Variable
Definite Loop with a Counter

6 5.2 While loop with Loop Control Variable
Indefinite Loop with a Sentinel Value Indefinite loop Performed a different number of times each time the program executes The user decides how many times the loop executes Use a sentinel value to terminate the loop Sentinel Sentinel is a preselected value that stops the execution of a program The sentinel is assigned to a loop control variable

7 5.2 While loop with Loop Control Variable
Indefinite Loop with a Sentinel Value

8 5.2 While loop with Loop Control Variable
Indefinite Loop with a Sentinel Value

9 Questions: While Loop Determine the output of the following pseudocode: start set number = 2 while number <= 50 output number number = number * 2 endwhile stop

10 Questions: While Loop Write a pseudocode & flowchart to display the numbers from 1 to 10.

11 Questions: While Loop Write a pseudocode & flowchart that displays the even numbers between 1 and 9.

12 Questions: While Loop Write a pseudocode & flowchart to calculate and display the sum of the numbers from 1 to 10.

13 Questions: While Loop Write a pseudocode & flowchart to calculate and display the sum of the numbers from 1 to 10. Calculate and display the average value of the sum of the numbers. Hint: Determine the sum from Determine the average value from the sum.

14 Questions: While Loop Every day, a weather station receives 15 temperatures expressed in degrees Fahrenheit. A program is to be written that will accept each Fahrenheit temperature, convert it to Celsius and display the converted temperature to the screen. After 15 temperatures have been processed, the words “All temperatures processed” are to be displayed on the screen.

15 while loop structure Assignment:
Design an algorithm that print “hello” five times. Design an algorithm that calculate the total from 0 to 10 and display the total results. Design an algorithm that print 0,2,4,6,8,….,20. Design an algorithm that print the sum of the even integers from 2 to 10.

16 5.3 Nested Loop Nested loops loops within loops Outer loop
the loop that contains the other loop Inner loop the loop that is contained Needed when values of two (or more) variables repeat to produce every combination of values

17 5.3 Nested Loop Example: Answer Sheet - without loop, 63 output statements

18 5.3 Nested Loop Example: Answer Sheet (cont’d) “QUIT”

19 5.3 Nested Loop Example: Answer Sheet (cont’d)

20 5.3 Nested Loop Example: Answer Sheet (cont’d)

21 5.3 Nested Loop

22 5.5 Avoiding Common Loop Mistakes
Common Mistakes: Neglecting to initialize the loop control variable Neglecting to alter the loop control variable Using the wrong comparison with the loop control variable Including statements inside the loop that belong outside the loop

23 5.5 Avoiding Common Loop Mistakes
Greeting Program For example

24 5.5 Avoiding Common Loop Mistakes
Missing Initialization

25 5.5 Avoiding Common Loop Mistakes
Mistakes 1: Neglecting to initialize the loop control variable Example: input name statement removed Value of name unknown or garbage Program may end before the user can enter any names Or the greeting output will included garbage – for example, “ Hello 1ABr5”

26 5.5 Avoiding Common Loop Mistakes
Not altering loop control variable

27 5.5 Avoiding Common Loop Mistakes
Mistakes 2: Neglecting to alter the loop control variable Remove input name instruction from end of the loop in the programe User never enters a name after the first one Inner loop executes infinitely Always incorrect to create a loop that cannot terminate

28 5.5 Avoiding Common Loop Mistakes
Wrong test

29 5.5 Avoiding Common Loop Mistakes
Mistakes 3: Using the wrong comparison with the loop control variable Programmers must use correct comparison Seriousness depends on actions performed within a loop Overcharge insurance customer by one month Overbook a flight on airline application Dispense extra medication to patients in pharmacy

30 5.5 Avoiding Common Loop Mistakes
Mistakes 4: Including statements inside the loop that belong outside the loop Example: Discount every item by 30 percent & produce 100 new price label stickers for each item Inefficient because the same value (newPrice) is calculated 100 separate times for each price that is entered Move outside the loop for efficiency

31 5.5 Avoiding Common Loop Mistakes

32 5.5 Avoiding Common Loop Mistakes

33 5.5 Avoiding Common Loop Mistakes

34 5.6 For Loop for statement or for loop is a definite loop
Provides three actions in one structure Initializes Evaluates Alters

35 5.6 For Loop Pseudocode: Step value
The amount by which a loop control variable changes Can be positive or negative (incrementing or decrementing) Default step value is 1 Programmer specifies a step value when each pass through the loop changes the loop control variable by a value other than 1

36 5.6 For Loop Example: Display “Hello” four times
Above code accomplish 3 tasks: count is initialized to 0 count is compared to the limit value 3 while count ≤ 3, loop body executes (output “Hello”) Increases count by 1

37 5.6 For Loop Pre-test loop the loop control variable is tested before each iteration for loops and while loops are pre-test loops Post-test loop the loop control variable is tested after each iteration do…while is a post-test loop

38 5.6 For Loop

39 Questions: For Loop Use for loop to write a pseudocode & flowchart to display the numbers from 1 to 10.

40 Questions: For Loop Use for loop to write a pseudocode & flowchart to display the numbers 4,6,8,10,12,14,16,18,20

41 Questions: For Loop Use a for loop to write a pseudocode & flowchart to calculate and display the sum of the numbers from 1 to 10.

42 Summary Loops write one set of instructions that operate on multiple, separate sets of data Three steps must occur in every loop Initialize the loop control variable Compare the variable to some value Alter the variable that controls the loop Nested loops loops within loops maintain two individual loop control variables Alter each at the appropriate time

43 Summary Common mistakes made by programmers
Neglecting to initialize the loop control variable Neglecting to alter the loop control variable Using the wrong comparison with the loop control variable Including statements inside the loop that belong outside the loop Most computer languages support a for statement for loop used when the number of iterations is known


Download ppt "CHAPTER 5A Loop Structure"

Similar presentations


Ads by Google