what is the different between if-else and switch statment
(other than syntax)

Answer Posted / mikew

The switch branches on one value only, whereas the if-else
tests multiple logical expressions.

So you could say that the switch is a subset of if-else.

The potential difference if that switch is conceptually an
N-way branch point, whereas the if-else is always a
(repeated) binary branch.

However, if you are checking, say, a return code, against a
varied list of possibilities, then the switch can give
greater clarity to source code - and allow simpler addition
of new cases, making it easier for maintenance, as well as
allowing the compiler to generate simpler code.

Simpler code is possible because it can generate a jump
table to perform the multiple comparisons, i.e. in pseudo-code:

_jump_table:
DEFW case1_address
DEFW case1_value
...
DEFW caseN_address
DEFW caseN_value
DEFW NULL /* terminator */

- search _jump_table for case_value
- branch to corresponding case_address, or take default
action if NULL terminator found instead.

In the special case that case1 ... caseN values are
sequential numbers (maybe with a few gaps) then the table
and code can be further simplified as a simple indexed branch:

_jump_table:
DEFW case1_address
...
DEFW default_address /* fill in any holes !*/
...
DEFW caseN_address

- check value is between case1_value and caseN_value
- subtract case1_value
- load branch address word from _jump_table word-indexed on
previous result

Is This Answer Correct ?    27 Yes 14 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What are the 4 types of programming language?

584


What does %p mean c?

633


Explain how can you avoid including a header more than once?

604


What is scope rule of function in c?

553


Is null equal to 0 in sql?

656






can any one provide me the notes of data structure for ignou cs-62 paper

1707


What are the uses of a pointer?

684


What is pointer in c?

743


What is the modulus operator?

738


Can two or more operators such as and be combined in a single line of program code?

812


Input is "rama loves rajesh and rajesh Loves rama also and rajesh wear gloves and bloves" To print output is count the numbers of times repeted the word love without case sensitive.

1597


the maximum length of a character constant can be a) 1 character b) 8 characters c) 256 chaacters d) 125 characters

1803


Tell me what is the purpose of 'register' keyword in c language?

620


What is a nested formula?

607


What is assert and when would I use it?

581