Java Controls & Flow – Quick Reference
1. Conditional Statements
| Statement | Syntax | Example |
|---|---|---|
| if | if (condition) { ... } | if (x > 0) { System.out.println("Positive"); } |
| if-else | if (condition) { ... } else { ... } | if (x > 0) { ... } else { ... } |
| if-else-if | if (...) {...} else if (...) {...} else {...} | if (score >= 90) { ... } else if (score >= 75) { ... } else { ... } |
| nested if | if (...) { if (...) {...} } | if (x>0){ if(x%2==0){...} } |
| switch | switch(var){ case val: ...; break; default: ...; } | switch(day){ case 1: ... break; default: ... } |
2. Loops
| Loop | Syntax | Example |
|---|---|---|
| for | for(init; condition; update){...} | for(int i=1;i<=5;i++){...} |
| while | while(condition){...} | while(i<=5){ i++; } |
| do-while | do{...} while(condition); | do{ i++; } while(i<=5); |
| for-each | for(type var : array/collection){...} | for(int n: numbers){...} |
3. Jump Statements
| Statement | Use |
|---|---|
| break | Exit loop or switch immediately |
| continue | Skip current iteration, continue next |
| return | Exit method immediately |
4. Ternary Operator
| Syntax | Example |
|---|---|
condition ? valueIfTrue : valueIfFalse | String res = (x%2==0) ? "Even" : "Odd"; |
5. Flow Control Tips
- Always use
{}for clarity. - Avoid deep nesting; break into methods.
- Use
switchfor discrete values. - Use
for-eachfor collections when index not needed. - Name variables clearly for readability.
6. Quick Visual Example
java
for (int n : numbers) {
if (n > 0) {
System.out.println("Positive");
} else if (n < 0) {
System.out.println("Negative");
} else {
System.out.println("Zero");
}
}✅ Memory Hooks:
- if → condition check
- switch → multi-choice
- for / while / do-while → loops
- break / continue / return → jump control
- ?: → quick if-else