aspose file tools*
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 EJB 3 in Action this week in the EJB and other Java EE Technologies 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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: If statement
 
Similar Threads
Hashcode and equals() coding
examlab(question-53)-unreachable statement NAO?
question about assertion
who can xplain these???
Assertion question from Dan's exam