5.1 Conditional Statements (if, if-else, switch):
Conditional statements allow your program to make decisions and execute different blocks of code based on certain conditions.
- The if statement is used to execute a block of code if a certain condition is true. If the condition is false, the code block is skipped.
class IfExample{
public static void main(String[] args) {
int age = 25;
if (age >= 18) {
System.out.println("You are eligible to vote.");
}
}
}
Output: "You are eligible to vote.”
- The if-else statement allows you to specify an alternative block of code to execute when the condition is false
class IfElseExample{
public static void main(String[] args) {
int temperature = 25;
if (temperature > 30) {
System.out.println("It's a hot day.");
} else {
System.out.println("It's a moderate day.");
}
}
}
Output: "It's a moderate day.”
- The switch statement provides an alternative way to perform multiple conditional checks based on the value of an expression.
class SwitchExample {
public static void main(String[] args) {
String dayOfWeek = "Tuesday";
switch (dayOfWeek) {
case "Monday":
System.out.println("It's the beginning of the week.");
break;
case "Tuesday":
System.out.println("It's a regular workday.");
break;
case "Saturday":
case "Sunday":
System.out.println("It's the weekend!");
break;
default:
System.out.println("Invalid day.");
}
}
}
Output: "It's a regular workday.”
5.2 Looping Statements (while, do-while, for):
Looping statements allow you to repeat a block of code multiple times based on a condition.
- The while loop repeatedly executes a block of code as long as the condition is true.
class WhileExample{
public static void main(String[] args) {
int count = 1;
while (count <= 5) {
System.out.println("Count: " + count);
count++;
}
}
}
Output:
Count: 1
Count: 2
Count: 3
Count: 4
Count: 5
- The do-while loop is similar to the while loop, but it guarantees that the code block is executed at least once, even if the condition is false.
class DoWhileExample{
public static void main(String[] args) {
int num = 5;
do {
System.out.println("Number: " + num);
num--;
} while (num > 0);
}
}
Output:
Number: 5
Number: 4
Number: 3
Number: 2
Number: 1
- The for loop provides a compact way to iterate over a range of values.
class ForExample{
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
System.out.println("Value: " + i);
}
}
}
Output:
Value: 1
Value: 2
Value: 3
Value: 4
Value: 5
5.3 Breaking and Continuing Loops:
The “break” statement is used to exit a loop prematurely. It immediately terminates the loop and transfers control to the next statement after the loop.
class BreakExample{
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
if (i == 3) {
break;
}
System.out.println("Value: " + i);
}
}
}
Output:
Value: 1
Value: 2
- The
continue
statement is used to skip the rest of the loop iteration and move to the next iteration.
class ContinueExample{
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue;
}
System.out.println("Value: " + i);
}
}
}
Output:
Value: 1
Value: 2
Value: 4
Value: 5
5.4 Nested Loops and Control Statements:
You can have loops within loops, known as nested loops. This allows you to perform more complex iterations.
class NestedExample{
public static void main(String[] args) {
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 2; j++) {
System.out.println("i: " + i + ", j: " + j);
}
}
}
}
Output:
i: 1, j: 1
i: 1, j: 2
i: 2, j: 1
i: 2, j: 2
i: 3, j: 1
i: 3, j: 2