Hop Right

Understanding the While Loop
x = 0
while abs(x) < 5:
r = randi(0,1)
if r == 0:
x = x+1
else:
x = x-1
-5
-4
-3
-2
-1
0
1
2
3
4
5
The value of
X
is increased
from 0 to 1.

Understanding the While Loop
x = 0
while
abs(x) < 5
:
r = randi(0,1)
if r == 0:
x = x+1
else:
x = x-1
-5
-4
-3
-2
-1
0
1
2
3
4
5
abs(x) < 5 is true.
Robot not at
boundary.
Loop continues.

Understanding the While Loop
x = 0
while abs(x) < 5:
r = randi(0,1)
if r == 0:
x = x+1
else:
x = x-1
-5
-4
-3
-2
-1
0
1
2
3
4
5
Assume r = 1
Coin = Tails
Hop Left

Understanding the While Loop
x = 0
while abs(x) < 5:
r = randi(0,1)
if r == 0:
x = x+1
else:
x = x-1
-5
-4
-3
-2
-1
0
1
2
3
4
5
The value of
X
is decreased
from 1 to 0.

Understanding the While Loop
x = 0
while
abs(x) < 5
:
r = randi(0,1)
if r == 0:
x = x+1
else:
x = x-1
-5
-4
-3
-2
-1
0
1
2
3
4
5
abs(x) < 5 is true.
Robot not at
boundary.
Loop continues

Understanding the While Loop
x = 0
while abs(x) < 5:
r = randi(0,1)
if r == 0:
x = x+1
else:
x = x-1
-5
-4
-3
-2
-1
0
1
2
3
4
5
Assume r = 0
Coin = Heads
Hop Right

Understanding the While Loop
x = 0
while abs(x) < 5:
r = randi(0,1)
if r == 0:
x = x+1
else:
x = x-1
-5
-4
-3
-2
-1
0
1
2
3
4
5
The value of
X
is increased
from 0 to 1.

Understanding the While Loop
x = 0
while
abs(x) < 5
:
r = randi(0,1)
if r == 0:
x = x+1
else:
x = x-1
-5
-4
-3
-2
-1
0
1
2
3
4
5
abs(x) < 5 is true.
Robot not at
boundary.
Loop continues

Understanding the While Loop
x = 0
while abs(x) < 5:
r = randi(0,1)
if r == 0:
x = x+1
else:
x = x-1
-5
-4
-3
-2
-1
0
1
2
3
4
5
Assume r = 0
Coin = Heads
Hop Right

Understanding the While Loop
x = 0
while abs(x) < 5:
r = randi(0,1)
if r == 0:
x = x+1
else:
x = x-1
-5
-4
-3
-2
-1
0
1
2
3
4
5
The value of
X
is increased
from 1 to 2.

Understanding the While Loop
x = 0
while
abs(x) < 5
:
r = randi(0,1)
if r == 0:
x = x+1
else:
x = x-1
-5
-4
-3
-2
-1
0
1
2
3
4
5
abs(x) < 5 is true.
Robot not at
boundary.
Loop continues

Understanding the While Loop
x = 0
while abs(x) < 5:
r = randi(0,1)
if r == 0:
x = x+1
else:
x = x-1
-5
-4
-3
-2
-1
0
1
2
3
4
5
Assume r = 0
Coin = Heads
Hop Right

Understanding the While Loop
x = 0
while abs(x) < 5:
r = randi(0,1)
if r == 0:
x = x+1
else:
x = x-1
-5
-4
-3
-2
-1
0
1
2
3
4
5
The value of
X
is increased
from 2 to 3.

Understanding the While Loop
x = 0
while abs(x) < 5:
r = randi(0,1)
if r == 0:
x = x+1
else:
x = x-1
-5
-4
-3
-2
-1
0
1
2
3
4
5
Assume r = 1
Coin = Tails
Hop Left

Understanding the While Loop
x = 0
while abs(x) < 5:
r = randi(0,1)
if r == 0:
x = x+1
else:
x = x-1
-5
-4
-3
-2
-1
0
1
2
3
4
5
The value of
X
is decreased
from 3 to 2.

Understanding the While Loop
x = 0
while
abs(x) < 5
:

#### You've reached the end of your free preview.

Want to read all 48 pages?

- Fall '19
- Control flow