Java: if statement

An If statement is a control flow statement in a java programming language that conveys that anything that satisfies and is true to the logic provided will be able to execute the logic body. If the condition is not satisfied if the body is rejected and the code continues after the end of the curly brace of the if statement.

Syntax

If(Condition)
{
    //if the conditon is true this body will be executed
}

Example - Negative Scenario

public class Test
{
    public static void main(String[] args)
    {
        int val1 = 15, val2 = 2, val3, val4 = 10, val5;

        if ((val2 + val1) == 18)
        {
            System.out.println("Correct Sum Up Here Is Your Token: " +val4);
        }
        System.out.println("This will always execute.");
    }
}

Output: This will always execute.

So here is a negative scenario where I have given a condition of adding two numbers and asking when it is equal to the provided number then execute the body of the if statement.

Example - Positive Scenario

public class Test
{
    public static void main(String[] args)
    {
        int val1 = 15, val2 = 2, val3, val4 = 10, val5;
        if ((val2 + val1) == 17)
        {
            System.out.println("Correct Sum Up Here Is Your Token : " +val4);
        }
        System.out.println("This will always execute");
    }
}

Output: Correct Sum Up Here Is Your Token: 10
This will always execute

So here when the condition is satisfied it went on with the if body and continued to proceed to the next step of the code after the curly braces of the if statement.

Thu, 10/21/2021 - 14:02

Authored by

Ashwin is working with iVagus as a Technical Lead. He has experience in various technologies like Cypress, JavaScript, TypeScript, Rest Assured, Selenium, Cucumber, and Java.
Tags