1. Control variables 2. Example 3. Infinite loop 4. Trap user while invalid input 1. Ask first, then...

11
1. Control variables 2. Example 3. Infinite loop 4. Trap user while invalid input 1. Ask first, then loop 2. Hardcode bad, then loop Review while loops 1

Transcript of 1. Control variables 2. Example 3. Infinite loop 4. Trap user while invalid input 1. Ask first, then...

Page 2: 1. Control variables 2. Example 3. Infinite loop 4. Trap user while invalid input 1. Ask first, then loop 2. Hardcode bad, then loop Review while loops.

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

Page 3: 1. Control variables 2. Example 3. Infinite loop 4. Trap user while invalid input 1. Ask first, then loop 2. Hardcode bad, then loop Review while loops.

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

Page 7: 1. Control variables 2. Example 3. Infinite loop 4. Trap user while invalid input 1. Ask first, then loop 2. Hardcode bad, then loop Review while loops.

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

Page 8: 1. Control variables 2. Example 3. Infinite loop 4. Trap user while invalid input 1. Ask first, then loop 2. Hardcode bad, then loop Review while loops.

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

Page 9: 1. Control variables 2. Example 3. Infinite loop 4. Trap user while invalid input 1. Ask first, then loop 2. Hardcode bad, then loop Review while loops.

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

Page 10: 1. Control variables 2. Example 3. Infinite loop 4. Trap user while invalid input 1. Ask first, then loop 2. Hardcode bad, then loop Review while loops.

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

Page 11: 1. Control variables 2. Example 3. Infinite loop 4. Trap user while invalid input 1. Ask first, then loop 2. Hardcode bad, then loop Review while loops.

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