1/25
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai |
|---|
No analytics yet
Send a link to your students to track their progress
Control Structures
Statements that control the flow of execution in a program. They allow decisions, repetitions, and jumps in the program sequence.
Types of Control Structures
Java has three main types of control structures:
Selection – decision-making (if, if-else, switch)
Repetition – looping (while, do-while, for)
Branching – flow control (break, continue, return)
Selection Statements
Decision-making statements used to control which set of instructions should be executed based on conditions. Includes if, if-else, and switch statements.
if Statement
Executes a block of code only if a given condition is true.
Example:
if (a % 2 == 0) {
System.out.println("Even number");
}if-else Statement
Executes one block of code if the condition is true and another block if the condition is false.
Example:
if (a % 2 == 0)
System.out.println("Even");
else
System.out.println("Odd");switch Statement
Used for decision-making when a variable has multiple possible values. It is an easier alternative to multiple if-else statements.
Example:
switch (day) {
case 1: System.out.println("Monday"); break;
case 2: System.out.println("Tuesday"); break;
default: System.out.println("Invalid");
}case Keyword
Defines each possible value for a switch variable and the code block that executes when matched.
break in Switch
Terminates the current case in a switch statement, preventing the code from “falling through” to the next case.
default Keyword
A case in the switch statement that executes when none of the other case values match.
Rules for switch Statements
Case constants must be unique.
Data type of cases must match the switch variable.
default is optional but should be last.
Repetition Statements
Used to repeat a block of code multiple times as long as a condition is true. Also called loops.
Loop Control Variable
The variable that determines how many times a loop runs, usually appearing in the loop condition.
while Loop
A pre-test loop that repeats code while the condition is true. The condition is checked before each iteration.
Example:
int i = 1;
while (i <= 5) {
System.out.println(i);
i++;
}do-while Loop
A post-test loop that executes the block once before checking the condition. Ensures the loop body runs at least once.
Example:
int j = 1;
do {
System.out.println(j);
j++;
} while (j <= 10);for Loop
A loop used when the number of repetitions is known. It combines initialization, condition, and update in one line.
Syntax:
for (init; condition; update) {
statement;
}Components of a for Loop
Initialization – sets starting value
Test/Condition – determines whether loop continues
Increment/Decrement – updates loop variable after each iteration
Common for Loop Mistake
Adding a semicolon right after the for statement causes the loop to execute incorrectly or skip its block.
Iteration
One complete execution of a loop’s body.
Branching Statements
Statements that transfer control to another part of the program. Includes break, continue, and return.
break Statement
Exits a loop or switch statement immediately. It stops the current control structure’s execution.
Labeled Break
A form of break statement that allows you to exit from an outer loop in nested loops.
continue Statement
Skips the rest of the current loop iteration and moves to the next iteration of the loop.
return Statement
Ends a method’s execution and optionally sends a value back to the method’s caller.
Forms of return Statement
return value; → returns a value
return; → returns nothing (used in void methods)
return expression; → returns an evaluated value
Example of return Usage
public static void hello() {
System.out.println("Hello " + welcome());
}
static String welcome() {
return "Welcome to Java Programming";
}Nested Loop
A loop placed inside another loop. Commonly used in multiplication tables or matrix operations.