Download presentation
Presentation is loading. Please wait.
Published byἌρης Ταρσούλη Modified over 6 years ago
1
Week 8 - Programming II Today – more features: Loop control
Extending if/else Nesting of loops Debugging tools Textbook chapter 7, pages (sections 7.2.3, 7.5, 7.6 )
2
Loop Controls Loops contain sets of commands that you want to do repeatedly. You might want to: Skip commands in the current iteration Stop the loop itself Why continue once you’ve found what you’re looking for !!
3
Skipping Ahead: Continue
Continue – jumps to next loop iteration: for k = 1:25000 if x(k) > 0 continue end { more commands } skip ahead
4
Early Termination: Break
Break – ends the loop: for variable = {array of length n} ….. break end go to commands beyond end
5
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
6
only needed 10 years
7
Example – accept input, appending it to vector, until a negative number is entered:
allow up to 1000 values, if necessary
8
negative value stops the input
9
provide hi/lo feedback
Example – Hi-Lo: a guessing game with feedback select hidden number input guess correct? yes no provide hi/lo feedback 5 tries? win lose
11
Extensions of if/else if expression
As introduced, if/else allows for two choices: if expression {commands if expression is true } else {commands if false } end
12
What if there are more than 2 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
13
Could use “nested” if/else commands
14
or
15
if expression1 The “elseif” command {commands if expression1 is true }
elseif expression2 {commands if expression2 is true } else {commands if both expressions are false } end
16
Examples: Note – many elseifs are allowed, but only 1 “else”
17
Loops within Loops – Nesting
for index1 = array1 {outer loop commands} for index2 = array2 {inner loop commands} end {more outer loop commands} can be more than 2 levels deep
18
Variable values by example
index1 index2 Variable values by example
19
Example – computing a table of z = x2+y2 for x and y equal to the integers 1, 2,…6:
20
Example – matching of people’s skills and tasks:
Job 1 Job 2 Job 3 Job 4 Joe 7 4 2 Sue 6 8 5 Bob 1 3 Liz 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
21
Solution – use nested loops to try all combinations, skipping repeats:
First, let’s initialize variables: Job 1 2 3 4 Joe 7 Sue 6 8 5 Bob Liz
22
Next, start nested loops:
Check for repeats and skip
23
Test a valid assignment for quality:
And then terminate the 4 for loops:
24
Job 1 Job 2 Job 3 Job 4 Joe 7 4 2 Sue 6 8 5 Bob 1 3 Liz The result:
25
Debugging Tools so far, Run
26
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
27
controls for creating and removing breakpoints
28
indicator of breakpoint location (can have multiple breakpoints)
29
What shows up at the breakpoint
Command window: Editor window: different prompt location indicator
30
Can single step (F10) or continue (F5) to the next breakpoint (or end)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.