PDA

View Full Version : switch case in C


thinkingon
21-07-2008, 04:51 PM
hi every1..
how to implement constant expressions using switch statements in C?like we implement expressions using if statement

if(a<b+c) or if((a<b)&&(b<c))........

how can we do it using switch case(rather than implementing constant or character values)?

anuvrat_parashar
21-07-2008, 06:08 PM
Hello
This may disappoint u but switch case construct is not available in C(as per my knowledge). Its there in C++(I dont know much about C but am comfortable with C++). And in C++ switch case construct is used for constant conditions, it not used for the kind of statements u wish to use it for( oops another disappointment!!!). I hope u know the syntax of the switch case construct.

It accepts a value in the parenthesis of switch and executes the statements under the corresponding case label. If it does not find any then it executes the default statement.

nitish_mythology
21-07-2008, 07:01 PM
Switch cannot deal with ranges of value AFAIK. Only with (==) operator.

QwertyManiac
21-07-2008, 09:23 PM
Use a simple else-if ladder for expression-like conditions. Switch is made for jumping based on a value, else-if is more logical than switch here.

anshul
22-07-2008, 11:18 AM
Switch can be used only all the possible outcomes of the query are known and the value under comparison cannot be an expression but a constant.

And switch is there in the C library and can be used freely if you know what to do with it.