This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.

I get the answer as 61433 when I compile the following code: class BLBeck { public static void main(String args[]) { int x = 6; int success = 0; do { switch(x) { case 0: System.out.print("0"); x += 5; break; case 1: System.out.print("1"); x += 3; break; case 2: System.out.print("2"); x += 1; break; case 3: System.out.print("3"); success++; break; case 4: System.out.print("4"); x -= 1; break; case 5: System.out.print("5"); x -= 4; break; case 6: System.out.print("6"); x -= 5; break; } } while ((x != 3) || (success < 2)); }} I thoght that the answer is 614 Can anybody explain pl!Thanks

Hi, The answer as 61433 is correct This is becoz the while loop has an OR condition. while ((x != 3) || (success < 2)); after u reach 614, the value of x is 3 and that of sucess is 0. Now when the while condition is analysed it first checks for x!=3. This evaluates to a false since right now x is equal to 3. Now since there is an OR condition it will check for the true value of the second part. here sucess is 0 which is < 2 and hence sucess< 2 return a True value. so the do - while loop runs again and prints 3. It now increments the value of sucess to 1. Again the above logic holds and the program prints 3 with incrementing the value of Sucess to 2. Now both the parts of the while condition evaluate to a false and the program exits the while loop. Hence we get 61433 as the output Regards, Rads

The reason for outputting 61433 is that it's "OR" condition. So even though "x != 3" becomes false when value of x=3, being "OR" condition it will check for second condition "success < 2" which is true as the value of success is "0". So it goes again in the loop prints 3, increments the value of success which is "1" still the condition is true so it again goes inside the loop prints "3" again increments the value of success which is now "2". Now the condition "success < 2" is false as value of success is "2". Since both the "OR" conditions are false it will come out of the loop. Hope I clarify your doubt.

The answer will be 61433 and not 614, this because of the fact that there is ||(OR) operation between the two boolean expressions. Thus for the first three loops, which will print 614, first boolean expression (x != 3) returns true and since the operation is ||(OR) the second expression will not be evaluated, and in fourth loop the first boolean expression (x != 3) will return false thus the second boolean expression (success < 2)will be evaluted, which turns out to be true similerly for the fifth loop. Please note: For the ||(OR) operation, second operand will not be evaluated if first operand turns out true and whole expression will be ture. Again for && (AND) operation, second operand will not be evaluated if the first operand turns out false and whole expression will be false. However this not true with '|' and '&' bit operations, where both operand are evaluated irrespctive of the value of first operand. Hope this will clear your doubt to some extent. Thx

Arun (SCJP-1.4, IBM-141, SCWCD-1.4)
"How hard you try is rooted to how often you try" - Doug Hall"