• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Flow control!

 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
It's just like a fortune cookie, but instead of a cookie, it's pie. And we'll call it ... tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic