Question:
When is a switch statement better than multiple if statements?
Answer:
The switch statement is better than multiple if statements when there are more than two alternatives to be selected whether the case value matches to the variable of either character or integer type. Source: CoolInterview.com
IN CASE OF A SWITCH STATEMENT, THE CURSOR STRAIGHT AWAY JUMPS TO THE REQUIRED CASE AND OVERLOOKS OTHER STATEMENTS. AS SOON AS THE EXECUTION IS OVER IT MOVES OUT OF THE OTHER IRREVALENT STEPS PERTAINING TO THE REQUIRED CONDITION BY THE HELP OF "break" STATEMENT,THEREBY REDUCING THE TIME COMPLEXITY OF THE PROGRAM. WHEREAS "if" STAEMENT IS USEFUL WHEN THE NUMBER OF CONDITIONS TO BE CHECKED IS LESS. Source: CoolInterview.com
Answered by: ANIRUDDHA SANYAL | Date: 9/22/2007
| Contact ANIRUDDHA SANYAL
In menu-driven programs. Source: CoolInterview.com
Answered by: ranu | Date: 3/12/2008
| Contact ranu
If you have the better answer, then send it to us. We will display your answer after the approval.
Rules to Post Answers in CoolInterview.com:-
- There should not be any Spelling Mistakes.
- There should not be any Gramatical Errors.
- Answers must not contain any bad words.
- Answers should not be the repeat of same answer, already approved.
- Answer should be complete in itself.
|