In our last article we talked about Arrays and Operators in C. In this article we will cover conditional statements like the If statement, the Else clause, and the Case statement. If there is time, we will also begin discussing how to work with loops.
Contributed by James Payne Rating: / 29 February 25, 2008
When writing programs, we often find ourselves having to respond to something the user has done. Conditionals work, at their very basic level, by stating: if the user does this, do that. It of course gets more complicated than that, but that is where we are going to begin.
Say you set your alarm at night. If it doesn't go off, you have to call your boss and explain why you are such a slacker. That's the basis of an If statement. If this, then that. Here it is in code:
#include <stdio.h>
#include <stdlib.h>
void main()
{
int yourMommasWeight=2000;
if(yourMommasWeight >120)
{
printf("nnnnWow your momma is FAAAAT!nnnnn");
}
}
The above code insults your fat momma. It does so by creating an integer variable named yourMommasWeight and assigning it the value 2000. We then say that if the value of yourMommasWeight is greater than 120, print some text. If the value is less than 120, nothing happens. However, we did make the value of the variable greater than 120, and this is the result:
Wow your momma is FAAAAT!
We could also write a program that compared your mom to my mom to see if your mom was fatter:
#include <stdio.h>
#include <stdlib.h>
void main()
{
int yourMommasWeight=2000;
int mymom=119;
if(yourMommasWeight >mymom)
{
printf("nnnnYep...yer mom is a big fat pig.nnnnn");
}
}
In the above example, we compare two variables: yourMommasWeight and mymom. Again, we set the value of the variables, and since your mother is fatter than mine, it prints out some text. If my mother had been fatter, nothing would have happened. We'll fix that shortly. However, first let's be a little fair and get some input from the user. I mean after all, I may be just a little biased.
#include <stdio.h>
#include <stdlib.h>
void main()
{
int yourmom;
int mymom;
printf("Enter your mother's weight: ");
scanf_s("%i",&yourmom);
printf("Enter my mother's weight: ");
scanf_s("%i",&mymom);
if(yourmom>mymom)
{
printf("nnnnYep...yer mom is a big fat pig.nnnnn");
}
}
In this program we create two variables, yourmom and mymom. We then ask the user to enter a number, and store that value in the variable yourmom. Next, we ask the user to give us another number, and store that in the variable mymom. It then compares the values in both variables, and if the value of yourmom is greater than mymom, it prints some text. If the value of mymom is greater than yourmom, nothing occurs.
Okay, let's make the program do something if by some crazy chance my mom is fatter than yours. For this we will need the Else Clause...
printf("nnnnYep...yer mom is a big fat pig.nnnnn");
}
else
{
printf("nnMy mother may be fatter...but yours is still uglyn");
}
}
This program works off of our good buddy the Else clause. Using else allows us to say: If this occurs do that, else do that. The above program works exactly as our previous program, only now if my mother is fatter than yours, it prints:
My mother may be fatter...but yours is still ugly
And of course if your mother had been fatter, it would have printed:
Yep...yer mom is a big fat pig.
Lastly, if neither condition had been true (ie; if the two values had been equal to one another), nothing would have occurred. Well never fear...we have a fix for that as well.
The Else...If allows you to evaluate a third condition. In this case, we are going to use it to help us make the program react if the two values are equal:
#include <stdio.h>
#include <stdlib.h>
void main()
{
int yourmom;
int mymom;
printf("Enter your mother's weight: ");
scanf_s("%i",&yourmom);
printf("Enter my mother's weight: ");
scanf_s("%i",&mymom);
if(yourmom>mymom)
{
printf("nnnnYep...yer mom is a big fat pig.nnnnn");
}
else if(yourmom==mymom)
{
printf("nnYou must be my long lost brother! God bless our fat
moms!n");
}
else
{
printf("nnMy mother may be fatter...but yours is still uglyn");
}
}
Now we have all our bases covered. If the mymom is greater than yourmom this prints out:
My mother may be fatter...but yours is still ugly
If yourmom is greater than mymom:
Yep...yer mom is a big fat pig.
And finally, if both our moms are equally as fat:
You must be my long lost brother! God bless our fat moms!
You are not limited to three conditions. With the Else...If we can code till the cows come home, which is forever, because all you care about are your needs. How many times have you given milk to your cows?
In this program, we will create rhymes based on the number a user enters in (ranged 0-5):
#include <stdio.h>
#include <stdlib.h>
void main()
{
int rhyme;
printf("Enter a number from 0-5: ");
scanf_s("%i",&rhyme);
if(rhyme==1)
{
printf("n One...a big fat bun.");
}
else if(rhyme==2)
{
printf("n Two...I hate you");
}
else if(rhyme==3)
{
printf("n Three...You look like Mr. T");
}
else if(rhyme==4)
{
printf("n Four...Don't vote for Al Gore");
}
else if(rhyme==2)
{
printf("n Five...Quit talking Jive");
}
else if(rhyme==0)
{
printf("n Zero...You know I'm yer hero");
}
else
{
printf("nThat number is not between 0-5 you dummy.");
}
}
In this program, we allow the user to pick a number from 0-5. The number they choose is stored in a variable named rhyme. We then go through a series of If and Else...If statements that asks whether the variable is equal to the numbers in the range 0-5. If it is, then it prints the appropriate line; if not, it moves on to the next statement. Finally, if the value is not within the range 0-5, it prints out:
That number is not between 0-5 you dummy.
If the number had been equal to say, three, this would have printed out:
As you can see from the above code, working with a boatload of If, Else and Else...Ifs can get not only time-consuming, but hard to read as well. To simplify matters, we can use the Case statement instead. Here is the same program as above, with the same results:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int rhyme;
printf("Enter a number from 0-5: ");
scanf_s("%i",&rhyme);
switch(rhyme)
{
case '1':
printf("n One...a big fat bun.");
break;
case '2':
printf("n Two...I hate you");
break;
case '3':
printf("n Three...You look like Mr. T");
break;
case '4':
printf("n Four...Don't vote for Al Gore");
break;
case '5':
printf("n Five...Quit talking Jive");
break;
case '0':
printf("n Zero...You know I'm yer hero");
break;
default:
printf("nThat number is not between 0-5 you dummy.");
}
return(0);
}
A lot better. It's easier to use, and saves the programmer a lot of coding.
We will cover the Case statement more in the Loops tutorial, which should poke its head around the corner in another article or two.
Wrapping It Up
You may have noticed that we focused on numeric values in all of our statements in this article. This was by design. Dealing with text is a little more complex in C than one would hope, as though some irate number-loving hippie accountant had created it. But never fear; we will cover this very topic in our next exciting episode. And hopefully we will discuss the basics of Logical Operators in Statements, Nesting Statements, and Loops, though that too may have to wait for a few more issues as well.
So stick around, because there is a lot of learning to do and let's face it: you need all the knowledge you can get.