java - How to write nested ?: statements -
i shorten code use of ?:
(if-else-then) comparative operator instead of using traditional if{}else{}
blocks inconveniently tend take on screen. never taught operator, , appreciate regarding how nest multiple comparisons within 1 line.
this code shorten:
if(y<0) y=0; else if(y+h>s.getheight()) y = s.getheight()-h;
i managed condense each condition (not nested):
y = (y<0) ? 0 : y; y = (y+h>s.getheight()) ? s.getheight()-h : y;
is correct way nest it?
y = (y<0) ? 0 : ((y+h>s.getheight()) ? s.getheight()-h : y);
thank you!
edit: given link post pertaining ?: operator. link. however, question has nesting instead of simple if statement. therefore, question not duplicate of post.
yes, correct syntax it's not readable.
you can check in java. this:
int = 3; int b = 5; string s = (a < b) ? "less b" : (a > b) ? "more b" : "equal b"; system.out.println(s);
but code more readable if use if , if else statements. ? , : basic if statement. example:
int = 3; int b = 5; string s = (a == b) ? "equal" : "not equal" system.out.println(s);
but in case, rather use if statement. don't see ? , : instead of if statement :)
regards, golobic
Comments
Post a Comment