• 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

If statement

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 309
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks shankar, that was so simple
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic