0
16kviews
Explain difference between switch statement, ladder of if else and nested if else.
1 Answer
written 8.3 years ago by |
Sr. No | If else statement | Switch statement | Ladder if else statement | Nested if else statement |
---|---|---|---|---|
1. | We can test only one condition. | We can test multiple conditions. | We can test multiple conditions | We can test multiple conditions |
2. | We cannot,have different conditions. | We can have only one expression. | Conditions are independent to each other. | Conditions are independent to each other. |
3. | Used when there is only one condition and one value of the same is to be tested. | Used when there is only one condition and multiple values of the same is to be tested. | Used when there is multiple conditions are to be tested. | Used when there is multiple conditions are to be tested |
4. | Values are based on constraint. | Values are based on user choice | Values are based on constraint | Values are based on constraint |
5. | In this first condition is tested then it comes to else if the condition is not true | In this first condition is checked and then it switches to that case. | In this first condition is tested then it comes to else if the condition is not true and then the other conditions are tested. | In this first condition is tested then it comes to else if the condition is not true and then the other conditions are tested. |
6. | It is used to evaluate a condition to be true or false | It is used to test multiple values of the same variable or expression like 1, 2 3 etc. | It is used to evaluate multiple conditions to be true or false and accordingly perform various tasks according to the correct condition | It is used to evaluate multiple conditions based on previous condition to be true to decide whether to perform a task or not. |