Hi Will,
Here is a little more about the 'switch' statement. You probably already have this down, but it helps me to review so please put up with my additon to WauloK's info...
1. The switch statement is a conditional statement like the if/else.
2. Use the switch statement when you have an integer expression to evaluate:
switch (integer_expression)
//This always must evaluate to an integer. For example you can't use a conditional expression within the switch( x<y)-->won't work. However switch (x+10) will work because it evaluates to an integer.
3. Use the switch when, for example, you want to evaluate the value of a variable called 'age' in a series of statements. So instead of:
if( age == 3)
else if (age== 6)
else if (age==18)
else if(age...)
Use:
switch(age)
{
case 3:
//each case indicates the value the integer to evaluate, in this case (pun intended) if age ==3 then enter the body of this case statement and print out "You are a toddler."
printf("You are a toddler.);
break;
case 18:
//everything following the case statement has to be a literal number or it must evaluate to a literal number. It can't be a variable. It can also be a #DEFINE.
printf("Now you can vote.")
break;
// causes the program to break out of the switch and continue on down the program. If break isn't put into the case, then the program will continue on down and execute the next case statement. the 'break' is vitally important.
default:
// if no case is matched, the default will always occur. It is essentially the same as an else statement in the if/else.
printf("Please enter your age: ");
break;
//if you don't have a default clause and no case is met, in this case, nothing would get printed at all.
}
As WauloK's example shows you, you can have an empty case:
switch(age) {
case1:
case2:
case3:
printf("You are a toddler");
break;
//the above code would get the printf statement in case3 if case1, case2 or case3 values were met.
OK. Whew, I hope this hasn't put you to sleep...