Page 2 of 2

PostPosted: Fri Apr 28, 2006 12:41 am
by willg101
Very intersting thread...

I have a question though. How does the switch command work? I can't say I have ever seen it...

PostPosted: Fri Apr 28, 2006 2:06 am
by WauloK
Code: Select all
switch(n) {
    case 0:
      printf("You typed zero.\n");
      break;
    case 3:
    case 5:
    case 7:
      printf("n is a prime number\n");
      break;
    case 2: printf("n is a prime number\n");
    case 4:
    case 6:
    case 8:
      printf("n is an even number\n");
      break;
    case 1:
    case 9:
      printf("n is a perfect square\n");
      break;
    default:
      printf("Only single-digit numbers are allowed\n");
}



http://en.wikipedia.org/wiki/Switch_statement

PostPosted: Fri Apr 28, 2006 10:24 pm
by Just4Fun
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... :)

PostPosted: Sat Apr 29, 2006 1:40 am
by Fuzzy
You explained that very well, J3F! Good job.

PostPosted: Sun Apr 30, 2006 12:34 am
by Just4Fun
:oops: Thanks Pete... My meager programming confidence just went up a notch...