SrcForge

Lesson 10

Control Statements

Control Statements in Java are used to control the flow of program execution based on conditions and decisions.

Lesson content

What are Control Statements in Java?

Control statements determine the order in which statements in a Java program are executed. They are mainly used for decision-making and branching in programs.

Java provides different types of control statements such as:

  • if statement
  • if...else statement
  • if...else if statement
  • switch statement

Definition List

  • Control Statement: A statement that controls the flow of execution in a Java program.
  • Condition: An expression that evaluates to either true or false.
  • Branching: The process of selecting different execution paths based on conditions.

if Statement in Java

The if statement is used to execute a block of code only if a condition is true.

Syntax of if Statement

if (condition)
{
  // block of code to be executed if the condition is true
}

Example 01

class sample
{
    public static void main(String args[])
    {
        int a=10, b=20;
        if(a>b)
          System.out.println("A is greater than B");
    }
}

Example 02

package Conditonal_Statements;

public class Simple_if {
    public static void main(String args[])
    {
        int i = 10;
        if (i < 15)
        {
            System.out.println("10 is less than 15");
        }
    }
}

In these examples, the code inside the if block executes only when the condition is true.

if...else Statement in Java

The if...else statement is used when there are two possible outcomes.

  • The if block executes when the condition is true.
  • The else block executes when the condition is false.

Syntax of if...else Statement

if (condition)
{
  // block of code to be executed if the condition is true
}
else
{
  // block of code to be executed if the condition is false
}

Example 03

int a = 20;
if (a < 18)
{
  System.out.println("Good day.");
}
else
{
  System.out.println("Good evening.");
}

Output: Good evening

Example 04

package Conditonal_Statements;

import java.util.Scanner;

public class if_else {
    public static void main(String args[]) {
        int year;
        System.out.println("Enter Year : ");
        Scanner in = new Scanner(System.in);
        year = in.nextInt();
        if (year % 4 == 0 || (year % 100 == 0 && year % 400 == 0)) {
            System.out.println("Year " + year + " is a leap year");
        } else {
            System.out.println("Year " + year + " is not a leap year");
        }
    }
}

The program checks whether a year is a leap year using conditions inside the if...else statement.

if...else if Statement in Java

The else if statement is used to test multiple conditions.

Syntax of if...else if Statement

if (condition1)
{
  // block of code to be executed if condition1 is true
}
else if (condition2)
{
  // block of code to be executed if the condition1 is false and condition2 is true
}
else
{
  // block of code to be executed if the condition1 is false and condition2 is false
}

Example 05

int a = 22;
if (a < 10)
{
  System.out.println("Good morning.");
}
else if (a < 20)
{
  System.out.println("Good day.");
}
else
{
  System.out.println("Good evening.");
}

Output: Good Evening

Example 06

package Conditonal_Statements;

import java.util.Scanner;

public class Nested_if {
    public static void main(String args[])
    {
        /*
            Nested if Statement
            A company insures its drivers in the following cases:
            a. If the driver is married.
            b. If the driver is unmarried, male & above 30 years of age.
            c. If the driver is unmarried, female & above 25 years of age.
        */
        Scanner in =new Scanner(System.in);
        System.out.println("Enter The Marital Status M/U: ");
        char marital=in.next().charAt(0);
        if(marital=='u' || marital=='U' )
        {
            System.out.println("Enter The Gender M/F: ");
            char gender=in.next().charAt(0);
            System.out.println("Enter The Age : ");
            int age=in.nextInt();
            if((gender=='M'||gender=='m')&& age>=30)
            {
                System.out.println("You are Eligible for Insurance");
            }
            else if((gender=='F'||gender=='f')&& age>=25)
            {
                System.out.println("You are Eligible for Insurance");
            }
            else
            {
                System.out.println("You are Not Eligible for Insurance");
            }
        }
        else if(marital=='m' || marital=='M' )
        {
            System.out.println("You are Eligible for Insurance");
        }
        else {
            System.out.println("Invalid Input");
        }
    }
}

This example demonstrates nested if statements where multiple conditions are checked one inside another.

Switch Statement in Java

The switch statement is used to select one block of code from multiple options.

Syntax of Switch Statement

switch(expression)
{
  case x:
    // code block
    break;
  case y:
    // code block
    break;
  default:
    // code block
}

How Switch Statement Works

  • The switch expression is evaluated once.
  • The value is compared with each case.
  • If a match is found, the corresponding code block executes.
  • The break statement stops further execution.

Example 07

int day = 4;
switch (day) {
  case 1:
    System.out.println("Monday");
    break;
  case 2:
    System.out.println("Tuesday");
    break;
  case 3:
    System.out.println("Wednesday");
    break;
  case 4:
    System.out.println("Thursday");
    break;
  case 5:
    System.out.println("Friday");
    break;
  case 6:
    System.out.println("Saturday");
    break;
  case 7:
    System.out.println("Sunday");
    break;
  default:
    System.out.println("Invalid Day");
}

Output: Thursday

Example 08

package Conditonal_Statements;

import java.util.Scanner;

public class switch_demo {
    public static void main(String args[]) {
        int a,b,c,ch;
        System.out.println("1.Addition");
        System.out.println("2.Subtraction");
        System.out.println("3.Multiplication");
        System.out.println("4.Division");
        System.out.println("Enter Your Choice : ");
        Scanner in =new Scanner(System.in);
        ch=in.nextInt();
        System.out.println("Enter 2 Nos : ");
        a=in.nextInt();
        b=in.nextInt();
        switch (ch)
        {
            case 1:
                c=a+b;
                System.out.println("Addition : " +c);
                break;
            case 2:
                c=a-b;
                System.out.println("Subtraction : "+c);
                break;
            case 3:
                c=a*b;
                System.out.println("Multiplication : "+c);
                break;
            case 4:
                c=a/b;
                System.out.println("Division : "+c);
                break;
            default:
                System.out.println("Invalid Selection");
                break;
        }
    }
}

Advantages of Control Statements in Java

Better Decision Making

Control statements help programs make decisions based on conditions.

Improved Program Flow

They allow developers to control how and when code executes.

Easier Error Handling

Conditions help validate user input and avoid invalid operations.

FAQ

1. What are control statements in Java?

Control statements are statements used to control the flow of execution in a Java program.

2. What is the difference between if and switch statements?

The if statement is used for checking conditions, while the switch statement is used for selecting one option from multiple cases.

3. What is a nested if statement in Java?

A nested if statement is an if statement placed inside another if statement to check multiple conditions.