473,403 Members | 2,354 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

convert the switch case to if else

6
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
3 7575
donbock
2,426 Expert 2GB
Refer to the rules regarding posting homework.
May 5 '20 #2
AjayGohil
83 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
9,065 Expert Mod 8TB
Good and consistent indenting == readable/maintainable code people.
Oct 16 '20 #4

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

Similar topics

21
by: Andy | last post by:
Can someone tell me if the following Switch...Case construct is valid? I'm wanting to check for multiple values in the Case statement without explicitly listing each values. So for example, will...
10
by: Chih-Hsu Yen | last post by:
I encountered a strange problem about switch-case statement. switch(cmd) { case 1: statements; break; case 2: statements; break; ... .... case 11: S1; S2; S3; statements;
2
by: Tomas Larsson | last post by:
Hi. I have some problems when using a static readonly declaration of a guid in a switch/case statement. I'll give you an example. public sealed class Activites { private Activites(){}
15
by: Benny Raymond | last post by:
I'm confused as to how fallthrough is limited in switch. For example the following works: string switch_test = "a"; switch (switch_test) { case "a": case "b": case "c": doSomething(a);
6
by: graeme g | last post by:
hi how would i write the following in switch case statement: if (x < 40) y = 0; else if (x < 65) y = 1; else if (x < 80) y = 2;
5
by: sam_cit | last post by:
Hi Everyone, I read somewhere that there are some compile time operations behind switch-case, which is why it can work for cases which evaluates to an integer or character and not strings and...
8
by: eeffoc | last post by:
I've got a set of six radio buttons inside of a list box and what I'm trying to accomplish is generate a random number all with different ranges (Dice for d&d) depending on what radio button is...
13
by: Satya | last post by:
Hi everyone, This is the first time iam posting excuse me if iam making any mistake. My question is iam using a switch case statement in which i have around 100 case statements to compare. so...
2
by: Annalyzer | last post by:
My form looks like this: <form action="handle_event.php" method="POST" enctype="multipart/form-data"> <table id="event_edit" border="0"> <tr> <td> ...
1
by: jeepzhen | last post by:
Hi experts, I'm now working on a project to convert vb6 code to c#.net. I am facing a problem when using switch case in side a for loop which concatenating a constant value with for loop counter in...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.