The if else structure is used to create decision mechanisms in the program. Basic structure
if (boolean variable or constant) {
The operations to be applied when the value of the boolean variable or constant is true.
} else {
The operations to be applied when the value of the boolean variable or constant is false.
}
The if else structure can be written in abbreviated form.
variable name = (variable value if boolean variable or constant? boolean is true: variable value if boolean is false);
import java.io.*; public class Main { public static void main(String[] args) { int x = 30; if(x==10){ System.out.println("x = "+x); } else if(x==20){ System.out.println("x = "+x); } else { System.out.println("x = "+x); } } }
output:
x = 30
Tagged In:
Java