Thursday 8 June 2017

Java(if-else)

                         if-else statement

Java if else statement is used to test the condition.It checks the boolean condition (i.e. true or false).

There are various type of if statement in java.
  • if statement
  • if-else statement
  • nested if statement
  • if-else-if ladder

Java if statement

The java if statement test the condition.It execute the if block if condition is true.

Syntax:  
              if(condition){
                //code to be executed
              }


Example: 
                 public class ifexample{
                 public static void main (String[] args){
                  int car=50;

                  if(car>45){
                       system.out.println("I have car more than 45")
                  }
              }
        }

Output: I have car more than 45

Java if-else statement

The java if-else statement also test the condition.It executes the if block if condition is true otherwise else block is executed.

Syntax: 
             if(condition){
             //code if condition is true
                   }else{
            //code if condition is false
            }


Example: 
                       public class ifelseexample{
               public static void main(String[] args){
                  int num=15;
              if(num%2==0){
              
                         system.out.println("The number is even");
              }
                else{
                     system.out.println("The number is odd");
                }
         }
    }

Output: The number is odd


No comments:

Post a Comment