Compare and contrast efficiency of nested if-else and switch case ,by an example? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Compare and contrast efficiency of nested if-else and switch case ,by an example?

29th Nov 2016, 1:10 PM
munish rastogi
munish rastogi - avatar
3 Answers
0
They are really very similar. However, it only depends on certain situations, Most of the time, if/else statements are fine, but they have no "fallthrough" like switches do. If you want it to execute some code like: int num; Switch (num){ Case 1: Cout << num << endl; Case 2: Cout << num << endl; This just means that if the value "1" is entered, it displays both 1 and 2. But if 2 is entered, only 2 is printed. This is just simpler to read (in my opinion, but if/else are easier to use). Like I said earlier, if you want to execute all the code below from a certain point: Switch, else if/else.
29th Nov 2016, 2:15 PM
PenguinBlast
PenguinBlast - avatar
0
Use switch statements for constant values, not conditionals (as switch statement do not support it). And use if-else for conditionals (use for constants too if it is more efficient, like if only finding one single value of a variable).
29th Nov 2016, 3:58 PM
Cohen Creber
Cohen Creber - avatar
0
They are raeally
11th Dec 2021, 4:30 AM
aman saifi