简体   繁体   中英

Can all if statements be converted to equivalent switch statements?

I think that all if statements like this

if(condition){statements;}

can be converted to a switch statement like this

switch(!(same condition as in if)){case 0:statements;}

Am I correct or is there some exception that cannot be converted from if to switch ?

Any condition within a if() statement evaluates to either 0 or 1 (it is actually any value other than 0).
So writing it in switch() statement is equivalent to writing switch(a) , where a can be either 1 or 0 .

So, you are absolutely correct!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM