Presentation is loading. Please wait.

Presentation is loading. Please wait.

Review while loops Control variables Example Infinite loop

Similar presentations


Presentation on theme: "Review while loops Control variables Example Infinite loop"— Presentation transcript:

1 Review while loops Control variables Example Infinite loop
Trap user while invalid input Ask first, then loop Hardcode bad, then loop

2 1. A control variable Any loop has a starting point and an ending point. In programming, a “loop control variable” keeps track of where we are. This variable has 3 phases: Initialization of variable – starting point Change of variable - incrementation Condition to exit loop – ending point

3 2. An example Example: the loop control variable is x
while (x < 10)  2. condition x = x + 1; fprintf(‘%02d\n’, x); end The loop starts at zero, and while x is strictly less than 10, x is incremented by 1 and displayed to the screen. The loop stops when x actually reaches 10!

4 3. Infinite loop Infinite Loop  Loop control variable never meets the condition and the code runs and runs……. Use CTRL+C in the command window to break it

5 Infinite loop, cont. Why is this an infinite loop in MATLAB?
x = 11; % 1. initialization while (x > 10) % 2. condition fprintf(‘%02d\n’, x); x = x + 1; % 3. change loop variable end

6 4. Trap a user for a value Ask a user for a decimal value between 1.0 to 10.0 What if they don’t give the proper value? Two approaches: Ask first, then loop Initialize the control variable to a bad value, and loop

7 4.1. Ask first, then loop (1) Ask the use first for a value (Give the user a chance to NOT mess up!) Then loop while it was a wrong value. %ask user for a specific distance distance = input(‘Enter a distance (1-10 cm): ’); %while this value was invalid, prompt again while (distance<1 || 10<distance) distance = input(‘Error! Enter a value 1-10: ’); end

8 4.1. Ask first, then loop (2) Ask the use first for a value (Give the user a chance to NOT mess up!) Then loop while it was a wrong value. %ask user for a specific distance distance = input(‘Enter a distance (1-10cm): ’); %while this value was invalid, prompt again while ~(1<= distance && distance<=10) distance = input(‘Error! Enter a value 1-10: ’); end

9 4.2. Make bad, then loop (1) The trick is to make MATLAB think a bad value has been entered, without prompting the user!!  HARDCODE %makes loop run at least once distance = -1; %BAD VALUE %prompt/repeat while invalid while (distance<1 || 10<distance) distance = input(‘Enter a distance (1-10 cm): ’); end This option does not allow an easy ERROR message.

10 4.2. Make bad, then loop (2) The trick is to make MATLAB think a bad value has been entered, without prompting the user!!  HARDCODE %makes loop run at least once distance = -1; %BAD VALUE %prompt/repeat while invalid while ~(1<= distance && distance<=10) distance = input(‘Enter a distance (1-10 cm): ’); end This option does not allow an easy ERROR message.

11 Wrapping Up while loops are very common to validate user-inputs.
The block of code in a while loops only runs when the condition is true. The code block must change the condition, or it is called an infinite loop. A control-loop variable keeps track of whether the loop should execute or not


Download ppt "Review while loops Control variables Example Infinite loop"

Similar presentations


Ads by Google