Learn to Code iOS Apps 1: Welcome to Programming
Learn to code iOS apps using Apple’s development tools. For complete beginners – no prior programming experience needed! By Mike Jaoudi.
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Contents
Learn to Code iOS Apps 1: Welcome to Programming
35 mins
Obtaining User Input
Add the following lines of code immediately after the previously added code:
NSLog(@"Enter a number between 1 and 100");
scanf("%i", &guess);
NSLog(@"You entered: %i", guess);
Aha — that %i
looks familiar, doesn’t it? Format specifiers are used for output and input functions in your app. The %i
format specifier causes scanf
to process the player’s input as an integer.
Run your app; when you see the “Enter a number” prompt, click your mouse in the console to make the cursor appear. Type a number and press Enter; the program should print the number back to you, as shown in the screenshot below:
Now that you’ve confirmed that the random number generator and the user input methods work, you don’t need your debug statements any longer. Remove the following two NSLog
statements from your code:
NSLog(@"The random value is %i", answer);
and
NSLog(@"You entered: %i", guess);
Okay — you have the basic user input and output methods in place. Time to add some game logic.
Working With Conditionals
Right now, your code runs from top to bottom in a linear fashion. But how do you handle the situation where you need to perform different actions based on the user’s input?
Think about the design of your game for a moment. Your game has three possible conditions that need to be checked, and a set of corresponding actions:
- The guess is too high — tell the player to guess a lower number
- The guess is too low — tell the player to guess a higher number
- The guess is correct — congratulate the player
Conditional statements are programming structures that allow you to make on-the-fly decisions in your code and change the flow of the logic. Conditionals work by determining if a particular set of conditions is true. If so, then the app will perform the corresponding specific set of actions.
Add the following lines of code immediately after the scanf("%i", &guess);
line:
if (guess > answer) {
NSLog(@"Lower!");
}
else if (guess < answer) {
NSLog(@"Higher!");
}
else {
NSLog(@"Correct! The answer was %i", answer);
}
The conditional statement above starts with an if
statement and provides a set of conditions inside the parentheses. In the first block, the condition is "is guess
greater than answer
?". If that condition is true, then the app executes the actions inside the first set of curly braces, skips the rest of the conditional statement, and carries on.
If the first condition was not met, the reverse condition is tested with an else if
statement: "is guess
less than answer
?". If so, then the app executes the second set of actions inside the curly braces.
Finally, if neither of the first two conditions are true, then the player must have guessed the correct number. In this case, the app executes the third and final set of actions inside the curly braces. Note that this else
statement doesn't have any conditions to check; this acts as a "catch-all" condition that will execute if none of the preceding conditions were true.
There are many different comparison operators that you can use in your if
statements, including the ones listed below:
- > : greater than
- < : less than
- >= : greater than or equal to
- <= : less than or equal to
- == : equal to
- != : not equal to
Note: To check if two variables are equal, use two equal signs. A single equals sign is the assignment operator, which assigns a value to a variable. It's an easy mistake to make, but just remember that "equal TO" needs "TWO equals"! :]
Note: To check if two variables are equal, use two equal signs. A single equals sign is the assignment operator, which assigns a value to a variable. It's an easy mistake to make, but just remember that "equal TO" needs "TWO equals"! :]
Run your app, and try to guess the number that the computer chose. What happens after you make one guess?
Right now you can only enter one guess before the program quits. Unless you are extremely good at guessing — or psychic! :] — your app will tell you that your guess is incorrect and terminate.
Well, that's no fun. You need some way to loop back to some point in the the program and give the player another chance to guess. Additionally, you want the app to stop when the player guesses the correct number.
This is a job for a while loop.
Working With While Loops
A while loop is constructed much like an if
statement; they both have a condition and a set of curly braces that contain code to execute if the condition is true.
An if
statement runs a code block only once, but a while loop will run the block of code repeatedly until the condition is no longer true. That means your code block needs an exit condition that makes the condition false to end the execution of the while loop. If you don't have an exit condition, the loop could run forever!
The first question is which code needs to be inside the while loop. You don't want to loop over the random number generation with the arc4random
statement, or else the player will be guessing a new random number each time! Just the user prompt, scanf, and the conditional if
block needs to be looped over.
The other question is how to create your exit condition. The repeat condition is to loop while guess
does not match answer
. This way, as soon as the user guesses the correct number, the exit condition occurs automatically.
Note that you will need to add two lines to your existing code to wrap your game logic in a while loop: the while
statement itself, and the closing curly brace to close off the while
loop.
Modify your code to include the two lines indicated by the comments below:
while (guess != answer) { // first line to add
NSLog(@"Enter a number between 1 and 100");
scanf("%i", &guess);
if(guess > answer){
NSLog(@"Lower!");
}
else if(guess < answer){
NSLog(@"Higher!");
}
else{
NSLog(@"Correct! The answer was %i", answer);
}
} // second line to add — end of while loop
Run your app, and play through the game a few times. How good of a guesser are you?