• 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

Question ID :952739442300

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why is the output i = 3?

class TestClass
{
public static void main(String args[])
{
boolean b = false;
int i = 1;
do
{
i++ ;
} while (b = !b);
System.out.println( i );
}
}
 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI
why is the output i = 3?
class TestClass
{
public static void main(String args[])
{
boolean b = false;
int i = 1;
do
{
i++ ;
} while (b = !b);
System.out.println( i );
}
}
look at the while option ! i++ is call the firte time (while always do) and (b = !b) (minstake maybe is ==, otherwise isn't booleam. made the loop again i =2 and ask again etc. when the option in the while is false jump out !
hope this help
 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<pre>
class TestClass
{
public static void main(String args[])
{
boolean b = false;
int i = 1;
do
{
i++ ;
} while (b = !b);
System.out.println( i );
}
}
</pre>
The catch here is "while (b = !b)" note the equal to sign. its not "==" but is a single "=".
Since its a do-while loop, the loop is executed atleast once..
so after the first iteration the value is 2.
Now whether the loop will get executed again depends on the result of "while (b = !b)"
The return value of "b = !b" is true, i.e the value on the 'Right hand side', which is !false , = true.
so the loop will be executed one more time, during which i is incremented again and the value of i at the end of loop is 3.
Now the condition "b = !b" is checked again.
The resulting value is false, coz b is true, !b is false,
and the value of "b = !b" is equal to the value on the 'Right hand side' of '=' , therefore false and therfore thats the end of loop.
Finally we have i value equal to 3.
HTH,
Rex

[This message has been edited by Rex Rock (edited July 11, 2001).]
[This message has been edited by Rex Rock (edited July 11, 2001).]
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
First loop b= false, inside while() b=!b, then b=true; loop again, inside while b=!b, b= false; end loop. i=3.
If b is boolean, b=b is ok for condition evaluating, not a mistake.
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
initial do loop, first entry
i = 2
b = true (this RETURNS "TRUE"?)
second iteration
i = 3
b = false (this RETURNS "FALSE"?)
If I'm understanding the explanations correctly, the above is what happens. And my question is: a boolean assignment actually returns a boolean?
In that case, would this be correct:
boolean b = TRUE;
boolean c = (b = FALSE); //supposedly, same as "boolean c=FALSE;"
 
Oh the stink of it! Smell my tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic