I'm using the below free text to learn intro to java and I am having trouble understanding the difference between the code segments:
http://math.hws.edu/eck/cs124/downloads/javanotes7-linked.pdf
Example 1
int x;
x = -1;
if (x < 0)
x = 1;
else
x = 2;
Example 2
int x;
x = -1;
if (x < 0)
x = 1;
if (x >= 0)
x = 2;
In Example 1, x is 1; In Example 2, x is 2.
On the right, if -1 is not > or = to 0 then shouldn't the output be 1? Could someone please explain why the output would instead be 2?
ifstatements operate by saying that the two are not equivalent even if they can look the same to a new programmer. The author's point being (coincidentally) illustrated by OP being confused at what the difference would be.