This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes If statement Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "If statement" Watch "If statement" New topic
Author

If statement

geeta rai
Ranch Hand

Joined: Sep 18, 2003
Posts: 48
Why is 'A' printed in the following code?
class test
{
static boolean b;
public static void main(String[] args)
{
System.out.println("The value of b is "+ b);

if (b = true) //1
{
System.out.print("A");
}
else if (b) //2
{
System.out.print("B");
}
else if (!b)
{
System.out.print("C");
}
else
{
System.out.print("D");
}
}
}
I think after assigment of b to true at the statement //1, the control should go to //2 and B should be printed.
shankar vembu
Ranch Hand

Joined: May 10, 2001
Posts: 309
Originally posted by geeta rai:
I think after assigment of b to true at the statement //1, the control should go to //2 and B should be printed.

in the above code, ur assigning true to b, and therefore the line 1 evaluates to if(true) and so it enters in the section where A is printed.
regards.
[ November 22, 2003: Message edited by: shankar vembu ]
geeta rai
Ranch Hand

Joined: Sep 18, 2003
Posts: 48
Thanks shankar, that was so simple
 
Consider Paul's rocket mass heater.
 
subject: If statement