Tech Support Guy banner
  • Please post in our Community Feedback thread for help with the new forum software! If you are having trouble logging in, please Contact Us for assistance.
Status
Not open for further replies.
1 - 3 of 3 Posts

·
Registered
Joined
·
41 Posts
Discussion Starter · #1 ·
How would I convert this to a if/else statment

int color;

printf(“Enter 1 for red, 2 for green, 2 for yellow, or 3 for blue.\n”);
scanf ( “%d”, &color);

switch(color)
{
case 0:
printf(“Red");
case 1:
printf(“Green");
break;
case 2:
printf(“Yellow");
case 3:
printf(“Blue");
default:
printf(“Invalid Color");
}
 

·
Registered
Joined
·
551 Posts
Does C support the "else if" or "elseif" statement? (I know C++ does). If so, it's a simple bit of code:
Code:
if (color == 0) {
printf "red";
} else if (color == 1) {
printf "Green";
}
etc...

Or, you could use embedded if statements:
Code:
if (color == 0) {
printf "Red";
} else {
if (color == 1) {
printf "Green";
} else {
if (color == 2) {
printf "Yellow";
}
}
}
 
1 - 3 of 3 Posts
Status
Not open for further replies.
Top