+ 1
Compare and contrast efficiency of nested if-else and switch case ,by an example?
3 Antworten
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.
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).
0
They are raeally