Netsed If statement

You can use Nested If means one if statement inside another if statement. The below program shows the use of same. You can also use nested if..else statement. If there is only one statement in IF or ELSE block there is no need to mention the  {  and  } brackets. 


It has following syntax.
if(condition 1)
{ 
   //Executes if condition 1 is true 
   if(condition 2)
   { 
     //Executes if condition 2 is true 
   } 
}

PROGRAM
public class NetsedIfDemo {
 
 public static void main(String[] args) {
  
  int number = -52;
  System.out.println("Number is : "+number);
  
  if(number > 0) //1
   if(number%2 == 0)
    System.out.println("It is positive & even");
  if(number < 0) //2
   if(number%2 == 0)
    System.out.println("It is negative & even");
  if(number > 0) //3
   if(number%2 != 0)
    System.out.println("It is positive & odd");
  if(number < 0) //4
   if(number%2 != 0)
    System.out.println("It is negative & odd");
 }
}
OUTPUT
C:\>javac NetsedIfDemo.java
C:\>java NetsedIfDemo
Number is : -52
It is negative & even

Comments

Popular posts from this blog

Program to define a class 'employee' with data members as empid, name and salary. Accept data for 5 objects using Array of objects and print it.

Define a class Student with four data members such as name, roll no.,sub1, and sub2. Define appropriate methods to initialize and display the values of data members. Also calculate total marks and percentage scored by student.