Controlling The Flow Of Your Program
Welcome to Colour Me Cocoa.
Looks like you're new here. If you like what you see, you may want to subscribe to my RSS feed, or if you prefer sign up to receive email updates.
Thanks for visiting!
Now that you have harnessed the power of loops to perform repetitive work easily in your program, it’s time to control the flow. Programs wouldn’t be very useful if they did the same thing every time you ran them and today I am going to show you how to take control.
One way to take control of your program is by using the If-Else statement. The basic syntax is below.
if (CONDITION) {
BODY
}
else If (CONDITION) {
BODY 2
}
else {
Body 3
}
You can have as many Else If statements between the first if and the final else as you want. You can even remove all the else statements and use the if by itself if you prefer.
Similarly to the for and while loops from last time we merely replace the CONDITION and BODY sections with the relevant code to complete the if-else statement. Below is the final code from the C loops tutorial.
If-Else
#include <stdio.h>
int main (int argc, const char * argv[]) {
// insert code here...
int i;
for (i = 0; i < 5; i++) {
printf("Hello, World!\n");
}
i = 0;
while (i < 5) {
printf("Hello, World!\n");
i++;
}
i = 0;
do {
printf("Hello, World!\n");
i++;
} while (i < 5);
return 0;
}
We are going to modify this code to run through another loop 3 times using the variable j, then, depending on the value of j we will print a statement 5 times.
#include <stdio.h>
int main (int argc, const char * argv[]) {
// insert code here...
int i;
int j;
for (j = 0; j < 3; j++) {
i = 0;
if (j == 0) {
for (i = 0; i < 5; i++) {
printf("j is equal to 0, this is a for loop\n");
}
}
else if (j == 1) {
while (i < 5) {
printf("j is equal to 1, this is a while loop\n");
i++;
}
}
else {
do {
printf("j is something else, this is a do-while loop\n");
i++;
} while (i < 5);
}
}
return 0;
}
The above code does just that! Open the MyFirst project in xCode and replace main.c with the code above. What you should see as the output when you run the code is each statement repeated 5 times. We have just used the if-else statement to control which loop is run based on the value of j.
One point of note in the above code is the use of double equals (==) in the else-if statements. The double equals is a comparison operator. It compares the values either side to see if they match. The single equals (=) is an assignment operator. If you use the single equals sign then you will set j to 0 instead of checking if j is eqaul to 0.
Try it out in xCode. You’ll notice that if you use just a single equals sign the first for loop will never exit as j is always being reset to 0. This also means that only the first if statement will run and your program will spit out the string “j is equal to 0, this is a for loop” for ever and ever. Use control + C to force the program to stop running.
Switch
Another way to control the flow of your program is by using the switch statement.
switch (EXPRESSION) {
case: CONSTANT-EXPRESSION
BODY
break;
default:
BODY 2
}
Above is the syntax for the switch statement. You can have as many case statements with their own bodies as your heart desires. The EXPRESSION section is where you place your controlling variable, in our case this is j. The CONSTANT-EXPRESSION is where you place a value for your EXPRESSION. The BODY is the same as previously.
There are two new statements used with switch. The break statement is the first one we come across, it does what it sounds like. It breaks out of the switch statement. If you leave it out then the next case statement will run regardless of j's value. You can use break elsewhere in your code, but it isn’t recommended as it makes the code harder to follow.
The second is the default statement. This statement is similar to the else statement in if-else. It will pick up any other value that has not been captured by a case statement.
The equivalent output to the if-else example can be obtained using the following switch statement:
#include <stdio.h>
int main (int argc, const char * argv[]) {
// insert code here...
int i;
int j;
for (j = 0; j < 3; j++) {
switch (j) {
case 0:
for (i = 0; i < 5; i++) {
printf("j is equal to 0, this is a for loop\n");
}
break;
case 1:
i = 0;
while (i < 5) {
printf("j is equal to 1, this is a while loop\n");
i++;
}
break;
default:
i = 0;
do {
printf("j is something else, this is a do-while loop\n");
i++;
} while (i < 5);
}
}
return 0;
}
Try it out in Xcode. Your program should display the same output.
So which is better? It’s up to you really. Certain logic (like a menu system) lends itself to a switch statement while other logic is easier to read if written as an if-else statement. I always try to maximize readability when writing code and you should to, it makes updating and maintaining your code a whole lot easier if you can figure out what is happening quickly.
While the code in this sample is not interactive, hopefully you can see past this and understand the power that these flow control statements give you. Once you learn how to change the value of j based on user input you can then direct the flow of the program to run a different section of code. Once again feel free to play with the code, break it, fix it, move it around and try out a few of the following:
- Remove the
breakstatements - Add
breakstatements to theif-elsestatements - Re-order the conditional statements
February 5th, 2007 at 12:26 am
Nice post. Can you explain why you’re using if (j == 0) { insteaf of if (j = 0) { in the if-else example?
February 5th, 2007 at 2:31 am
Hi Jim,
Sure can. I’ll edit the post to include the explanation. Basically the double equalts (==) used in the if example is a comparison operator. It compares the values either side to see if they match. The single equals (=) is an assignment operator. If you use the single equals sign then you will set j to 0 instead of checking if j is eqaul to 0.
Try it out in xCode. You’ll notice that if you use just a single equals sign the first for loop will never exit as j is always being reset to 0. This also means that only the first if statement will run and your program will spit out the string “j is equal to 0, this is a for loop” for ever and ever. Use control + C to force the program to stop running.