• Post Reply Bookmark Topic Watch Topic
  • New Topic

Do While Loop.  RSS feed

 
Suresh Rajadurai
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • 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
 
Sahid Khan
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • 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,
 
Srikanth Basa
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check this http://www.janeg.ca/scjp/oper/ternary.html
 
raghu nagabandi
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey just he confudsed you by giving like that.
(flag)?true:false equillent to flag itself
 
Ganesh Kumar
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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.
 
puppy Lee
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(flag)? true:false
means if flag==true then "while(true)";
if flag==false then "while(false)"
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!