Conditional Statements in Java: A Complete Guide

Posted in

Conditional Statements in Java: A Complete Guide
Aashiyamittal

Aashiya Mittal
Last updated on March 29, 2024

    The decision-making process in programming languages is analogous to decision-making in real life. In programming, you will come across certain situations where you will have two or more options and need to select the one based on some specific condition. To decide upon one option among the others, the role of conditional statements comes into the picture.

    Conditional statements in any programming language help computers to make decisions based on a given condition. As a result, we also refer to conditional statements as decision-making statements.

    For example, if there is a block of code in your program and you want the computer to execute it only if a particular condition holds true. In such a case, you need to use conditional statements.

    Moreover, conditional statements help in controlling the execution flow of a program based on certain conditions. This means that conditional statements instruct the computer to run different blocks of code in a program depending on the specified conditions.

    In this blog post, we shall introduce you to the conditional statements in the Java programming language, along with their syntaxes, flowcharts, and examples.

    Conditional Statements in Java

    The Java programming language provides five different conditional statements, namely if, if-else, if-else-if, nested if, and switch case. Let us discuss each of these statements in detail below.

    1. If Statement

    This is the basic and simple conditional statement in Java. It determines whether to execute a certain block of code or not based on the given condition. If the condition in the ‘if’ statement evaluates to true, it executes code present inside the ‘if’ block. Otherwise, it will not execute the ‘if’ block.

    Moreover, the rest of the code outside the ‘if’ block will be executed, irrespective of the condition in the ‘if’ statement evaluating to true or false.

    Syntax

    if (condition){
    statement (s);
    }

    Flowchart

    Example

    publicclassMyClass {
    publicstaticvoidmain(String args[]) {
    int x=15, y=20;
    if (x<y)
    {
    System.out.println("X is less than Y");
    }
    System.out.println("Welcome to TechGeekBuz!");
    }
    }

    Output

    X is less than Y

    Welcome to TechGeekBuzz!

    In the above example, we have two variables , x and y with values of 15 and 20, respectively. We have specified a condition that if x is less than y, then print the statement inside the ‘if’ block.

    Here, x>y, i.e., 15>20. The condition holds true. As a result, the statement inside the ‘if’ block will be executed. The statement outside the ‘if’ block gets executed even if the specified condition holds false.

    2. If-Else Statement

    The ‘if-else’ statement decides upon a single block to execute out of the two different blocks of code. If the condition in the ‘if’ statement is true, it executes the code inside the ‘if’ block. Otherwise, the execution flow of the program moves to the ‘else’ block.

    Moreover, all the statements outside the ‘if’ and ‘else’ blocks get executed, regardless of the specified condition.

    Syntax

    if(condition_1) {
    Statement(s);
    }
    else {
    Statement(s);
    }
    }

    Flowchart

    Program

    publicclassMyClass {
    publicstaticvoidmain(String args[]) {
    int x=50, y=70;
    if (x>y)
    {
    System.out.println("X is greater than Y");
    }
    else
    {
    System.out.println("Y is greater than X");
    }
    System.out.println("Welcome to TechGeekBuzz!");
    }
    }

    Output

    Y is greater than X
    Welcome to TechGeekBuzz!

    In the above example, we have two variables, x and y with values of 50 and 70, respectively. We have specified the condition in the ‘if’ statement (x>y).

    If the given condition evaluates to true, the program flow moves to execute the statements in the ‘if’ block. Otherwise, the flow moves to the else block.

    Since 50 is not greater than 70, the condition in the ‘if’ statement holds false, and the ‘else’ block gets executed.

    3. If-Else-If Ladder

    With the ‘if-else-if’ ladder, we can specify multiple conditions or decide among multiple conditions. If the condition specified in the ‘if’ statement holds true, the program execution flow moves to the block of code associated and all the remaining ladder of else-if and else is bypassed.

    If any of the conditions in the else-if statement evaluates to true, the block of code associated with that statement gets executed. Otherwise, the program execution flow moves to the last ‘else’ block.

    Like the above conditional statements, any condition specified outside the if-else-if ladder gets executed even if the conditions in it hold true or false.

    Syntax

    if (condition){
    Statement(s);
    }
    elseif (condition){
    Statement(s);
    }
    .
    .
    else{
    Statement(s);
    }


    Flowchart

    Program

    publicclassMyClass {
    publicstaticvoidmain(String args[]) {
    int x = 5;
    if (x==0)
    {
    System.out.println("X is equal to zero");
    }
    elseif(x>0)
    {
    System.out.println("X is positive");
    }
    else
    {
    System.out.println("X is negative");
    }
    System.out.println("Welcome to TechGeekBuzz!");
    }
    }


    Output

    X is positive
    Welcome to TechGeekBuzz!

    In the above example, we have checked whether the given number is positive or negative. We have taken a variable x and initialized its value to 5.

    Later, we specified a condition (x==0) in the ‘if’ statement, which evaluates to false. The control flow of the program moves to the ‘else if’ statement to check its condition (X>0), which holds true, and the block of code inside it gets executed.

    4. Nested-If Statement

    Nested-if is another conditional statement in Java that enables us to use one ‘if’ statement inside the other one. It refers to if within if. More specifically, you can use ‘if’ and ‘else’ statements inside another ‘if’ statement. This type of conditional statement allows us to check multiple conditions.

    Syntax

    if (condition1)
    {
    Statement(s);
    if (condition2) {
    Statement(s);
    }
    else {
    Statement(s);
    }
    }


    Flowchart

    Program

    publicclassMyClass {
    publicstaticvoidmain(String args[]) {
    int x = 5;
    if (x>0)
    {
    if (x<10)
    {
    System.out.println("X is less than 10");
    }
    
    else
    {
    System.out.println("X is greater than 10");
    
    }
    }
    
    else
    {
    System.out.println("X is negative");
    }
    System.out.println("Welcome to TechGeekBuzz!");
    }
    }


    Output

    X is less than 10
    Welcome to TechGeekBuzz!

    In the above example, we have used the ‘if-else’ statement inside the ‘if’ statement. We have taken a variable of value 5.

    Later, we specified a condition (x>0), which holds true, and the control flow moves inside the ‘if’ block. Then we have another ‘if’ with condition (x<10), which evaluates to true. The statement associated with this condition is executed. If none of the ‘if’ conditions satisfy, the control flow would move to the last ‘else’ statement and execute the block of code inside it.

    5. Switch Case Statement

    The switch case statement in Java is a multi-way branch statement. It lets us execute the block of code among multiple alternatives. It consists of one expression and multiple case values. The expression is evaluated and its value is checked with each case value. Then the statements associated with the matching case value are executed. However, if no case value matches, the default statement is executed.

    Syntax

    switch (expression)
    {
    case value1:
    statement(s);
    break;
    case value2:
    statement(s);
    break;
    .
    .
    case valueN:
    statement(s);
    break;
    default:
    statementDefault;
    }


    Flowchart

    Program

    publicclassMyClass {
    publicstaticvoidmain(String args[]) {
    int rainbow;
    String colorString;
    
    switch (rainbow=8) {
    case1:
    colorString = "Violet";
    break;
    
    case2:
    colorString = "Indigo";
    break;
    
    case3:
    colorString = "Blue";
    break;
    
    case4:
    colorString = "Green";
    break;
    
    case5:
    colorString = "Yellow";
    break;
    
    case6:
    colorString = "Orange";
    break;
    
    case7:
    colorString = "Red";
    break;
    
    default:
    colorString = "Invalid Color";
    }
    System.out.println(colorString);
    
    System.out.println("Welcome to TechGeekBuzz!");
    
    }
    }

    Output

    Invalid Color
    Welcome to TechGeekBuzz!

    In the above example, we have taken a variable ‘rainbow’ whose value is 8. Also, we have defined seven different case values for each color in the rainbow. There is one default statement that gets executed if the value of the expression in the switch statement does not match any of the defined case values.

    Here, the expression ‘rainbow=8’ does not match with any of the case values, and hence, the default statement is executed.

    Conclusion

    This was all about conditional statements in Java. These statements help computers decide which part of the program to execute based on the specified conditions. Also, conditional statements control the flow of the program execution.

    We hope you found this article interesting and helpful, as we have discussed the syntax, flowchart, and example of each conditional statement to help you develop a better understanding.

    People are also reading:

    FAQs


    Java uses five different conditional statements: 1. if 2. if-else 3. if-else-if 4. nested-if 5. Switch-case.

    Conditional statements in programming are decision-making statements that help computers make a decision whether to execute a block of code or not based on the specified conditions.

    We use conditional statements in programming to decide the flow of program execution based on conditions.

    Leave a Comment on this Post

    0 Comments