JAVA programs

Program Using if else statement

In this program we see the use of the if-else statement. If-else is a flow control statement that operate on particular condition given in it. If condition is true block below condition is execute if condition is false else block code execute.

Program

class IfElseDemo {

   public static void main(String[] args) {    

      int num = -5;

    

      if (num > 0)

      {

         System.out.println("Given Number is positive.");

      }

      else

      {

         System.out.println("Given Number is Negetive.");

      }

   }

}

When Run The Program Following Output:

Given Number is Negetive

Description

Use if statement to execute a block of Java code if a condition is true.if condition is false else block code will execute.                                                                                                         

Syntax of if-else

if (condition) {

  // block of code to be executed if the condition is true

}

else

{

// block of code to be executed if the condition is false

}

 

For ex:

if(a> b)

{

System.out.println(“a is greater”);

}

else

{

System.out.println(“b is greater”);

}

In above example if a is greater that time only the statement “a is greater”will print. Else “b is greater” will print on screen

Releted Programs

© 2017 codewmAll rights reserved.