Java If - ELSE
Java If - ELSE
o if statement
o if-else statement
o if-else-if ladder
o nested if statement
Java if Statement
The Java if statement tests the condition. It executes the if block if condition is true.
Syntax:
1. if(condition){
2. //code to be executed
3. }
Example:
1. //Java Program to demonstate the use of if statement.
2. public class IfExample {
3. public static void main(String[] args) {
4. //defining an 'age' variable
5. int age=20;
6. //checking the age
7. if(age>18){
8. System.out.print("Age is greater than 18");
9. }
10. }
11. }
Output:
Syntax:
1. if(condition){
2. //code if condition is true
3. }else{
4. //code if condition is false
5. }
Example:
1. //A Java Program to demonstrate the use of if-else statement.
2. //It is a program of odd and even number.
3. public class IfElseExample {
4. public static void main(String[] args) {
5. //defining a variable
6. int number=13;
7. //Check if the number is divisible by 2 or not
8. if(number%2==0){
9. System.out.println("even number");
10. }else{
11. System.out.println("odd number");
12. }
13. }
14. }
Output:
odd number
Syntax:
1. if(condition1){
2. //code to be executed if condition1 is true
3. }else if(condition2){
4. //code to be executed if condition2 is true
5. }
6. else if(condition3){
7. //code to be executed if condition3 is true
8. }
9. ...
10. else{
11. //code to be executed if all the conditions are false
12. }
Example:
1. //Java Program to demonstrate the use of If else-if ladder.
2. //It is a program of grading system for fail, D grade, C grade, B grade, A grade and
A+.
3. public class IfElseIfExample {
4. public static void main(String[] args) {
5. int marks=65;
6.
7. if(marks<50){
8. System.out.println("fail");
9. }
10. else if(marks>=50 && marks<60){
11. System.out.println("D grade");
12. }
13. else if(marks>=60 && marks<70){
14. System.out.println("C grade");
15. }
16. else if(marks>=70 && marks<80){
17. System.out.println("B grade");
18. }
19. else if(marks>=80 && marks<90){
20. System.out.println("A grade");
21. }else if(marks>=90 && marks<100){
22. System.out.println("A+ grade");
23. }else{
24. System.out.println("Invalid!");
25. }
26. }
27. }
Output:
C grade
Syntax:
1. if(condition){
2. //code to be executed
3. if(condition){
4. //code to be executed
5. }
6. }
Example:
1. //Java Program to demonstrate the use of Nested If Statement.
2. public class JavaNestedIfExample {
3. public static void main(String[] args) {
4. //Creating two variables for age and weight
5. int age=20;
6. int weight=80;
7. //applying condition on age and weight
8. if(age>=18){
9. if(weight>50){
10. System.out.println("You are eligible to donate blood");
11. }
12. }
13. }}
Output:
Example 2:
1. //Java Program to demonstrate the use of Nested If Statement.
2. public class JavaNestedIfExample2 {
3. public static void main(String[] args) {
4. //Creating two variables for age and weight
5. int age=25;
6. int weight=48;
7. //applying condition on age and weight
8. if(age>=18){
9. if(weight>50){
10. System.out.println("You are eligible to donate blood");
11. } else{
12. System.out.println("You are not eligible to donate blood");
13. }
14. } else{
15. System.out.println("Age must be greater than 18");
16. }
17. } }
Output: