Skip to main content

Java if-else statment with example program.

Introduction:-

    Today we will be having a look on the java if-else statement and we shall see a sample program a new thing about this post that there will be a download link to the source- code of the program so you can have full access to the code .To implement your programing skills on it and if you wish to share the code genrated by you feel free to comment down below.


What is if-else statement?

   if-else statement is basically the conditional statement where only one we be executed if the condition evaluates true the code inside the if statement shall be executed or if the condition evalutes false the else statement shall be executed . Now the condition is written inside the if statement and there is no condition in the else block.

What is the syntax of if-else in java?

Syntax:-

  if (condition){
    statement 1
    statement 2
    statement 3
   .............
 }
 else{
 statment 1
 statement 2
   .............
 }

  So the above given syntax is just a simple if-else no ladder or any-other type of if-else is given. For the ladder there will be a seprate post so check for future updates.

How to write a simple java if-else program?

Code Statement :-Program is about finding the largest of two number.   

Code:-
   class condition{
      public static void main(String[] args){
          int a = 5;
          int b = 6;
          if (a>b){
              System.out.println("The Greatest of the number is"+a);
          }
          else{
               System.out.println("The Greatest number is "+b);
          } 
      }
  }

   Now save this file as "condition.java" and then run using cmd or any ide.The output of the above program is given bellow.

OUTPUT:-
The Greatest of the number is 6

To download the source code Download

   So this post was all about the if-else statement in the next post there will be about the ladder if else.If you found this post usefull do share with friends and comment down bellow and do check other java post.

Comments

Popular posts from this blog

Program to calculate the room area and volume to illustrate the concept of single inheritance.

CODE: class Roomarea   { protected double l, b; Roomarea(double len, double br) { l=len; b=br; } void putdata() { System.out.println("Area of Room: "+(l*b)); } } class Roomvol extends Roomarea   { double h; Roomvol(double len, double br, double he) { super(len, br); h=he;   } void putdata() { super.putdata(); System.out.println("Volume of Room: "+(l*b*h)); } } public class EXP18Q4 { public static void main(String[] args) { Roomvol r1 = new Roomvol(10, 20, 10); r1.putdata();   } }

Define an exception called ‘NotMatchexception’ that is thrown when a string is not equal to ‘India’. Write a java program that uses this exception.

Code: import java.util.Scanner; class NotMatchException extends Exception {   public NotMatchException (String str)   {     System.out.println(str);   } } public class StringExc {    public static void main(String[] args)    {      Scanner scan = new Scanner(System.in);      System.out.print("Enter the string : ");      String input = scan.nextLine();      try      {        if(input.equalsIgnoreCase("India"))          System.out.println("String matched !!!");        else          throw new NotMatchException ("String not matched !!!");     }     catch (NotMatchException s)     {       System.out.println(s);     }   } } Output:

Develop a program using control loops in applets.

Code: import java.awt.*; import java.applet.*; public class Control extends Applet {      public void paint(Graphics g)      {      for(int i=1;i<=4;i++)     {          if(i%2==0)         {            g.fillOval(90,i*50+10,50,50);           g.setColor(Color.black);        }        else       {          g.fillOval(90,i*50+10,50,50);          g.setColor(Color.red);       }    }   } } /* <applet code=Control width=300 height=300> </applet> */ Output: