Status
Not open for further replies.

thinkingon

Right off the assembly line
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)?
 
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.
 

QwertyManiac

Commander in Chief
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

Journeyman
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.
 
Status
Not open for further replies.
Top Bottom