• Conditional Statements In Java

    Let’s see the following conditional statements

    1. if statement
    2. nested if statement
    3. if-else statement
    4. if-else-if statement
    5. Switch Case Statement

    if statement:

    The if statement is the most basic of all the control flow statements. The if statement tells our program to execute a certain section of code only if a particular test evaluates to true.


    Sample Program:

    Nested if statement:

    An if statement inside another the statement. If the outer if condition is true then the section of code under outer if condition would execute and it goes to the inner if condition. If inner if condition is true then the section of code under inner if condition would execute.


    Sample Program:

    if-else statement:

    If a condition is true then the section of code under if would execute else the section of code under else would execute.


    Sample Program:

    if-else-if statement:

    Sample Program:

    Switch Case:

    The switch statement in Java is a multi branch statement. We use this in Java when we have multiple options to select. It executes particular option based on the value of an expression.

    Switch Case

    Switch works with the byte, short, char, and int primitive data types. It also works with enumerated types, the String class, and a few special classes that wrap certain primitive types such as Character, Byte, Short, and Integer.


    Sample Program:

    Switch Case without break statement:

    Switch Case with break statement:

    Switch Case With Break

    Must Read: Java Tutorial

    Sharing is caring.

    Share on facebook
    Share on twitter
    Share on linkedin

    Like This Post?

    We have a lot more where that came from?

    We only send really good stuff occasionally, promise.

    Rajkumar SM

    Leave a Comment

    Your email address will not be published. Required fields are marked *

    Scroll to Top
    API Testing eBook