This week's book giveaway is in the Security forum.We're giving away four copies of Penetration Testing Basics and have Ric Messier on-line!See this thread for details.
Win a copy of Penetration Testing Basics this week in the Security forum!

# Flow control!

Sridhar Srinivasan
Ranch Hand
Posts: 117
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

Ranch Hand
Posts: 40
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,

Gauri Savanur
Greenhorn
Posts: 9
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.