iOS For High School Students: Getting Started
This is a blog post by Connor Osborn, an aspiring student with a passion for creating software and 2d games! This is a new tutorial series on beginning programming for iOS – for high school students, by high school students! We’ll walk you through creating an app from the very beginning, assuming zero knowledge! And we’ll make […] By .
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
iOS For High School Students: Getting Started
35 mins
- Getting Started
- Understanding The Hello World Application
- Creating Our Own Application
- Variables
- Printing Statements
- Using Scanf For Input and Assigning Variables
- Programming with Logic: If Statements
- Solving Problematic Inputs with While Loops and Else Statements
- The Infinite While Loop
- Where To Go From Here?
Creating Our Own Application
The goal of the Are You A WIZARD? program is to tell you what class of person you are, based on your input. The program will ask questions that can be answered on a scale of 1-10.
But as the programmers, we will create the questions and responses that the computer will display! There is a lot of potential to make fun variations of this program for your friends to try. Let’s begin.
Starting with the template provided, let’s remove the NSLog function and the comment above it, since we don’t need them. We end up with this:
//
// main.m
// Are You A WIZARD?
//
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
[pool drain];
return 0;
}
Go ahead and run it again to make sure it still compiles and runs (but of course it won’t print out the “Hello, World” anymore since you deleted it!).
Variables
Now we need to create some variables.
Variables are essential because they make it significantly easier to manipulate information. They allow us to use a symbolic name that is independent of the information it represents, very similar to how variables are used in mathematics.
There are four basic types of variables, and for each variable we create, we need to tell our “brainless” computer what type it is. :p Here’s a brief run-down:
- int: stands for an integer variable; a whole number, a number that is not a fraction; examples: -2,-1,0,1,2
- float: stands for a floating-point variable; any number that is expressed with a decimal; examples: 3.5, 3.0, -.001
- double: stands for an extended floating-point variable; any number that exceeds the range of float; examples: 10000000000000000
- char: stands for any character variable; examples: x, a, b, d, $, #
We create variables by making the following type of declaration:
int x; //this basically means we created an integer variable named "x"
float bee; //this is a float variable named "bee"
So let’s create some variables for our program. We will use our variables to determine whether or not someone is a wizard.
You can create your own variables if you want. However, I strongly recommend working through this tutorial with the variables I provide, so that you don’t inadvertently create any errors!! Change ’em later, friends. :]
In the main function below, I’ve added the following variables: strength, intelligence, speed, alchemy_skill, sum, and avg. They are all float variables. Don’t worry for now what the variables mean.
//
// main.m
// Are You A WIZARD?
//
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
//These are the different variables that will be evaluated to generate responses:
float strength, intelligence, speed, alchemy_skill, sum, avg;
[pool drain];
return 0;
}
Notice how I declared all the variables in a single line, separating them with commas. But you could also write them like so:
float strength;
float intelligence;
float speed;
float alchemy_skill;
float sum;
float avg;
Notice also how I inserted a comment into my code with the // marks. It makes understanding the code a little easier, either for your forgetful self, or for other people reading your code! Trust me, somebody (possibly you!) will be glad you did it.
It’s not enough to just create variables, though. We have to assign them values. But sit tight, we will shortly.
Printing Statements
Now let’s create the questions we want the computer to ask the user. Let’s start with the following questions:
- What is your strength?
- What is your intelligence?
- What is your speed?
- What is your alchemy skill level?
Now we want to tell the computer to print these statements. Each of these statements is just a bunch of characters crammed together right? So, we use the NSLog function, the same one used in the Hello World application. All it does is print a string of characters. The format is as follows:
NSLog(@"Whatever you want to say. :p");
Easy, right? We would implement it like this in our program to code the above questions:
//
// main.m
// Are You A WIZARD?
//
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
//These are the different variables that will be evaluated to generate responses.
float strength, intelligence, speed, alchemy_skill, sum, avg;
//I included (1-10) so that users would know how to answer.
NSLog(@"What is your strength (1-10)?");
NSLog(@"What is your intelligence (1-10)?");
NSLog(@"What is your speed (1-10)?");
NSLog(@"What is your alchemy skill level (1-10)?");
[pool drain];
return 0;
}
Now let’s run the program and see what we’ve got!
Did it work? Then that’s it, you’re done!
Just kidding, let’s keep working. :]
Using Scanf For Input and Assigning Variables
The next big thing is that we want the user to be able to interact with the program. As it is, the program just spits out questions.
Fortunately, we have a function that specifically takes input from the keyboard, scanf(). The scanf() function takes an input from the user of the program and it stores it as a variable. We call this input the “argument.” Below is the syntax of the scanf() function.
scanf("%f", &strength);
This tells the computer: “Wait for the user to input something (the argument). The argument must be a float (that’s what “%f” means). Store that input as the value of the variable ‘strength’ (which we already created).”
In other words, the scanf() function is one way of assigning values to variables!
Realize that “%f” and “&” are just part of the syntax you will need to memorize. Because we are storing the input in a float variable, we use “%f.” If our input was being stored in an integer variable we would use “%i” instead. Below are the various operators used with their respective variable types.
Since we want to get input after every question, that means we’re going to need to… use scanf() after each NSLog()! Give it a try and check your program with mine below.
//
// main.m
// Are You A WIZARD?
//
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
//These are the different variables that will be evaluated to generate responses.
float strength, intelligence, speed, alchemy_skill, sum, avg;
//I included (1-10) so that users would know how to answer
NSLog(@"What is your strength (1-10)?");
scanf("%f", &strength);
NSLog(@"What is your intelligence (1-10)?");
scanf("%f", &intelligence);
NSLog(@"What is your speed (1-10)?");
scanf("%f", &speed);
NSLog(@"What is your alchemy skill level (1-10)?");
scanf("%f", &alchemy_skill);
[pool drain];
return 0;
}
So far, this program tells the computer the user’s strength, intelligence, speed, and alchemy skill. Those variables get assigned a value according to what the user types as input. However, we have some variables that were not assigned a value.
Remember, we created two additional variables:
float sum;
float avg;
We’re going to have our program compute sum and avg as combinations of the other four variables, which is why we didn’t use scanf() to assign them. We will define sum and avg as follows:
sum = strength + intelligence + speed + alchemy_skill;
avg = (strength + intelligence + speed + alchemy_skill)/4;
Important point: because sum and avg are being assigned values that are combinations of other variables, these other variables must be assigned a value first. So the two lines above must go after the last scanf() function. Your program should look like this:
//
// main.m
// Are You A WIZARD?
//
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
//These are the different variables that will be evaluated to generate responses.
float strength, intelligence, speed, alchemy_skill, sum, avg;
//I included (1-10) so that users would know how to answer
NSLog(@"What is your strength (1-10)?");
scanf("%f", &strength);
NSLog(@"What is your intelligence (1-10)?");
scanf("%f", &intelligence);
NSLog(@"What is your speed (1-10)?");
scanf("%f", &speed);
NSLog(@"What is your alchemy skill level (1-10)?");
scanf("%f", &alchemy_skill);
sum = strength + intelligence + speed + alchemy_skill;
avg = (strength + intelligence + speed + alchemy_skill)/4;
[pool drain];
return 0;
}