+ 6

What is the difference between switch case and if else if?

6th Jul 2018, 7:28 PM
Anamta Fatima
Anamta Fatima - avatar
11 Réponses
+ 13
In switch case the condition should be an exact match to be true while in if else if you can even give range as condition :-)
6th Jul 2018, 7:31 PM
Anamta Fatima
Anamta Fatima - avatar
+ 5
I think switch-case statements are more specific to condtions. though they are completely interchangeable
6th Jul 2018, 10:50 PM
Mr. Bot
Mr. Bot - avatar
+ 3
In switch case, block of code runs on the basis of matching of values described in cases for a variable or expression while in if .. else if, code is executed on the basis of conditions. one more difference is that, break is used in switch to prevent the fall through so you can avoid writing the break and the swich can work for multiple cide blocks for different values after the correct matching but in if.. else if . only one block of code will be executed (u can have multiple conditions in one else if case, but then too only one block wil be exected). eg. if you are asked to print table from certain number to the table of 10 , you can use switch cases for all ten numbers from 1 to 10 without break... and on the basis of value, it will print all tables from that number to 10.... this is not possible in if.. else if..
8th Jul 2018, 12:07 PM
Jain Rishav Amit
Jain Rishav Amit - avatar
+ 2
just for readability but above 10 cases switch is not suitable
6th Jul 2018, 9:04 PM
grzesiekmq
grzesiekmq - avatar
+ 2
i think in if else multiple conditions are checked and in switch only one condition is checked and i had read it somewhere that switch is better that if-else, i may be wrong
8th Jul 2018, 3:49 PM
Tarika
Tarika - avatar
+ 1
😐😐😐
6th Jul 2018, 8:07 PM
Sad
Sad - avatar
+ 1
In general "switch" is faster, but variants are limited to set of values of the same type. With "if" we can logically combine expressions of any kind.
7th Jul 2018, 7:08 PM
Микола Федосєєв
Микола Федосєєв - avatar
+ 1
What is the difference between switch case and if else if? (in c++)
8th Jul 2018, 5:14 AM
Haidar Bazzoun
Haidar Bazzoun - avatar
+ 1
To check different (rather complex) conditions you should use if-else statements. Whereas switch-case statements are better if you need to make different decisions based on one value.
18th Jul 2018, 8:41 PM
t33l0
+ 1
switch case is the case is processed and the break is appear then it switched to another ... if else is must defines the all condition...
16th Aug 2018, 12:46 AM
Naveen M
Naveen M - avatar
+ 1
1. Switch case is used to replace multiple if else if statements. 2. Writing if else if statements as per our need( many if else if) is a time consuming way... but with this switch case we can save our time.
1st Sep 2018, 12:52 PM
Anshika Sharma
Anshika Sharma - avatar