CONTROL STRUCTURES IN JAVA

🧠 CONTROL STRUCTURES IN JAVA

This topic is super important kasi ito β€˜yung foundation ng how your program decides, repeats, and changes flow habang tumatakbo.
In short, this is what makes your program β€œsmart” and not just a list of boring instructions.


🧩 What Are Control Structures?

Control structures decide how and when parts of your program are executed.
Without them, your code would just run from top to bottom β€” no logic, no decisions.

Java has 3 main types of control structures:

  1. Selection – decision-making (if, if-else, switch)

  2. Repetition – looping (while, do-while, for)

  3. Branching – control transfer (break, continue, return)


🧭 1⃣ SELECTION STATEMENTS

Used for decision-making β€” choosing what to do based on conditions.


🧱 if Statement

Executes a block of code only if a certain condition is true.

Syntax:

if (boolean-expression) {
   statements;
}

Example:

int a = 16;
if (a % 2 == 0) {
   System.out.println("This is an even number");
}

βœ… If the condition (a % 2 == 0) is true, the message prints.
🚫 If false, nothing happens.

Real-life analogy:
If it’s raining β†’ bring umbrella β˜”
If not β†’ do nothing.


🧱 if-else Statement

Gives you two possible paths β€” one if true, one if false.

Syntax:

if (condition) {
   statements;
} else {
   statements;
}

Example:

int a = 16;
if (a % 2 == 0) {
   System.out.println("This is an even number");
} else {
   System.out.println("It is an odd number");
}

Shortcut tip:
🧩 β€œIf-else” = choose between two possibilities (like yes/no, pass/fail, etc.)


🎚 switch Statement

Used when you have many possible values for a variable β€” easier than writing many if-else’s.

Syntax:

switch (variable) {
   case value1:
      statements;
      break;
   case value2:
      statements;
      break;
   default:
      statements;
}

Example:

int day = 5;
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;
   default: System.out.println("Invalid entry");
}

πŸ—“ Output: Friday
Because day = 5.

Notes:

  • break stops the execution (or else the next case will run too).

  • default runs when no match is found.

  • Data type for case must match switch variable.

Real-life analogy:
Parang menu selector β€” pick 1, 2, or 3 β†’ gets a specific output πŸ”πŸ₯€πŸŸ


πŸ” 2⃣ REPETITION STATEMENTS (LOOPS)

Used for repeating tasks without writing the same code many times.

Java has 3 main loops:

  • while

  • do-while

  • for


πŸŒ€ while Loop

Repeats code while a condition is true. (Pre-test loop)

Syntax:

while (condition) {
   statement;
}

Example:

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

🧩 Output: 1 2 3 4 5
It stops once i becomes 6 (condition false).

Remember: Check condition first β†’ then execute.


πŸ”„ do-while Loop

Similar to while, but this one executes the block at least once before checking the condition. (Post-test loop)

Syntax:

do {
   statement;
} while (condition);

Example:

int j = 1;
do {
   System.out.println(j);
   j++;
} while (j <= 10);

πŸ’‘ Even if condition is false, the code inside do still runs once!


πŸ”‚ for Loop

Used when the number of iterations is known ahead of time.

Syntax:

for (initialization; condition; update) {
   statement;
}

Example:

for (int i = 1; i <= 10; i++) {
   System.out.print(i * i + " ");
}

🧩 Output: 1 4 9 16 25 36 49 64 81 100

Parts Explained:

Part

Purpose

Initialization

Set starting value

Condition

Decide if loop continues

Increment/Decrement

Change loop variable after each iteration

⚠ Common mistake: Adding a semicolon after for β€”

for (int i=1; i<=10; i++); // ❌ don’t do this

It makes the loop run incorrectly or skip content!


3 BRANCHING STATEMENTS

Used to jump or transfer control to another part of the program.


πŸšͺ break Statement

Ends a loop or switch immediately.

Example:

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

🧩 Output: 1 2 3 4
β†’ It stops when i == 5.

Used in:

  • switch statements

  • for, while, and do-while loops


⏭ continue Statement

Skips the current loop iteration and moves to the next one.

Example:

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

🧩 Output: 1 2 4 5
(It skipped 3)


πŸ”™ return Statement

Ends the method and optionally sends a value back to the caller.

Syntax:

return value;     // returns a value
return;           // returns nothing (for void methods)

Example:

public static void hello() {
   System.out.println("Hello " + welcome());
}

static String welcome() {
   return "Welcome to Java Programming";
}

🧩 Output: Hello Welcome to Java Programming

In short:

  • return β†’ exits a method.

  • return value β†’ gives something back.


βš– SUMMARY TABLE

Type

Statements

Function

Selection

if, if-else, switch

Decision making

Repetition

while, do-while, for

Looping or repetition

Branching

break, continue, return

Flow control or redirection


πŸ’‘ Quick Recap / Memory Tricks

🧠 β€œS-R-B” Rule:
Selection β†’ Decide
Repetition β†’ Repeat
Branching β†’ Break / Jump

✨ Mnemonic:

β€œIf I decide, I repeat, then I break.”
(if β†’ loop β†’ break, in that order πŸ˜‰)