Java Conditions and If Statements

Introduction to if else and Nested if else statements in Java

The if-else statement in Java is used to control the flow of execution of a program. It allows you to execute different blocks of code depending on the value of a Boolean expression. The syntax for the if-else statement is as follows:


    if (boolean expression) {
      // code to execute if the expression is true
    } else {
      // code to execute if the expression is false
    }
   

The Boolean expression can be any expression that evaluates to a true or false value. If the expression is true, the code block inside the if clause is executed. Otherwise, the code block inside the else clause is executed.

Nested if-else statements can be used to create complex conditional logic, but it is important to use them carefully, as they can make your code difficult to read and maintain.

For example, the following code uses a nested if-else statement to check the age of a person and print a different message depending on their age:

  int age = 10;
  if (age >= 18) {
    System.out.println("You are an adult.");
  } else if (age >= 13) {
    System.out.println("You are a teenager.");
  } else {
    System.out.println("You are a child.");
  }
 
Here are some tips for using if-else and nested if-else statements effectively:
  • Use if-else statements for simple conditional logic.
  • If the conditional logic is complex, it may be better to use a switch statement.
  • Use nested if-else statements carefully, as they can make your code difficult to read and maintain.
  • Use proper indentation and spacing to make your if-else statements easier to read.
  • Add comments to your code to explain what your if-else statements are doing.

Exercises

Read a number from the user. If the number equals 2 then display valid number, for any other input display invalid number


  package javaprogrammingdemo;
  import java.util.Scanner;
  public class javalabclass{
    public static void main(String args[]) 
    {
      Scanner input = new Scanner(System.in);
      System.out.println("Enter the number");
      int value = input.nextInt();
      if(value==2)
      {
        System.out.println("Valid Number");
      }
      else
      {
        System.out.println("Invalid Number");
      }
    }
  }
  
  Program Output
  Enter the number
  2
  Valid Number 

Get a Number as input from the user. If the user enter 1 then display the output as "one". If the user enters 2 then display the result as "two". For any other input display invalid input.


  package javaprogrammingdemo;
  import java.util.Scanner;
  public class javalabclass{
    public static void main(String args[]) 
    {
      Scanner input = new Scanner(System.in);
      System.out.println("Enter the number");
      int value = input.nextInt();
      if(value==1)
      {
        System.out.println("one");
      }
      else if(value==2)
      {
        System.out.println("two");
      }else
      {
        System.out.println("Invalid Input");
      }
    }
  }
  
  Program output
  Enter the number
  2
  two 

Get a Number as input from the and check if the number is odd or even.


  package javaprogrammingdemo;
  import java.util.Scanner;
  public class javalabclass{
  public static void main(String args[]) 
  {
    Scanner input = new Scanner(System.in);
    System.out.println("Enter the number");
    int value = input.nextInt();
    if(value%2==0)
    {
      System.out.println("Its an even number");
    }
    else
    {
      System.out.println("Its an odd number");
    }
  }
  }
  Program output
  Enter the number
  5
  Its an odd number