Control Flow: Conditionals & Loops
Duration: 7.0 min
Control Flow: Conditionals & Loops
Control flow determines the order in which statements execute. Java provides if/else for conditionals and for/while loops for repetition. Mastering control flow is essential for writing programs that respond to different conditions and process data efficiently. Understanding how to structure your code with proper control flow makes it more readable and maintainable.
public class IfElse {
public static void main(String[] args) {
int age = 20;
if (age >= 18) System.out.println("Adult");
else System.out.println("Minor");
}
}Adultpublic class IfElseIf {
public static void main(String[] args) {
int score = 75;
if (score >= 90) System.out.println("A");
else if (score >= 80) System.out.println("B");
else if (score >= 70) System.out.println("C");
else System.out.println("F");
}
}Cpublic class Switch {
public static void main(String[] args) {
int day = 3;
switch (day) {
case 1: System.out.println("Mon"); break;
case 2: System.out.println("Tue"); break;
case 3: System.out.println("Wed"); break;
default: System.out.println("Other");
}
}
}Wedpublic class ForLoop {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) System.out.println(i);
}
}1
2
3
4
5public class WhileLoop {
public static void main(String[] args) {
int i = 1;
while (i <= 3) {
System.out.println(i);
i++;
}
}
}1
2
3public class DoWhile {
public static void main(String[] args) {
int i = 1;
do {
System.out.println(i);
i++;
} while (i <= 3);
}
}1
2
3public class BreakContinue {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
if (i == 3) continue;
if (i == 5) break;
System.out.println(i);
}
}
}1
2
4public class NestedLoop {
public static void main(String[] args) {
for (int i = 1; i <= 2; i++)
for (int j = 1; j <= 2; j++)
System.out.println("i=" + i + ", j=" + j);
}
}i=1, j=1
i=1, j=2
i=2, j=1
i=2, j=2public class Ternary {
public static void main(String[] args) {
int age = 20;
String status = (age >= 18) ? "Adult" : "Minor";
System.out.println(status);
}
}Adultpublic class EnhancedFor {
public static void main(String[] args) {
int[] nums = {1, 2, 3, 4, 5};
for (int num : nums) System.out.println(num);
}
}1
2
3
4
5public class LoopLabels {
public static void main(String[] args) {
outer: for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
if (i == 2 && j == 2) break outer;
System.out.println("i=" + i + ", j=" + j);
}
}
}
}i=1, j=1
i=1, j=2
i=1, j=3
i=2, j=1💡 Tip: Best Practice: Use break in switch statements to prevent fall-through to the next case.
💡 Tip: Common Mistake: Infinite loops occur when the loop condition never becomes false. Always ensure your loop has a proper exit condition.
Learn more: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/if.html
❓ What does the if statement do?
❓ Which keyword is used to exit a loop?
❓ What is the difference between while and do-while?
❓ What does continue do in a loop?
❓ Which statement is used for multiple conditions?
❓ What is the output of the ternary operator (true ? "yes" : "no")?
❓ How many times does this loop execute: for(int i=0; i<5; i++)?
❓ What is a nested loop?
❓ Which loop is best for iterating a fixed number of times?
❓ What happens if you forget break in a switch case?