Quote (ferf @ Oct 17 2015 09:29am)
exactly my point
x = y * 2; ------is used inside brace (if statement), yet it's still prints x = 40, instead of x = 10...... Just like Y = 20 in if statement, yet can't use outside, YET x still = 40.... they're declared in the same if statement yet 1 works outside it and the other doesn't
why would you expect x = 10? are you perhaps confusing the = operator? keep in mind = is the
assignment operator, not an equation-equality statement like in math.
for example:
int firstNumber = 10; // firstNumber holds 10
int secondNumber = firstNumber + 1; // firstNumber holds 10, secondNumber holds 11
firstNumber = 50; // firstNumber holds 50, but secondNumber still holds 11, not 51
the equal sign = doesn't mean secondNumber will always contain a value 1 higher than firstNumber. the value firstNumber + 1 is computed at runtime and assigned to secondNumber. secondNumber will hold this value until you assign it a new one. exact same situation in your example, except the braces seem to be confusing you.
see how i specified the values in comments on a line-by-line basis? do that with your example to show me your reasoning.
This post was edited by carteblanche on Oct 17 2015 07:38am