Download presentation

Presentation is loading. Please wait.

1
Example – calculating interest until the amount doubles using a for loop: will calculate up to 1000 years, if necessary if condition decides when to terminate loop break terminates the execution of the whole loop.

2
only needed 10 years

3
Example – accept input, appending it to vector, until a negative number is entered: allow up to 1000 values, if necessary

4
negative value stops the input

5
What if there are more than 2 situations? 3 situations: find the largest of 3 variables a, b, c a ≥ b ≥ c a ≥ c ≥ b b ≥ a ≥ c b ≥ c ≥ a c ≥ b ≥ a c ≥ a ≥ b 4 situations: convert a compass angle to a direction: 0º east 90º north 180º west 270º south

6
Could use “nested” if/else commands

7
or

8
The “elseif” command if expression1 {commands if expression1 is true } elseif expression2 {commands if expression2 is true } else {commands if both expressions are false } end

9
Examples: Note – many elseifs are allowed, but only 1 “else”

10
Example – Hi-Lo: a guessing game with feedback select hidden number input guess correct? yes no provide hi/lo feedback 5 tries? yes no win lose

12
Variable values by example 1 3 1 6 1 9 2 3 2 6 2 9 3 3 6 3 9 4 3 4 6 4 9 index1index2 All possible combinations of the indices are generated.

13
Example – computing a table of z = x 2 +y 2 for x and y equal to the integers 1, 2,…6:

14
Example – matching of people’s skills and tasks: Situation: 4 tasks 4 people with different skills to do them Skill table as shown Goal – assign tasks to maximize the sum Example solution of 20 Job 1 Job 2 Job 3 Job 4 Joe7442 Sue6852 Bob4713 Liz6521

15
Solution – use nested loops to try all combinations, skipping repeats: First, let’s initialize variables: Job 1234 Joe7442 Sue6852 Bob4713 Liz6521

16
Next, start nested loops: Check for repeats and skip continue stops the present pass and starts the next pass.

17
Test a valid assignment for quality: And then terminate the 4 for loops:

18
The result: Job 1 Job 2 Job 3 Job 4 Joe7442 Sue6852 Bob4713 Liz6521

19
Debugging ≡ finding and correcting errors (bugs) in programs Useful debugging tools: – Ability to stop a program in the middle of its execution (at a breakpoint) – Ability to examine variable values at that point – Ability to modify variable values at that point

20
controls for creating and removing breakpoints

21
indicator of breakpoint location (can have multiple breakpoints)

22
What shows up at the breakpoint Command window: Editor window: location indicator different prompt

23
Can single step (F10) or continue (F5) to the next breakpoint (or end)

Similar presentations

© 2019 SlidePlayer.com Inc.

All rights reserved.

To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie policy.

Ads by Google