DIGIT-ARC read-only archive · snapshot: July 23, 2026

switch case in C

5 posts · originally from https://geek.digit.in/community/threads/switch-case-in-c.93637/

T
thinkingon Original post #1
Jul 21, 2008 at 4: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)?
A
Jul 21, 2008 at 6: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
Jul 21, 2008 at 7:01 PM
Switch cannot deal with ranges of value AFAIK. Only with (==) operator.
QwertyManiac
Jul 21, 2008 at 9: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
anshul #5
Jul 22, 2008 at 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.