Conditional statements allow your program to make decisions and execute different code blocks based on conditions.
public class GradeClassifier {
public static void main(String[] args) {
int score = 85;
if (score >= 90) {
System.out.println("Grade: A");
} else if (score >= 80) {
System.out.println("Grade: B");
} else if (score >= 70) {
System.out.println("Grade: C");
} else if (score >= 60) {
System.out.println("Grade: D");
} else {
System.out.println("Grade: F");
}
}
}
public class DayOfWeek {
public static void main(String[] args) {
int day = 4; // 1=Mon, 2=Tue, ..., 7=Sun
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;
case 6:
System.out.println("Saturday");
break;
case 7:
System.out.println("Sunday");
break;
default:
System.out.println("Invalid day");
}
}
}
Loops allow you to execute a block of code repeatedly while a condition is true.
public class ForLoopExample {
public static void main(String[] args) {
// Countdown from 10 to 1
for (int i = 10; i >= 1; i--) {
System.out.println("Countdown: " + i);
}
System.out.println("Blast off!");
}
}
public class WhileLoopExample {
public static void main(String[] args) {
int sum = 0;
int number = 1;
// Sum numbers from 1 to 5
while (number <= 5) {
sum += number;
number++;
}
System.out.println("Sum: " + sum);
}
}
import java.util.Scanner;
public class DoWhileExample {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int number;
do {
System.out.print("Enter a number between 1 and 10: ");
number = scanner.nextInt();
} while (number < 1 || number > 10);
System.out.println("You entered: " + number);
scanner.close();
}
}
Loop control statements change execution from its normal sequence.
public class BreakExample {
public static void main(String[] args) {
int[] numbers = {3, 7, -2, 8, 4};
for (int num : numbers) {
if (num < 0) {
System.out.println("Found negative number: " + num);
break; // Exit loop immediately
}
System.out.println("Checking: " + num);
}
}
}
public class ContinueExample {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
if (i % 2 != 0) {
continue; // Skip odd numbers
}
System.out.println("Even number: " + i);
}
}
}
public class ReturnExample {
public static void main(String[] args) {
int number = 17;
boolean isPrime = checkPrime(number);
System.out.println(number + " is prime? " + isPrime);
}
public static boolean checkPrime(int num) {
if (num <= 1) {
return false;
}
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) {
return false; // Found a divisor, not prime
}
}
return true; // No divisors found, prime number
}
}
Tip: Use break to exit a loop entirely, continue to skip to the next iteration, and return to exit a method.
You can nest loops and conditionals inside each other to create complex control flows.
public class MultiplicationTable {
public static void main(String[] args) {
// Create a 10x10 multiplication table
for (int i = 1; i <= 10; i++) {
for (int j = 1; j <= 10; j++) {
System.out.printf("%4d", i * j);
}
System.out.println(); // New line after each row
}
}
}
public class PyramidPattern {
public static void main(String[] args) {
int rows = 5;
for (int i = 1; i <= rows; i++) {
// Print spaces
for (int j = 1; j <= rows - i; j++) {
System.out.print(" ");
}
// Print stars
for (int k = 1; k <= 2 * i - 1; k++) {
System.out.print("*");
}
System.out.println();
}
}
}
* *** ***** ******* *********
public class FizzBuzz {
public static void main(String[] args) {
for (int i = 1; i <= 20; i++) {
if (i % 3 == 0 && i % 5 == 0) {
System.out.println("FizzBuzz");
} else if (i % 3 == 0) {
System.out.println("Fizz");
} else if (i % 5 == 0) {
System.out.println("Buzz");
} else {
System.out.println(i);
}
}
}
}
Performance Tip: Be cautious with deeply nested loops as they can significantly impact performance. The time complexity is O(nk) for k levels of nesting.