Control Statements in Java TN

Control Statements in Java

Control Statements in Java

In this tutorial on control statements in Java, we will have a detailed look at the IF-ELSE and nested IF-ELSE statements. The next tutorial will explain the basics of iteration statements in Java.

This java tutorial is part of Free Selenium 3.0 Tutorials by Techcanvass. These tutorials are categorized into Java, Selenium, TestNG, Automation Framework and Grid.

There are three main categories of control flow statements as shown in the image below:

Control Statements in Java

IF……ELSE Statement

If…else statement is a conditional statement. It’s used to execute specific logic based on conditions.

BASIC ….IF… STATEMENT

The simplest form of conditional statement is using a single IF in the code block as shown below:

  if(condition)
{
statement1;
}

¬ The code in the IF Block is executed only if the condition is true. If the condition is NOT TRUE, nothing happens.

If we want to perform some action when the condition is NOT TRUE, we use IF….ELSE conditional statement. The syntax is as shown below:

 if(condition)
{
statement1;
}
else
{
statement2;
}

Let’s take an example to understand the usage of IF….Else statements:

class PrintPassFail {
    public static void main(String arg[])
    {
        int marks = 62;
        if(marks > 35)            // is 62 greater then 35
        {
¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†System.out.println(“Pass”); ¬†¬†¬†¬†// Print Pass when condition is TRUE
        }
        else
        {
¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†System.out.println(“Fail”); ¬†¬†¬†¬†// Print Fail when condition is FALSE¬

       }
    }
}

Let’s understand the code. The code checks the value of marks, which has been assigned a value 62 with the following line:

int marks = 62;

The IF…..ELSE block has two parts. The IF part of the IF…ELSE statement is as follows:

 if(marks > 35)            // is 62 greater then 35
        {
¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†System.out.println(“Pass”); ¬†¬†¬†¬†// Print Pass when condition is TRUE
        }

The code checks the value of marks. If the value of marks is more than 35 (marks > 35), then the system prints a value “Pass” on the console as shown below:

Control Statements in Java Program

NESTED ….IF… ELSE STATEMENT

We can also nest IF statements inside another IF statement. Nesting of IF….ELSE statements help us in handling complex conditions also.

Syntax is as shown below:

if (Condition 1 is TRUE) {

   // Executes when condition 1 is true

   if (Condition 2 is TRUE) {

      // Executes when Condition 2 (and Condition 1) is true

   }

}

Let’s take an example to understand the usage of nested IF…ELSE statements. Table shows the rate of interest for different loan amounts and tenures. Let’s write a program to know the interest rate automatically.

The table suggests the following:

=> If the loan amount is less than or equal to 10,00,000

———————-> And if the tenure is less than equal to 10 years

—————————————> Then the interest rate is 10%

————————-> Else (if the tenure is more than 10 years)

————————————————> Then the interest rate is 11%

=> If the loan amount is more than 10,00,000

—————————————-> And if the tenure is less than equal to 10 years

—————————————————> Then the interest rate is 10.5%

—————————————–> Else (if the tenure is more than 10 years )

—————————————————> Then the interest rate is 11.5%

This is a good case for using a nested IF..ELSE statement. The code is as below:

package Javatutorials;

class InterestRate {
    public static void main(String arg[])
    {
        int loan=1050000;
        int tenure=9; //9 years
¬†¬†¬†¬†¬†¬

        if(loan <= 100000)            // If loan amount is less than or equal to 10,00,000
        {
            if ( tenure <= 10)           // If tenure is less than or equal to 10 years
            {
¬†¬†¬†¬†¬†¬† ¬†¬†¬† ¬†¬†¬†¬† System.out.println(“interest rate is 10%”);
            }
            else                    //// If tenure is more than 10 years
            {
¬†¬†¬†¬†¬†¬† ¬†¬†¬† ¬†¬†¬† ¬†System.out.println(“interest rate is 11%”);
            }
        }
        else    // If loan amount is more than 10,00,000
        {
            if ( tenure <= 10)           // If tenure is less than or equal to 10 years
            {
¬†¬†¬†¬†¬†¬† ¬†¬†¬† ¬†¬†¬†¬† System.out.println(“interest rate is 10.5%”);
            }
            else                    //// If tenure is more than 10 years
            {
¬†¬†¬†¬†¬†¬† ¬†¬†¬† ¬†¬†¬† ¬†System.out.println(“interest rate is 11.5%”);
            }

       }
}
}

The out put of this program is shown below:

Control Statements in Java Program 2
¬†¬

IF……ELSEIF……..ELSE Statement

We have an alternative to nested IF….ELSE statement block. The syntax is as follows:

if (Condition 1 is TRUE) {

   // Executes when condition 1 is true

}

 elseif (Condition 2 is TRUE) {

      // Executes when Condition 2 (and Condition 1) is true

}

  else

{   }

}

IF….ELSEIF….ELSE is different from nested IF…ELSE as there is no nesting. Each IF, ELSEIF and ELSE checks a condition independently.

Exercises for You

Exercise 1: Write the interest rate example using IF…ELSEIF…ELSE statment blocks

Exercise 2: Guess the output

Guess the output of the following program using If….else code block:

package Javatutorials;

public class GuessOutput {

    public static void main(String[] args) {
        int age = 29;

        if (age < 13)

        {

¬†¬† ¬†¬†¬† ¬†¬†¬† ¬†System.out.println(“You are a child!”);

        }

        else if (age < 19)

        {

¬†¬† ¬†¬†¬† ¬†¬†¬† ¬†System.out.println(“You are no longer a child, but teenager.”);

        }

        else

        {

            if (age < 65)

            {

¬†¬† ¬†¬†¬† ¬†¬†¬† ¬†¬†¬† ¬†System.out.println(“You are an adult!!!”);

            }

            else

            {

¬†¬† ¬†¬†¬† ¬†¬†¬† ¬†¬†¬† ¬†System.out.println(“You are now senior, enjoy!!!”);

            }

¬†¬† ¬†¬†¬† ¬†System.out.println(“Since you are over the age of 19, you deserve a drink!”);

        }

    }

}

About Techcanvass

Techcanvass offers IT certification courses for professionals. We are an IIBA endorsed education provider (EEP), iSQI ATP (for Certified Agile Business Analyst Training) as well as Agile Testing alliance partner for CP-SAT certification training in Selenium.

Our Selenium 3.0 Training programs along with DevOps courses are designed for Manual testing professionals with basics of Java and Python covered.

Selenium 3.0 Training with Java

Selenium Training Course

Selenium 3.0 Training with Python

python selenium training

Selenium Certification Training (CP-SAT)

Selenium Certification Course

DevOps Certification Training

DevOps Certification Training

Leave a Reply

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