Wednesday 22 June 2011

Write a Program to use if - else in Java

Open notepad or any text editor and type this code.

public static void main(String args[])
  {
  int a=5,b=8;
      System.out.print("2 nos. are: "+a+" and "+b+"\n");
      if(a>b)
       {
        System.out.print("Bigger no is:"+a);
        }
      else
       {
      System.out.print("Bigger no is:"+b);
       }
   }
}

and save this file as example.java in the bin directory (c:\j2sdk1.4.2\bin).

Now open command prompt and go to your Java directory.
Now compile your  program by typing  javac ifelse.java
Now the program will be compiled.
Now Run your program by typing  java ifelse

The output of the above program will be like this.

No comments:

Post a Comment