[go: up one dir, main page]
More Web Proxy on the site http://driver.im/

DEV Community

Cover image for Conditional Statements in Java
M. Oly Mahmud
M. Oly Mahmud

Posted on

Conditional Statements in Java

Conditional statements in Java are used to control the execution flow based on certain conditions. Usually conditional statements work by evaluating boolean expressions. In this article, we will explore the basics of the conditional statements.

1. if Statement

The if statement evaluates a condition and executes the code block only if the condition is true.

Syntax:

if (condition) {
    // Code to execute if the condition is true
}
Enter fullscreen mode Exit fullscreen mode

Example:

int number = 10;
if (number > 5) {
    System.out.println("Number is greater than 5.");
}
Enter fullscreen mode Exit fullscreen mode

2. if-else Statement

The if-else statement provides an alternate block of code to execute when the condition is false.

Syntax:

if (condition) {
    // Code to execute if the condition is true
} else {
    // Code to execute if the condition is false
}
Enter fullscreen mode Exit fullscreen mode

Example:

int number = 3;
if (number % 2 == 0) {
    System.out.println("Number is even.");
} else {
    System.out.println("Number is odd.");
}
Enter fullscreen mode Exit fullscreen mode

3. else-if

The else if statement is useful for evaluating multiple conditions sequentially. If a condition evaluates to true, its corresponding code block is executed, and the remaining conditions are skipped. Usually else if statement is used to form a nested if-else block.

Syntax:

if (condition1) {
    // Code for condition1
} else if (condition2) {
    // Code for condition2
} else {
    // Code if none of the above conditions are true
}
Enter fullscreen mode Exit fullscreen mode

Example:

int marks = 85;
if (marks >= 90) {
    System.out.println("Grade: A");
} else if (marks >= 80) {
    System.out.println("Grade: B");
} else if (marks >= 70) {
    System.out.println("Grade: C");
} else {
    System.out.println("Grade: D");
}
Enter fullscreen mode Exit fullscreen mode

4. Ternary Operator

The ternary operator (? :) is shorthand for the if-else statement. It assigns a value based on a condition.

Syntax:

variable = (condition) ? value1 : value2;
Enter fullscreen mode Exit fullscreen mode

Example:

int number = 10;
String result = (number % 2 == 0) ? "Even" : "Odd";
System.out.println("Number is " + result + ".");
Enter fullscreen mode Exit fullscreen mode

5. switch-case

The switch statement is an alternative to using multiple if-elseconditions for evaluating a single variable.

Syntax:

switch (variable) {
    case value1:
        // Code for value1
        break;
    case value2:
        // Code for value2
        break;
    // More cases
    default:
        // Code if none of the cases match
}
Enter fullscreen mode Exit fullscreen mode

Example:

int day = 3;
switch (day) {
    case 1:
        System.out.println("Monday");
        break;
    case 2:
        System.out.println("Tuesday");
        break;
    case 3:
        System.out.println("Wednesday");
        break;
    default:
        System.out.println("Invalid day");
}

Enter fullscreen mode Exit fullscreen mode

Conclusion

Conditional statements are a fundamental concept in Java programming language. To build applications, mastering this concept is a must.

Top comments (0)