• 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

How does this " if " statement behave

 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
check the foll code :
class Test
{
public static void main(String[] args)
{
if (args[0].equals("open"))
if (args[1].equals("someone"))
System.out.println("Hello!" );
else System.out.println("Go away "+ args[1]);
}
}
What would the output be if you enter the command line
1. java Test
2. java Test closed
3. java Test open
4. java Test open someone
Pls try to explain why - ( just answers can be obtained by running on the machine,but i want to understand why)
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
The Program can be interpreted as:

1) java Test------No arguments passed,trying to access args[0] & so ArrayIndexOutOf BoundsException thrown
2)java Test closed-----Fails at #1,control passes #6(No output)
3)java Test open----#1 condition succeeds,but since only one argument is passed, error at #2
4)java Test open someone----#1 condition succeeds,#2 condition succeeds,prints Hello!
I added the ubb code tags to make this clearer Carl
[This message has been edited by Carl Trusiak (edited February 04, 2001).]
 
Shailendra Guggali
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanx priya
i got it
further at //6 you can add one more else for the outer if statement - if - else is like LIFO
 
Look! I laid an egg! Why does it smell like that? Tiny ad, does this smell weird to you?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic