Control statements in Java determine the flow of execution in a program. They allow decisions, loops, and branching, making your programs dynamic and intelligent.


🧭 Types of Control Statements

  1. Decision-making statementsif, if-else, switch

  2. Looping statementsfor, while, do-while

  3. Jumping statementsbreak, continue, return


1️⃣ Decision-Making Statements

if Statement

Executes a block if a condition is true.

java
if (marks > 40) { System.out.println("Passed"); }

πŸ”„ if-else Statement

Executes one of two blocks depending on the condition.

java
if (marks >= 40) { System.out.println("Passed"); } else { System.out.println("Failed"); }

πŸ”€ if-else-if Ladder

Used when you have multiple conditions.

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

πŸ§ͺ switch Statement

Selects one of many code blocks based on a variable’s value.

java
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"); }

2️⃣ Looping Statements

πŸ” for Loop

Used when the number of iterations is known.

java
for (int i = 1; i <= 5; i++) { System.out.println("Count: " + i); }

πŸ”„ while Loop

Used when the condition is checked before loop execution.

java
int i = 1; while (i <= 5) { System.out.println("Count: " + i); i++; }

↩️ do-while Loop

Executes the block at least once, then checks the condition.

java
int i = 1; do { System.out.println("Count: " + i); i++; } while (i <= 5);

3️⃣ Jumping Statements

break

Exits from a loop or switch.

java
for (int i = 1; i <= 10; i++) { if (i == 5) break; System.out.println(i); }

⏭️ continue

Skips the current iteration.

java
for (int i = 1; i <= 5; i++) { if (i == 3) continue; System.out.println(i); }

πŸ”š return

Ends the current method and optionally returns a value.

java
public int add(int a, int b) { return a + b; }

πŸ§ͺ Sample Program

java
public class ControlExample { public static void main(String[] args) { int number = 3; switch(number) { case 1: System.out.println("One"); break; case 2: System.out.println("Two"); break; default: System.out.println("Other"); } for (int i = 1; i <= 3; i++) { System.out.println("Loop: " + i); } } }

πŸ“Œ Summary Table

StatementPurpose
if / elseConditional execution
switchMultiple conditions
for, while, do-whileRepeating actions
break, continueControlling loop execution
returnExiting a method