Java Conditional Statements

Java, like all other programming languages, is equipped with specific statements that allow us to check a condition and execute certain parts of code depending on whether the condition is true or false. Such statements are called conditional and are a form of a composite statement.

The Java if statement is used to test the condition. It checks the boolean condition: true or false. There are various types of if statements in Java.

  • if statement
  • if-else statement
  • if-else-if ladder
  • nested if statement

Java supports the usual logical conditions from mathematics:

  • Less than: a < b
  • Less than or equal to: a <= b
  • Greater than: a > b
  • Greater than or equal to: a >= b
  • Equal to a == b
  • Not Equal to: a != b

1. Java if Statement

The Java if statement tests the condition. It executes the if block if the condition is true.

Syntax:

1.   if(condition){  

2.   //code to be executed  

3.   }  


Example Code:

1.   //Java Program to demonstrate the use of if statement.  

2.   public class Example {  

3.   public static void main(String[] args) {  

4.       //defining an 'age' variable  

5.       int age=30;  

6.       //checking the age  

7.       if(age>19){  

8.           System.out.print("Age is greater than 19");  

9.       }  

10. }  

11. }  


2. Java if-else Statement

The Java if-else statement also tests the condition. It executes the if block if condition is true otherwise else block is executed.

Syntax:

1.   if(condition){  

2.   //code if condition is true  

3.   }else{  

4.   //code if condition is false  

5.   }  


Example Code:

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. }  


3. Java if-else-if ladder Statement

The if-else-if ladder statement executes one condition from multiple statements.

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 Code:

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. }  

Click here to find the complete Java Tutorials Series


Java Operators  << Previous     ||     Next >>  Java Control Statements (Loops)

Author
Vaneesh Behl
Passionately writing and working in Tech Space for more than a decade.

Comments

Popular posts from this blog

Java Date Format Validation with Regular Expressions: A Step-by-Step Guide

Mastering Selenium Practice: Automating Web Tables with Demo Examples

14 Best Selenium Practice Exercises for Automation Practice

Appium Inspector and Locating Elements for Mobile Automation

17 Best Demo Websites for Automation Testing Practice

Selenium IDE Tutorial: How to Automate Web Testing with Easy-to-Use Interface

Exploring Modules and Packages in Python: Building Reusable Code Components

Python Behave Tutorial: A Comprehensive Guide to Behavior-Driven Development (BDD)

Why Are Companies Choosing Kotlin For Their App Development?

Handle Multiple Browser Tabs and Alerts with Selenium WebDriver