Java Nested if programming example?

 

·     Java Nested  if

Q.1 ]  what is nested if and their programming example?

·      A nested if is an if statement in which we can use other if statements many number times.

Example1: 

  class  Example1  {

public static void main(String   args[])  {

int  a=10;

if (a>5)   {

if (a>9)   {

System.out.println(“the value is grater than 9”);

}

  else   {

System.out.println(“the value is lower  than 9”);

}

}

Example2: 

  class  Example2  {

public static void main(String   args[])  {

int  a=20,  b=50,  c=100 ;

if (a>15)   {

if (a>40)   {

if (a>120)   {

System.out.println(“the value is grater than 120”);

}

  else   {

System.out.println(“the value is lower  than 120”);

}

}

·        If – else  if the leader

·         It is a common programming syntax contract that is based on the sequence if nested if.

Example:   

   if  (condition 1)  {

statement; \\ executing condition 1 then return true

}

      else if  (condition 2)  {

statement; \\ executing condition 2 then return true

}

   else  (condition 3)  {

statement;   // when no other condition is satisfied

}

Q1] What is the basic structure of if and else if?

   if  (condition 1)  {

statement; \\ executing condition 1 then return true

}

      else if  (condition 2)  {

statement; \\ executing condition 2 then return true

}

      else if  (condition 3)  {

statement; \\ executing condition 2 then return true

}

   else  (condition 4)  {

statement;   // when no other condition is satisfied

}

Q.4 ] write a java program which will   tell that the given number is between range 20- 50 or not?

class  Range  {

public static void main(String   args[])  {

int  a=26;

if (a>15)   {

if (a<38)   {

System.out.println(“the value is  one  range   ” + a);

}

      else if  (a == 50)  {

System.out.println(“the value is  range   ” + a);

}

  else   {

System.out.println(“the value is getter  than 40” +a);

}

  else   {

System.out.println(“the value is lower  than 40”+a);

}

}

}

 

 


Post a Comment

0 Comments