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:
Selection β decision-making (if, if-else, switch)
Repetition β looping (while, do-while, for)
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:
breakstops the execution (or else the next case will run too).defaultruns when no match is found.Data type for
casemust matchswitchvariable.
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:
switchstatementsfor,while, anddo-whileloops
β 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 π)