This quick reference covers all major Java controls, conditions, and flow control mechanisms with examples.
Java Controls, Conditions, and Flow Controls Tutorial
Java provides control statements that allow you to direct the flow of your program. These statements decide the order in which code executes based on conditions or loops.
1. Conditional Statements
Conditional statements allow you to execute certain blocks of code depending on conditions.
1.1 if Statement
The simplest conditional statement. It executes a block if the condition is true.
int number = 10;
if (number > 0) {
System.out.println("The number is positive");
}Notes:
- Condition must evaluate to
boolean. - The block is enclosed in
{}(optional if single statement, but best practice to always use{}).
1.2 if-else Statement
Provides an alternative block when the condition is false.
int number = -5;
if (number > 0) {
System.out.println("Positive number");
} else {
System.out.println("Non-positive number");
}1.3 if-else-if Ladder
Used when multiple conditions need to be checked sequentially.
int score = 75;
if (score >= 90) {
System.out.println("Grade A");
} else if (score >= 75) {
System.out.println("Grade B");
} else if (score >= 50) {
System.out.println("Grade C");
} else {
System.out.println("Fail");
}1.4 Nested if Statements
An if inside another if. Useful for complex conditions.
int number = 20;
if (number > 0) {
if (number % 2 == 0) {
System.out.println("Positive even number");
} else {
System.out.println("Positive odd number");
}
}1.5 switch Statement
Used when you have multiple possible values for a variable. It's more readable than many if-else statements.
int day = 3;
String dayName;
switch (day) {
case 1:
dayName = "Monday";
break;
case 2:
dayName = "Tuesday";
break;
case 3:
dayName = "Wednesday";
break;
default:
dayName = "Invalid day";
}
System.out.println(dayName);Notes:
breakprevents fall-through to the next case.defaulthandles all other values.
2. Looping Statements (Flow Control)
Loops allow code to execute repeatedly.
2.1 for Loop
Executes a block of code a fixed number of times.
for (int i = 1; i <= 5; i++) {
System.out.println("i = " + i);
}Notes:
- Syntax:
for(initialization; condition; update)
2.2 while Loop
Executes a block while a condition is true.
int i = 1;
while (i <= 5) {
System.out.println("i = " + i);
i++;
}2.3 do-while Loop
Executes a block at least once, then repeats while a condition is true.
int i = 1;
do {
System.out.println("i = " + i);
i++;
} while (i <= 5);2.4 Enhanced for Loop (for-each)
Iterates over arrays or collections.
int[] numbers = {1, 2, 3, 4, 5};
for (int num : numbers) {
System.out.println(num);
}3. Jump Statements
Jump statements change the flow immediately.
3.1 break
Exits a loop or switch.
for (int i = 1; i <= 10; i++) {
if (i == 5) break; // exit loop
System.out.println(i);
}3.2 continue
Skips the current iteration and continues with the next.
for (int i = 1; i <= 5; i++) {
if (i == 3) continue; // skip number 3
System.out.println(i);
}3.3 return
Exits from a method immediately.
public void checkNumber(int number) {
if (number < 0) return; // exit method
System.out.println("Number is " + number);
}4. Conditional (Ternary) Operator
Short-hand for if-else.
int number = 10;
String result = (number % 2 == 0) ? "Even" : "Odd";
System.out.println(result);Syntax:
condition ? valueIfTrue : valueIfFalse5. Best Practices for Flow Control
- Always use
{}for blocks, even if optional. - Avoid deep nesting; use methods to simplify logic.
- Prefer
switchfor multiple discrete values. - Use
for-eachloops for arrays/collections where index is not needed. - Use descriptive variable names for conditions and loop counters.
6. Combined Example
public class FlowDemo {
public static void main(String[] args) {
int[] numbers = {10, -3, 0, 25, 8};
for (int num : numbers) {
if (num > 0) {
System.out.println(num + " is positive");
if (num % 2 == 0) {
System.out.println(num + " is even");
} else {
System.out.println(num + " is odd");
}
} else if (num < 0) {
System.out.println(num + " is negative");
} else {
System.out.println("Number is zero");
}
}
}
}Output:
10 is positive
10 is even
-3 is negative
Number is zero
25 is positive
25 is odd
8 is positive
8 is even