This post is part of our Java Programming: A Comprehensive Guide for Beginners series.
Control flow statements are essential for directing the flow of execution in a Java program. Conditional statements help make decisions based on conditions, looping statements facilitate repeated execution of code, and branching statements control the flow within loops and methods. A solid understanding of these control flow constructs is crucial for writing effective and logically structured Java programs. By mastering control flow statements, developers can create programs that are not only functional but also efficient and maintainable. Continuous practice and exploration of these statements contribute to becoming a proficient Java programmer.
18.1 Introduction
Control flow statements are fundamental structures in Java programming that determine the order of execution of statements within a program. This chapter explores various control flow statements, including conditional statements (if, else, switch), looping statements (for, while, do-while), and branching statements (break, continue, return).18.2 Conditional Statements
18.2.1 if Statement
The if statement is used to execute a block of code if a specified condition evaluates to true.public class IfStatementExample {
public static void main(String[] args) {
int x = 10;
if (x > 5) {
System.out.println("x is greater than 5");
}
}
}
18.2.2 if-else Statement
The if-else statement allows the program to execute one block of code if the condition is true and another block if the condition is false.Code Example: if-else Statement
public class IfElseStatementExample {
public static void main(String[] args) {
int x = 3;
if (x % 2 == 0) {
System.out.println("x is even");
} else {
System.out.println("x is odd");
}
}
}
18.2.3 switch Statement
The switch statement allows a variable to be tested for equality against a list of values. It provides a more concise way to express multiple if-else statements.Code Example: switch Statement
public class SwitchStatementExample {
public static void main(String[] args) {
int dayOfWeek = 3;
switch (dayOfWeek) {
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;
default:
System.out.println("Weekend");
}
}
}
18.3 Looping Statements
18.3.1 for Loop
The for loop is used to iterate a statement or a block of statements repeatedly.Code Example: for Loop
public class ForLoopExample {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
System.out.println("Iteration " + i);
}
}
}
18.3.2 while Loop
The while loop continues to execute a block of statements as long as the specified condition evaluates to true.Code Example: while Loop
public class WhileLoopExample {
public static void main(String[] args) {
int count = 1;
while (count <= 5) {
System.out.println("Count: " + count);
count++;
}
}
}
18.3.3 do-while Loop
The do-while loop is similar to the while loop, but it guarantees that the block of code is executed at least once before checking the condition.Code Example: do-while Loop
public class DoWhileLoopExample {
public static void main(String[] args) {
int count = 1;
do {
System.out.println("Count: " + count);
count++;
} while (count <= 5);
}
}
18.4 Branching Statements
18.4.1 break Statement
The break statement is used to terminate the loop or switch statement it is in.Code Example: break Statement
public class BreakStatementExample {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
if (i == 5) {
System.out.println("Breaking the loop at i=" + i);
break;
}
System.out.println("Iteration " + i);
}
}
}
18.4.2 continue Statement
The continue statement is used to skip the rest of the code inside a loop for the current iteration and jump to the next iteration.Code Example: continue Statement
public class ContinueStatementExample {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
if (i == 3) {
System.out.println("Skipping iteration at i=" + i);
continue;
}
System.out.println("Iteration " + i);
}
}
}
18.4.3 return Statement
The return statement is used to explicitly return from a method, and it also terminates the execution of the method.Code Example: return Statement
public class ReturnStatementExample {
public static void main(String[] args) {
int result = sum(5, 3);
System.out.println("Sum: " + result);
}
private static int sum(int a, int b) {
return a + b;
}
}
0 comments:
Post a Comment