Skip to content

Control Flow Statements



Why Do We Need Control Flow Statements?

Imagine searching for your keys in a ten-room house. If you find them in the second room, you do not continue searching the remaining eight rooms. You stop immediately.

Standard loops (like for and while) are designed to run until their main condition is met. However, real-world data is unpredictable. You need the ability to manually abort a loop early, skip a specific step, or exit an entire function the exact millisecond you have your answer. Control flow statements give you this manual override.


What Are Control Flow Statements?

Definition

Control flow statements are specific keywords that alter the normal, linear execution of a program. In the context of loops and functions, the three most important keywords are break, continue, and return.


The break Statement

The break statement acts as an emergency exit for a loop.

When the computer encounters a break, it immediately terminates the innermost loop it is currently in and moves on to the code directly below the loop.

Example: Finding a specific user ID.

Code
int target = 4;for (int i = 1; i <= 10; i++) {  if (i == target) {      print("Target found!");      break; // Destroys the loop instantly  }  print("Checking " + i);}// Output: Checking 1, Checking 2, Checking 3, Target found!

Without break, the loop would uselessly continue checking numbers 5 through 10 after the target was already found.


The continue Statement

The continue statement acts as a skip button.

It does not destroy the loop. Instead, it immediately stops the current iteration, skips any remaining code below it in the loop body, and jumps straight back to the top to start the next iteration.

Example: Printing only odd numbers.

Code
for (int i = 1; i <= 5; i++) {  if (i % 2 == 0) {      continue; // Skip even numbers  }  print(i);}// Output: 1, 3, 5

When i is 2, the continue statement triggers. The print(i) line is skipped, and the loop immediately jumps up to make i equal 3.


The return Statement

The return statement is the ultimate exit.

While break and continue only affect loops, return affects entire functions. When the computer hits a return, it instantly terminates the current function, hands a final value back to whoever called it, and ignores any remaining code in the function.

Example: Early exit in a function.

Code
int calculateDiscount(int price) {  if (price <= 0) {      return 0; // Exits the entire function immediately  }   int discount = price - 10;  return discount;}

If a return is placed inside a loop, it kills the loop and the function hosting it at the exact same time.


break vs continue

StatementActionAnalogy
breakJumps completely out of the loop.Leaving the movie theater early.
continueJumps to the top of the loop for the next cycle.Fast-forwarding through a boring scene.

Common Mistakes

Watch out

The continue trap in while loops: In a for loop, continue safely triggers the automatic increment (e.g., i++). In a while loop, if you place continue before your manual increment, you create a fatal infinite loop:

Code
int i = 0;while (i < 5) {  if (i == 2) {      continue; // BUG: i never increases! Infinite loop.  }  print(i);  i++;}
  • Confusing break and return: Using break inside a function's loop just stops the loop, allowing the rest of the function to finish. Using return stops the loop and prevents the rest of the function from executing.
  • Using break outside a loop or switch: A break statement will cause a syntax error if placed inside a standalone if statement that isn't wrapped in a loop or a switch.

Control Flow in DSA

These three keywords are the backbone of algorithmic efficiency:

  • break is used to optimize searches. If an array is sorted and you are looking for 5, and you suddenly encounter 10, you break the loop because you know 5 cannot possibly exist further down.
  • continue is used heavily in graph traversals (like BFS/DFS) to say: "If we have already visited this node, continue to the next one."
  • return is the foundation of Recursion, acting as the "base case" that stops a function from calling itself infinitely.

Quick Check

  1. If a loop is designed to run 100 times, but hits a break on iteration 5, how many times does the loop run?
  2. What does continue skip?
  3. What is the difference between break and return when used inside a loop that lives inside a function?
  4. Why is continue dangerous inside a while loop if you aren't careful?

Quick Recap

  • Control flow statements override standard linear execution.
  • break aborts the entire loop and escapes to the code below.
  • continue aborts only the current iteration and jumps to the next one.
  • return instantly terminates the entire function and passes a value back.
  • Beware of infinite loops when using continue inside while loops.

Where This Leads

Code
Operators   Conditions   Loops   Control Flow (Break/Continue)   Functions

You now have total control over how data is evaluated, looped, skipped, and stopped. The next step is organizing these loops and conditions into reusable, self-contained blocks of logic. This leads directly to Functions.

Key takeaway

Control flow statements give your loops intelligence. Instead of blindly processing every piece of data, break, continue, and return allow your program to react, skip the irrelevant, and stop exactly when the job is done.