If, Else, else if condition in java || Nested if else

if and else is used when we have to compare or apply operation bitween 2 or more condition

 

If :  if is used to cheak true or false of any condition (if we have only 1 condition )

eg. let we have any number and we have to cheak the number is greater or less then any perticular number

ex code.

package ……..;

import java.util.Scanner;
public class ….. {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

int x = sc.nextInt();

if(x<10) {
System.out.println(“no.is greater less then 10”);
}

}
}

 

If alse : this is also to work as same if but its print 2 answe true or false

ex code:

package intern;
import java.util.Scanner;
public class AO {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

int x = sc.nextInt();

if(x<10) {
System.out.println(“no.is greater less then 10”);
}else {
System.out.println(“no is less then 10”);
}

}
}

 

Nested if else

nested if alse is used when we have multiple condition

eg. let we have any number and we have to cheak the no is between the interval or not

ex. code:

package intern;
import java.util.Scanner;
public class AO {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

int x = sc.nextInt();

if(x<10) {
System.out.println(“no is less then 10”);
}else if(x>10 && x<20){
System.out.println(“no is in the interval”);
}else if(x>20) {
System.out.println(“no is greater then 20”);
}

Rishabh

I want to change the world with the help of tech 🙃

Leave a Reply