Why conditional operator not work in java switch case? -


this question has answer here:

i following code

int cnt=1; switch(cnt){ case (cnt<=10): system.out.println("less 10");                 break; case (cnt<=20): system.out.println("less 20");                 break; case (cnt<=30): system.out.println("less 30");                 break; } 

there questions available problem. didn't got proper answer or answer not fulfill needs. got answers problem use multiple if else statements. want know why operators not work in java switch case?

that's not how switch/case statements work - in java or in various similar languages (c, c++, c#). it's not point of them.

switch/case statements aren't sequence of conditions - they're sequence of constant values (with associated code), along single expression evaluated, , code associated result of expression executed.

if want sequence of conditions, use if/else. there isn't other way, in java. there are languages have condition matching this, java isn't 1 of them. (note in such languages, need careful rules can happen if multiple conditions matched - in example, if cnt 0 example...)


Comments

Popular posts from this blog

apache - PHP Soap issue while content length is larger -

asynchronous - Python asyncio task got bad yield -

javascript - Complete OpenIDConnect auth when requesting via Ajax -