Standard 'C' Functions

Non-platform specific questions.

Postby willg101 » Fri Apr 28, 2006 12:41 am

Very intersting thread...

I have a question though. How does the switch command work? I can't say I have ever seen it...
http://www.wish-games.com
Old betas now available!!
User avatar
willg101
 
Posts: 473
Joined: Thu Dec 16, 2004 12:08 am
Location: West Michigan
Score: 5 Give a positive score

Postby WauloK » Fri Apr 28, 2006 2:06 am

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
Available for Beta-testing.. PM me!
Black 16Gb iPhone 3GS
Windows 7: 2Gb RAM. Dell 9150.
WauloK
 
Posts: 42
Joined: Wed Apr 19, 2006 3:19 am
Location: Sydney, Aussieland
Score: 0 Give a positive score

Postby Just4Fun » Fri Apr 28, 2006 10:24 pm

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... :)
I've learned that I still have a lot to learn.

** Makslane == Genious **
Just4Fun
 
Posts: 524
Joined: Tue Jul 01, 2003 5:19 am
Location: USA: Washington State; West Coast
Score: 6 Give a positive score

Postby Fuzzy » Sat Apr 29, 2006 1:40 am

You explained that very well, J3F! Good job.
Mortal Enemy of IF....THEN(and Inspector Gadget)

Still ThreeFingerPete to tekdino
User avatar
Fuzzy
 
Posts: 1068
Joined: Thu Mar 03, 2005 9:32 am
Location: Plymostic Programmer
Score: 95 Give a positive score

Postby Just4Fun » Sun Apr 30, 2006 12:34 am

:oops: Thanks Pete... My meager programming confidence just went up a notch...
I've learned that I still have a lot to learn.

** Makslane == Genious **
Just4Fun
 
Posts: 524
Joined: Tue Jul 01, 2003 5:19 am
Location: USA: Washington State; West Coast
Score: 6 Give a positive score

Previous

Return to General

Who is online

Users browsing this forum: No registered users and 1 guest