Tuesday, 22 November 2016

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

No comments:

Post a Comment