• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

simple question

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
please take a look at this question
What is the result of executing the following fragment of code:
boolean flag = false;
if (flag = true) {
System.out.println("true");
} else {
System.out.println("false");
}

a)true is printed to standard out
b)false is printed to standard out
c)An exception is raised
d)Nothing happens
when i complied it it gave me true
then chaanged the value of flag= true i still got the same print out "true " why ?? i also thot boolean can only take == and not an assignment = ??
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if(flag=true) will always yield true since we are dealing with an assignment (=) here and not a comparison (==). Thus flag is assigned true and the condition evaluates to the value of flag, which is true.
 
Tosin Adedoyin
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
now am lost even when flag= true in the initial declaration ? it is true is that what you mean ?? i dont understand
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes it is true.
The initial declaration has no effect if the condition expression is an assignment expression. In the if condition you state (flag=true), thus flag WILL be true and the first block of the if statement will be evaluated.
Make the difference between
if(flag=true) //condition will be true because flag is ASSIGNED true
and
if(flag==true) //condition may not be true depending on what value flag has been assigned before
 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The expression (flag=true) has two effects:
FIRST, since this is an assignment expression, the value on the right-hand side (true) is copied into the variable on the left-hand side (flag)
SECOND, the value of the expression is evaluated. The value of this expression is simply the value of the flag variable, ie, true, since it was just assigned this value.
Now when the if() statement checks the value, it will be true, since flag is true.
 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just adding,
if(boolean expression){
System.out.println("True");
}
else{
System.out.println("False");
}
The expression has to result in a boolean value and that boolean value is checked by the if statement to make a decision.
The following expressions,if used in the above example, would result in printing of "True"
1>boolean flag=true;if(flag)
2>boolean flag=false;if(flag=true)
3>boolean flag=false;if(flag?false;true)
4>int flag=1;if(flag==1)
The following would print false
int flag=1;if(flag=1)
int flag=1;if(flag)
The expression within the "if" braces and what it returns is what matters!
 
reply
    Bookmark Topic Watch Topic
  • New Topic