467,168 Members | 1,027 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,168 developers. It's quick & easy.

convert the switch case to if else

Convert the following switch-case code to an if-else code.

Expand|Select|Wrap|Line Numbers
  1. switch ( i )
  2. {
  3. case 0:
  4.     n = 2;
  5.     break;
  6.  
  7. case 1:
  8.     n = 12;
  9.     break;
  10.  
  11. case 2:
  12.     n = 5 * 5;
  13.     break;
  14.  
  15. case 3:
  16.     n = 28;
  17.     break;
  18.  
  19. default:
  20.     n = 0;
  21.     break;
  22. }
May 4 '20 #1
  • viewed: 3765
Share:
3 Replies
Expert 2GB
Refer to the rules regarding posting homework.
May 5 '20 #2
64KB
Hey

simply ,you can use if-else leader and check condition in if statement.

Expand|Select|Wrap|Line Numbers
  1. if(i==0)
  2. {
  3.     n=2;
  4. }
  5. else if(i==1)
  6. {
  7.     n=12;
  8. }
  9. else if(i==2)
  10. {
  11.     n=5*5;
  12. }
  13. else if(i==3)
  14. {
  15.     n=28;
  16. }
  17. else
  18. {
  19.     n=0;
  20. }
Oct 14 '20 #3
Banfa
Expert Mod 8TB
Good and consistent indenting == readable/maintainable code people.
Oct 16 '20 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

10 posts views Thread by Chih-Hsu Yen | last post: by
2 posts views Thread by Tomas Larsson | last post: by
6 posts views Thread by graeme g | last post: by
5 posts views Thread by sam_cit@yahoo.co.in | last post: by
13 posts views Thread by Satya | last post: by
2 posts views Thread by Annalyzer | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.