Assign a boolean value inside and "if" statement in Java? -
this question has answer here:
in java, looking @ code , noticed boolean
value assigned value of true inside if
statement. thought if
statement evaluate statement , not allow programmer set value. purpose of this? "take out dinner"
never reached, correct?
public class dinner { public static void main (string[] args) { boolean iskeeperfish = false; if (iskeeperfish = true) { system.out.println("fish dinner"); } else { system.out.println("take out dinner"); } } }
you test equality using double equals signs:
if(iskeeperfish == true) { }
you can use assignment inside if-clauses this:
int = 7; int b; if((b = + 5) == 12) { system.out.println(b); // print 12; }
though it's discouraged since looks bit messy , assignment easy miss.
Comments
Post a Comment