Control Statements

Comprehensive study notes, diagrams, and exam preparation for Control Statements.

Control Statements

Definition

Control statements in Java are statements that determine the flow of execution in a program. Instead of running code strictly from top to bottom in one straight path, control statements let a program make decisions, repeat actions, or change direction based on conditions and logic.

In simple words, control statements answer these questions:

Should this block of code run or not?

Should the program repeat this task?

Should the execution jump to another part of the program?

They are one of the most important parts of Java because they make programs dynamic, intelligent, and useful. Without control statements, every Java program would behave like a simple list of instructions with no decision-making ability.


Main Content

1. Decision-Making Statements

Decision-making statements are used when a program must choose between two or more alternatives based on a condition.

if statement

  • : Executes a block only when a condition is true. Example:
  int age = 20;
  if (age >= 18) {
      System.out.println("Eligible to vote");
  }

Here, the message is printed only if the condition age >= 18 is true.

if-else, if-else-if ladder, and nested if

  • : These are used when the program must choose among multiple paths. Example:
  int marks = 75;

  if (marks >= 90) {
      System.out.println("Grade A");
  } else if (marks >= 75) {
      System.out.println("Grade B");
  } else {
      System.out.println("Grade C");
  }

This structure helps in handling different outcomes in a clear way.

Decision-making statements are commonly used in:

  • validating user input
  • checking eligibility
  • comparing values
  • handling different program conditions

2. Looping Statements

Looping statements are used when a block of code must be executed repeatedly as long as a condition remains true.

for loop

  • : Best when the number of repetitions is known in advance. Example:
  for (int i = 1; i <= 5; i++) {
      System.out.println(i);
  }

This prints numbers from 1 to 5.

while loop

  • : Used when repetition depends on a condition and the number of iterations is not fixed. Example:
  int i = 1;
  while (i <= 5) {
      System.out.println(i);
      i++;
  }

do-while loop

  • : Similar to while, but it executes the loop body at least once because the condition is checked after the body runs. Example:
  int i = 1;
  do {
      System.out.println(i);
      i++;
  } while (i <= 5);

Looping statements are used for:

  • printing sequences
  • processing arrays and collections
  • repeated calculations
  • menu-driven programs

A simple flow of looping can be understood as:

Start -> Check Condition -> Yes -> Execute Body -> Update -> Check Again
                    |
                    No
                    v
                  Stop

3. Branching Statements

Branching statements change the normal flow of execution by jumping to another statement, skipping parts of code, or ending loops early.

break

  • : Immediately terminates a loop or switch statement. Example:
  for (int i = 1; i <= 10; i++) {
      if (i == 5) {
          break;
      }
      System.out.println(i);
  }

This loop stops when i becomes 5.

continue

  • : Skips the current iteration and moves to the next one. Example:
  for (int i = 1; i <= 5; i++) {
      if (i == 3) {
          continue;
      }
      System.out.println(i);
  }

Here, the value 3 is skipped.

return

  • : Exits from the current method and optionally returns a value to the caller. Example:
  int add(int a, int b) {
      return a + b;
  }

Branching statements are useful for:

  • stopping loops when a condition is met
  • skipping invalid data
  • exiting methods early
  • improving control and readability

In Java, switch is also often grouped with control flow because it selects one block among many possible cases. It is especially useful when checking a variable against many fixed values.

Example:

int day = 3;

switch (day) {
    case 1:
        System.out.println("Monday");
        break;
    case 2:
        System.out.println("Tuesday");
        break;
    case 3:
        System.out.println("Wednesday");
        break;
    default:
        System.out.println("Invalid day");
}

Working / Process

1. Evaluate the condition

  • The program first checks a logical expression such as x > 10 or marks >= 50.
  • Depending on the result, the program decides what to do next.

2. Select the control path

  • If the condition is true, one block executes.
  • If it is false, another block may execute, or the program may move to the next statement.
  • In loops, the condition is checked repeatedly until it becomes false.

3. Transfer control accordingly

  • The execution may continue sequentially, repeat from the top of a loop, jump out of a block using break, skip one iteration using continue, or exit a method using return.

A general control-flow pattern looks like this:

Start
  |
  v
Condition?
 /      \
Yes      No
 |        |
 v        v
Execute  Next step / stop
 block
  |
  v
Repeat or finish

Advantages / Applications

Makes programs intelligent and responsive

Control statements allow Java programs to react to input, conditions, and changing situations instead of executing blindly in one fixed order.

Reduces code repetition and improves efficiency

Loops allow repeated tasks to be written once and executed many times, making code shorter, cleaner, and easier to maintain.

Supports real-world decision logic

They are used in banking systems, login validation, grading systems, traffic signal simulations, game logic, form validation, menu-driven applications, and many other practical programs.


Summary

  • Control statements manage the flow of execution in a Java program.
  • They include decision-making, looping, and branching mechanisms.
  • They are essential for making programs flexible, repetitive, and condition-based.
  • Important terms to remember: if, if-else, switch, for, while, do-while, break, continue, return