Turn Switch statement into if/then and if/else statement
up vote
0
down vote
favorite
I want to make this switch statement into if/else and if/then statement.
My Switch statement is :
char option;
printf("Choose your option : ");
scanf("%c",&option);
switch(option)
case 'a':
case 'A': a = 20 + 10 ;
printf("Addition process result:%d",a);
break;
case 'b':
case 'B': a = 20 - 10 ;
printf("Subtraction process result:%d",a);
break;
case 'c':
case 'C': a = 20 * 10 ;
printf("Multiplication process result:%d",a);
break;
case 'd':
case 'D': a = 20 + 10 ;
printf("Division process result:%d",a);
break;
default:
printf("Invalid option");
c if-statement if-this-then-that
|
show 1 more comment
up vote
0
down vote
favorite
I want to make this switch statement into if/else and if/then statement.
My Switch statement is :
char option;
printf("Choose your option : ");
scanf("%c",&option);
switch(option)
case 'a':
case 'A': a = 20 + 10 ;
printf("Addition process result:%d",a);
break;
case 'b':
case 'B': a = 20 - 10 ;
printf("Subtraction process result:%d",a);
break;
case 'c':
case 'C': a = 20 * 10 ;
printf("Multiplication process result:%d",a);
break;
case 'd':
case 'D': a = 20 + 10 ;
printf("Division process result:%d",a);
break;
default:
printf("Invalid option");
c if-statement if-this-then-that
3
I won't answer, because I think your code works better with aswitchstatement.
– Tim Biegeleisen
Nov 8 at 16:53
3
Make an attempt. If you can't get it working, show us what you've tried.
– dbush
Nov 8 at 16:56
1
There is no if/then statement in C.
– Steve Summit
Nov 8 at 17:16
1
Why do you want to convert to "if/then" or "if/else" statement? What do you think, such a statement looks like? Please show us an example. BTW: There is nothenin C
– Gerhardh
Nov 8 at 17:33
It would improve your code more if you usedtoupper(option)and only use one value per case, than turning everything inside out to use "if/else"
– Gerhardh
Nov 8 at 17:34
|
show 1 more comment
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I want to make this switch statement into if/else and if/then statement.
My Switch statement is :
char option;
printf("Choose your option : ");
scanf("%c",&option);
switch(option)
case 'a':
case 'A': a = 20 + 10 ;
printf("Addition process result:%d",a);
break;
case 'b':
case 'B': a = 20 - 10 ;
printf("Subtraction process result:%d",a);
break;
case 'c':
case 'C': a = 20 * 10 ;
printf("Multiplication process result:%d",a);
break;
case 'd':
case 'D': a = 20 + 10 ;
printf("Division process result:%d",a);
break;
default:
printf("Invalid option");
c if-statement if-this-then-that
I want to make this switch statement into if/else and if/then statement.
My Switch statement is :
char option;
printf("Choose your option : ");
scanf("%c",&option);
switch(option)
case 'a':
case 'A': a = 20 + 10 ;
printf("Addition process result:%d",a);
break;
case 'b':
case 'B': a = 20 - 10 ;
printf("Subtraction process result:%d",a);
break;
case 'c':
case 'C': a = 20 * 10 ;
printf("Multiplication process result:%d",a);
break;
case 'd':
case 'D': a = 20 + 10 ;
printf("Division process result:%d",a);
break;
default:
printf("Invalid option");
c if-statement if-this-then-that
c if-statement if-this-then-that
asked Nov 8 at 16:50
singlepiece000
125
125
3
I won't answer, because I think your code works better with aswitchstatement.
– Tim Biegeleisen
Nov 8 at 16:53
3
Make an attempt. If you can't get it working, show us what you've tried.
– dbush
Nov 8 at 16:56
1
There is no if/then statement in C.
– Steve Summit
Nov 8 at 17:16
1
Why do you want to convert to "if/then" or "if/else" statement? What do you think, such a statement looks like? Please show us an example. BTW: There is nothenin C
– Gerhardh
Nov 8 at 17:33
It would improve your code more if you usedtoupper(option)and only use one value per case, than turning everything inside out to use "if/else"
– Gerhardh
Nov 8 at 17:34
|
show 1 more comment
3
I won't answer, because I think your code works better with aswitchstatement.
– Tim Biegeleisen
Nov 8 at 16:53
3
Make an attempt. If you can't get it working, show us what you've tried.
– dbush
Nov 8 at 16:56
1
There is no if/then statement in C.
– Steve Summit
Nov 8 at 17:16
1
Why do you want to convert to "if/then" or "if/else" statement? What do you think, such a statement looks like? Please show us an example. BTW: There is nothenin C
– Gerhardh
Nov 8 at 17:33
It would improve your code more if you usedtoupper(option)and only use one value per case, than turning everything inside out to use "if/else"
– Gerhardh
Nov 8 at 17:34
3
3
I won't answer, because I think your code works better with a
switch statement.– Tim Biegeleisen
Nov 8 at 16:53
I won't answer, because I think your code works better with a
switch statement.– Tim Biegeleisen
Nov 8 at 16:53
3
3
Make an attempt. If you can't get it working, show us what you've tried.
– dbush
Nov 8 at 16:56
Make an attempt. If you can't get it working, show us what you've tried.
– dbush
Nov 8 at 16:56
1
1
There is no if/then statement in C.
– Steve Summit
Nov 8 at 17:16
There is no if/then statement in C.
– Steve Summit
Nov 8 at 17:16
1
1
Why do you want to convert to "if/then" or "if/else" statement? What do you think, such a statement looks like? Please show us an example. BTW: There is no
then in C– Gerhardh
Nov 8 at 17:33
Why do you want to convert to "if/then" or "if/else" statement? What do you think, such a statement looks like? Please show us an example. BTW: There is no
then in C– Gerhardh
Nov 8 at 17:33
It would improve your code more if you used
toupper(option) and only use one value per case, than turning everything inside out to use "if/else"– Gerhardh
Nov 8 at 17:34
It would improve your code more if you used
toupper(option) and only use one value per case, than turning everything inside out to use "if/else"– Gerhardh
Nov 8 at 17:34
|
show 1 more comment
2 Answers
2
active
oldest
votes
up vote
1
down vote
like this:
#include <stdio.h>
int main()
how to convert in if/then statement ?
– singlepiece000
Nov 8 at 17:03
@singlepiece000 What isif/then statement?
– Yunbin Liu
Nov 8 at 17:08
1
@YunbinLiu He doesn't know. He copied those words -- without understanding them, and perhaps inaccurately -- from his homework assignment, which you've just tried to do for him.
– Steve Summit
Nov 8 at 17:12
add a comment |
up vote
1
down vote
You just do:
if(option == 'a' || option == 'A')
// do whatever
else if (option == 'b' || option == 'B')
// do whatever
... the other else if's
then for the "invalid option", you have just an else . If the first if or any of the subsequent else if's evaluate true, then all the others will be skipped.
ok, I figure out if/else. But how to turn into if/then statement?
– singlepiece000
Nov 8 at 17:01
@singlepiece000 What exactly does that mean? If you made a change to your code, compile it and run it to test it.
– dbush
Nov 8 at 17:02
@dbush What it means is that those words "I want to make this switch statement into if/else and if/then statement" are the OP's uncomprehending paraphrase of a homework assignment. Perhaps the assignment was misleading, or perhaps something got lost in translation.
– Steve Summit
Nov 8 at 17:15
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
like this:
#include <stdio.h>
int main()
how to convert in if/then statement ?
– singlepiece000
Nov 8 at 17:03
@singlepiece000 What isif/then statement?
– Yunbin Liu
Nov 8 at 17:08
1
@YunbinLiu He doesn't know. He copied those words -- without understanding them, and perhaps inaccurately -- from his homework assignment, which you've just tried to do for him.
– Steve Summit
Nov 8 at 17:12
add a comment |
up vote
1
down vote
like this:
#include <stdio.h>
int main()
how to convert in if/then statement ?
– singlepiece000
Nov 8 at 17:03
@singlepiece000 What isif/then statement?
– Yunbin Liu
Nov 8 at 17:08
1
@YunbinLiu He doesn't know. He copied those words -- without understanding them, and perhaps inaccurately -- from his homework assignment, which you've just tried to do for him.
– Steve Summit
Nov 8 at 17:12
add a comment |
up vote
1
down vote
up vote
1
down vote
like this:
#include <stdio.h>
int main()
like this:
#include <stdio.h>
int main()
answered Nov 8 at 17:00
Yunbin Liu
930210
930210
how to convert in if/then statement ?
– singlepiece000
Nov 8 at 17:03
@singlepiece000 What isif/then statement?
– Yunbin Liu
Nov 8 at 17:08
1
@YunbinLiu He doesn't know. He copied those words -- without understanding them, and perhaps inaccurately -- from his homework assignment, which you've just tried to do for him.
– Steve Summit
Nov 8 at 17:12
add a comment |
how to convert in if/then statement ?
– singlepiece000
Nov 8 at 17:03
@singlepiece000 What isif/then statement?
– Yunbin Liu
Nov 8 at 17:08
1
@YunbinLiu He doesn't know. He copied those words -- without understanding them, and perhaps inaccurately -- from his homework assignment, which you've just tried to do for him.
– Steve Summit
Nov 8 at 17:12
how to convert in if/then statement ?
– singlepiece000
Nov 8 at 17:03
how to convert in if/then statement ?
– singlepiece000
Nov 8 at 17:03
@singlepiece000 What is
if/then statement?– Yunbin Liu
Nov 8 at 17:08
@singlepiece000 What is
if/then statement?– Yunbin Liu
Nov 8 at 17:08
1
1
@YunbinLiu He doesn't know. He copied those words -- without understanding them, and perhaps inaccurately -- from his homework assignment, which you've just tried to do for him.
– Steve Summit
Nov 8 at 17:12
@YunbinLiu He doesn't know. He copied those words -- without understanding them, and perhaps inaccurately -- from his homework assignment, which you've just tried to do for him.
– Steve Summit
Nov 8 at 17:12
add a comment |
up vote
1
down vote
You just do:
if(option == 'a' || option == 'A')
// do whatever
else if (option == 'b' || option == 'B')
// do whatever
... the other else if's
then for the "invalid option", you have just an else . If the first if or any of the subsequent else if's evaluate true, then all the others will be skipped.
ok, I figure out if/else. But how to turn into if/then statement?
– singlepiece000
Nov 8 at 17:01
@singlepiece000 What exactly does that mean? If you made a change to your code, compile it and run it to test it.
– dbush
Nov 8 at 17:02
@dbush What it means is that those words "I want to make this switch statement into if/else and if/then statement" are the OP's uncomprehending paraphrase of a homework assignment. Perhaps the assignment was misleading, or perhaps something got lost in translation.
– Steve Summit
Nov 8 at 17:15
add a comment |
up vote
1
down vote
You just do:
if(option == 'a' || option == 'A')
// do whatever
else if (option == 'b' || option == 'B')
// do whatever
... the other else if's
then for the "invalid option", you have just an else . If the first if or any of the subsequent else if's evaluate true, then all the others will be skipped.
ok, I figure out if/else. But how to turn into if/then statement?
– singlepiece000
Nov 8 at 17:01
@singlepiece000 What exactly does that mean? If you made a change to your code, compile it and run it to test it.
– dbush
Nov 8 at 17:02
@dbush What it means is that those words "I want to make this switch statement into if/else and if/then statement" are the OP's uncomprehending paraphrase of a homework assignment. Perhaps the assignment was misleading, or perhaps something got lost in translation.
– Steve Summit
Nov 8 at 17:15
add a comment |
up vote
1
down vote
up vote
1
down vote
You just do:
if(option == 'a' || option == 'A')
// do whatever
else if (option == 'b' || option == 'B')
// do whatever
... the other else if's
then for the "invalid option", you have just an else . If the first if or any of the subsequent else if's evaluate true, then all the others will be skipped.
You just do:
if(option == 'a' || option == 'A')
// do whatever
else if (option == 'b' || option == 'B')
// do whatever
... the other else if's
then for the "invalid option", you have just an else . If the first if or any of the subsequent else if's evaluate true, then all the others will be skipped.
edited Nov 8 at 17:15
answered Nov 8 at 16:56
Austin Stephens
41149
41149
ok, I figure out if/else. But how to turn into if/then statement?
– singlepiece000
Nov 8 at 17:01
@singlepiece000 What exactly does that mean? If you made a change to your code, compile it and run it to test it.
– dbush
Nov 8 at 17:02
@dbush What it means is that those words "I want to make this switch statement into if/else and if/then statement" are the OP's uncomprehending paraphrase of a homework assignment. Perhaps the assignment was misleading, or perhaps something got lost in translation.
– Steve Summit
Nov 8 at 17:15
add a comment |
ok, I figure out if/else. But how to turn into if/then statement?
– singlepiece000
Nov 8 at 17:01
@singlepiece000 What exactly does that mean? If you made a change to your code, compile it and run it to test it.
– dbush
Nov 8 at 17:02
@dbush What it means is that those words "I want to make this switch statement into if/else and if/then statement" are the OP's uncomprehending paraphrase of a homework assignment. Perhaps the assignment was misleading, or perhaps something got lost in translation.
– Steve Summit
Nov 8 at 17:15
ok, I figure out if/else. But how to turn into if/then statement?
– singlepiece000
Nov 8 at 17:01
ok, I figure out if/else. But how to turn into if/then statement?
– singlepiece000
Nov 8 at 17:01
@singlepiece000 What exactly does that mean? If you made a change to your code, compile it and run it to test it.
– dbush
Nov 8 at 17:02
@singlepiece000 What exactly does that mean? If you made a change to your code, compile it and run it to test it.
– dbush
Nov 8 at 17:02
@dbush What it means is that those words "I want to make this switch statement into if/else and if/then statement" are the OP's uncomprehending paraphrase of a homework assignment. Perhaps the assignment was misleading, or perhaps something got lost in translation.
– Steve Summit
Nov 8 at 17:15
@dbush What it means is that those words "I want to make this switch statement into if/else and if/then statement" are the OP's uncomprehending paraphrase of a homework assignment. Perhaps the assignment was misleading, or perhaps something got lost in translation.
– Steve Summit
Nov 8 at 17:15
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53212453%2fturn-switch-statement-into-if-then-and-if-else-statement%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
I won't answer, because I think your code works better with a
switchstatement.– Tim Biegeleisen
Nov 8 at 16:53
3
Make an attempt. If you can't get it working, show us what you've tried.
– dbush
Nov 8 at 16:56
1
There is no if/then statement in C.
– Steve Summit
Nov 8 at 17:16
1
Why do you want to convert to "if/then" or "if/else" statement? What do you think, such a statement looks like? Please show us an example. BTW: There is no
thenin C– Gerhardh
Nov 8 at 17:33
It would improve your code more if you used
toupper(option)and only use one value per case, than turning everything inside out to use "if/else"– Gerhardh
Nov 8 at 17:34