- Practice using loops.
- Encounter an infinite loop situation.
- Use
arc4random_uniform()
to generate a value within a specified range.
Whiley Coyote is chasing us so, like Road Runner, we need to make sure that we stay one step ahead of him! To help us out, Road Runner is waiting 50 steps away from us with an anvil ready to drop on Whiley Coyote and flatten him into the ground. This means that we need to run 51 steps to ensure we don't get hit by the anvil ourselves. For each step we take, let's imitate Road Runner by printing "Meep! Meep!" to the console.
To track our steps, let's implement a do-while
loop:
do {
statements
} while (condition)
- Open the
*.xcworkspace
file and navigate to theapplication:didFinishLaunchingWithOptions:
method in theFISAppDelegate.m
implementation file. - Write a
do-while
loop thatNSLog()
s "Meep! Meep!". WriteYES
inside the conditional()
.
- Hit run (
⌘``R
) and watch and your console printouts. You've just created an infinite loop! Hit stop (⌘``.
).
- Declare an
NSUInteger
variable calledsteps
before thedo-while
loop in order to count the steps. Startsteps
at0
("zero") and set the loop's conditional to pass ifsteps
is less than or equal to50
.
- Hit run and watch your console printouts. It's an infinite loop again! That's because we're not incrementing our counter inside the loop so the conditional will keep passing forever.
- Insert
steps++;
into the loop's implementation body beneath theNSLog()
. This will add one tosteps
each time the loop runs.
- Hit run. You should see a total of 51
Meep! Meep!
s. Hint: Instead of counting theMeep! Meep!
s, think about how you can print the value of thesteps
integer along with eachMeep! Meep!
.
- Whiley Coyote gets frustrated easily and pulls out a sign that reads "YOU'RE CUCKOO!" every ten steps or so.
- At the beginning of the loop, add an
if
statement that checks whethersteps
is an even factor of10
and also greater than0
. Hint: Use the modulus operator%
to calculate the remainder of dividingsteps
by ten. - Insert an
NSLog()
that printsYOU'RE CUCKOO!
into theif
statement. - Hit run. You should see
YOU'RE CUCKOO!
mixed into theMeep! Meep!
s in your console output.
- Immediately after this
if
statement, add a newif
statement that checks whensteps
equals50
. Insert anNSLog()
that prints a string describing the sound of the anvil falling on Whiley Coyote (e.g.@"SMASH!"
).
- Hit run. Your console output should end with:
...
Meep! Meep!
Meep! Meep!
YOU'RE CUCKOO!
SMASH!
Meep! Meep!
Whiley Coyote is actually pretty clever despite constantly getting outsmarted by Road Runner. Now that he's expecting the anvil to drop on him after 50 steps, Road Runner says that we need to randomize our plan to drop the anvil.
- Define a new
NSUInteger anvil
variable before the loop and set it equal toarc4random_uniform(25) + 26
. This generates a random integer from26
to50
. Replace the two occurrences of50
in the conditionals with theanvil
variable.
- Hit run a few different times. Notice that the Road Runner drops the anvil after a different number of steps each time.
- Play around with the range of the
anvil
variable by changing the integers that affect the usage ofarc4random_uniform()
.