1. Control variables 2. Example 3. Infinite loop 4. Trap user while invalid input 1. Ask first, then...
-
Upload
felix-warner -
Category
Documents
-
view
214 -
download
0
Transcript of 1. Control variables 2. Example 3. Infinite loop 4. Trap user while invalid input 1. Ask first, then...
1. Control variables
2. Example
3. Infinite loop
4. Trap user while invalid input1. Ask first, then loop
2. Hardcode bad, then loop
Review while loops
1
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
2
2. An example
Example: the loop control variable is x
x = 0;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!
3
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
4
Infinite loop, cont.
Why is this an infinite loop in MATLAB?
x = 11; % 1. initialization
while (x > 10) % 2. conditionfprintf(‘%02d\n’, x);x = x + 1; % 3. change loop
variableend
5
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:1. Ask first, then loop
2. Initialize the control variable to a bad value, and loop
6
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 distancedistance = input(‘Enter a distance (1-10 cm): ’);
%while this value was invalid, prompt againwhile (distance<1 || 10<distance)
distance = input(‘Error! Enter a value 1-10: ’);end
7
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 againwhile ~(1<= distance && distance<=10)
distance = input(‘Error! Enter a value 1-10: ’);end
8
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 oncedistance = -1; %BAD VALUE%prompt/repeat while invalidwhile (distance<1 || 10<distance)
distance = input(‘Enter a distance (1-10 cm): ’); end
This option does not allow an easy ERROR message.9
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 oncedistance = -1; %BAD VALUE%prompt/repeat while invalidwhile ~(1<= distance && distance<=10)
distance = input(‘Enter a distance (1-10 cm): ’); end
This option does not allow an easy ERROR message.10
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
11