+ 9
You cant use condition in switch
1st Oct 2017, 6:11 PM
Mahdi Momeni
Mahdi Momeni - avatar
+ 3
Exactly: you can use condition expression in 'case' statement, but the 'switch' expression value will be compared to the value of each 'case' expression... Typically, switch-case statements are use to compare a single variable value to only once value at each case: switch (myvar) { case value: // do something if myvar == value // and run each next case block // until a 'break' keyword occurs // most of the time, each case block will end with it // in all cases, no more 'case' statement will be tested case result_of_expression: // do something if myvar == result of expression // as a condition is an expression, you can use one, but your 'myvar' will be compared necessarly to boolean value true or false } So, in your code, at first case statement, condition of nexted instruction execution is equivalent to: if (true == (l=0)) ... wich is false (and where you probably do a first mistake: you doesn't compared l to 0, but assign 0 to l... so the 'result' of the assignement-expression is the value of l, and 0 is not equivalent to true -- and almost, l is now equal to zero for next 'case' statements ;P). Anyway, if l is already equal to 0, the 'case' statement will always be false ^^ And if by magical it would be true, all different case block will be running, as you don't use 'break' keyword... The second 'case' statement could be writen: if (true == ((l>=0)&&(l<70000)) ... which is actually always true, as l is now equal to zero ^^ So Oyp is set to 6, but next to 5, then 4... and finally will carrying 1 at last 'case' block... if Oyp is declared outside the 'switch' scope (closure), else Oyp is unknown (undefined) after exiting the 'switch' closure ;)
1st Oct 2017, 11:41 PM
visph
visph - avatar
+ 1
you have to define oyp outside the switch scope
1st Oct 2017, 5:12 PM
Chrizzhigh
Chrizzhigh - avatar
0
can you mark it as answer ^^ and no probs keep on coding
1st Oct 2017, 5:14 PM
Chrizzhigh
Chrizzhigh - avatar