• 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

Do While Loop.

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am just working on some mock exams. I came across a question, which has a while loop like this:

boolean flag = false
do
{
//set of commands to execute
}
while ( (flag)? true:false);


Please let me know the meaning of "(flag)? true:false"

Thanks
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I hope you know, ?: is a conditional ternary operator in java. It uses boolean value of the first operand to decide which of two other expressions should be evaluated. If first expression is true, second expression is evaluated, otherwise third one gets evaluated.

So (flag)? true:false is equivalent to just flag.

HTH,
 
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check this http://www.janeg.ca/scjp/oper/ternary.html
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey just he confudsed you by giving like that.
(flag)?true:false equillent to flag itself
 
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi suresh,

(Condition)?1:2;

Lets say a situation prevails like above.If the condition is "TRUE" the thing in '1' will be executed if it is false the thing in the '2' will be executed.

Example:
i=5;
(if==9) ? "equal":"not equal";

Now the condition is false so "Not equal" will be the output for this.


In your case


boolean flag = false *flag=false is initialized .*
do
{
//set of commands to execute
}
while ( (flag)? true:false); **Checks whether condition (flag) is true.It is not as it is initialized to false.So the 2 statement is executed.Which is "False " in your case.


I hope you got it





Ganesh Kumar
 
Suresh Rajadurai
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Gurus,

thank you so much. I got the exact picture of this. I appreciate your reply. Thanks once again.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Suresh",

Please check your private messages. You can see them by clicking "My Profile" at the top right of the page.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
(flag)? true:false
means if flag==true then "while(true)";
if flag==false then "while(false)"
 
yeah, but ... what would PIE do? Especially concerning this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic