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
Adding the Final Touches
You now have a functional game! There's only one thing to add: the turn counter. This will give your player some feedback on their gameplay.
The turn
variable has already been created to store this information, so it's just a matter of incrementing the value of turn
each time the player makes a guess.
Add the following line of code directly underneath the while (guess != answer) {
statement:
turn++;
turn++;
increments the count by one. Why don't you just use turn = turn + 1;
, you ask? Functionally, it's the same thing. However, incrementing a variable is such a common programming task that it pays to have a shorthand method to save on typing.
Fun Fact: The "C" programming language was derived from a previous language called "B". When the next iteration of the C language was written, the developers put their tongue firmly in cheek and named the new language "C++" — meaning "one better than C". :]
Fun Fact: The "C" programming language was derived from a previous language called "B". When the next iteration of the C language was written, the developers put their tongue firmly in cheek and named the new language "C++" — meaning "one better than C". :]
All that's left to do is display the current value of turn
in two places: on the user prompt, and at the end of the game.
Find the following line of code:
NSLog(@"Enter a number between 1 and 100");
...and modify it to look like the line below:
NSLog(@"Guess #%i: Enter a number between 1 and 100", turn);
The code above uses the format specifier %i
to display the current value of turn
in the user prompt.
Add the following line of code immediately after the closing curly brace of the while loop:
NSLog(@"It took you %i tries", turn);
This will display the final number of guesses once the player has guessed the correct number.
If you feel adventurous, instead of adding the above line to log the number of turns after the while loop, you could also modify the congratulatory message to output the number of turns right there. But I'll leave that as an exercise for you :]
Take a minute and review the contents of main
in your app to make sure that it matches the code below:
int main(int argc, const char * argv[])
{
@autoreleasepool {
int answer = 0;
int guess = 0;
int turn = 0;
answer = arc4random() % 100 + 1;
while (guess != answer) {
turn++;
NSLog(@"Guess #%i: Enter a number between 1 and 100", turn);
scanf("%i", &guess);
if (guess > answer) {
NSLog(@"Lower!");
}
else if (guess < answer) {
NSLog(@"Higher!");
}
else {
NSLog(@"Correct! The answer was %i", answer);
}
} // end of while loop
NSLog(@"It took you %i tries", turn);
}
return 0;
}
Run your app and check out the latest changes!
Where To Go From Here?
By creating this small app, you've learned some of the most fundamental concepts in Objective-C, namely:
- functions
-
if
..else
blocks - format specifiers
- while loops
The final project with full source code can be found here.
You're now ready to move on to the next tutorial in this series, where you'll learn about some more fundamental concepts in Objective-C, including working with objects and classes.
If you have any question or comments, come join the discussion on this series in the forums!