d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Booleans
Add Reply New Topic New Poll
Member
Posts: 10,049
Joined: Aug 11 2014
Gold: 540.00
Oct 20 2015 10:14pm
int x = -555;
boolean isNegative = (x < 0);

if (isNegative)
{
x = 100;
if (isNegative)
System.out.println("no");
else
System.out.println("yes");
}
else
System.out.println("maybe");

why is the answer ''no'' and not ''yes''?
The boolean ignores x=100?
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Oct 21 2015 03:53am
Code
boolean isNegative = (x < 0);


this is an assignment that only occurs once. the value stored here will stay until you re-assign it, regardless of the current value of x. if you want it to behave in such a way isNegative always looks at the current value of x, then you can create a method so it's re-evaluated every time it's called.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll