Control Structure
In Java, control structures are fundamental building blocks that govern the flow of execution within a program. They provide the mechanisms that allow you to control how and when different parts of your program are executed based on specified conditions. Control structures are pivotal in developing simple to complex applications, enabling decision-making, looping over code blocks, and the sequential execution of statements.
Java control structures are classified into three categories: sequential, selection, and looping. Sequential control is the most straightforward, where code is executed line by line in the order it appears in the program. Selection control structures like if, if-else, else-if, and switch allows decision-making based on conditions. Looping control structures such as for, while, do-while, and for-each can repeatedly execute a code block. These control structures’ careful and clever use is the key to creating efficient and effective Java programs.
Example
public class ControlStructures {
public static void main(String[] args) {
// Sequence Structure
int x = 1;
int y = x + 2;
System.out.println("x is " + x + ", y is " + y); // Output: x is 1, y is 3
// Selection Structures
// If-then
if (x > y) {
System.out.println("x is greater than y"); // This line will not be executed
}
// If-then-else
if (x > y) {
System.out.println("x is greater than y"); // This line will not be executed
} else {
System.out.println("x is not greater than y"); // Output: x is not greater than y
}
// If-then-else-if
if (x > y) {
System.out.println("x is greater than y"); // This line will not be executed
} else if (x == y) {
System.out.println("x is equal to y"); // This line will not be executed
} else {
System.out.println("x is less than y"); // Output: x is less than y
}
// Switch
switch (x) {
case 1:
System.out.println("x is 1"); // Output: x is 1
break;
case 2:
System.out.println("x is 2"); // This line will not be executed
break;
default:
System.out.println("x is neither 1 nor 2"); // This line will not be executed
break;
}
// Loop Structures
// While loop
while (x < 5) {
System.out.print(x + " - "); // Output: 1 - 2 - 3 - 4 -
x++;
}
System.out.println(x); // Output: 5
// Do-while loop
x = 0;
do {
System.out.print(x + " - "); // Output: 0 - 1 - 2 - 3 - 4 -
x++;
} while (x < 5);
System.out.println(x); // Output: 5
// For loop
for (int i = 0; i < 5; i++) {
System.out.print(i + " "); // Output: 0 1 2 3 4
}
System.out.println();
// For-each loop
int[] arr = { 1, 2, 3, 4, 5 };
for (int num : arr) {
System.out.print(num + " "); // Output: 1 2 3 4 5
}
System.out.println();
String str = "HelloWorld!";
for (String ch : str.split("")) {
System.out.print(ch + " "); // Output: H e l l o W o r l d !
}
System.out.println();
}
}Here is a list of explanations for each section of the code:
public class ControlStructures: This line declares a public class namedControlStructures. A class in Java is a blueprint for creating objects (a particular data structure).public static void main(String[] args): The main method serves as the entry point for the program. The Java Virtual Machine (JVM) calls the main method when the program starts.// Sequence Structure: This comment indicates that the code following it will demonstrate a sequence control structure.int x = 1; int y = x + 2;: These lines initialize an integer variablexwith a value of1, and an integer variableywith a value of3.System.out.println("x is " + x + ", y is " + y);: This line prints the string “x is 1, y is 3” to the console.// Selection Structures: This comment indicates that the code following it will demonstrate selection control structures.if (x > y) {...}: This is anifstatement. It checks whetherxis greater thany. Ifxis greater thany, the code inside the braces{}is executed. However, in this case,xis not greater thany, so the code inside the braces will not be executed.if (x > y) {...} else {...}: This is anif-elsestatement. It checks whetherxis greater thany. Ifxis greater thany, it executes the code in the first braces{}. If not, it executes the code in theelsebraces. In this case,xis not greater thany, so it prints “x is not greater than y”.if (x > y) {...} else if (x == y) {...} else {...}: This is anif-else-if-elsestatement. It checks whetherxis greater thany, and ifxis equal toy. If neither of these conditions is true, it executes the code in theelsebraces. In this case,xis less thany, so it prints “x is less than y”.switch (x) {...}: This is aswitchstatement. It checks the value ofxand executes the code corresponding to the matchingcase. Here, sincexis1, it prints “x is 1”.// Loop Structures: This comment indicates that the code following it will demonstrate loop control structures.while (x < 5) {...}: This is awhileloop. It repeats the block of code within the braces as long asxis less than5. Here, it printsxand incrementsxon each iteration.do {...} while (x < 5);: This is ado-whileloop. Unlike thewhileloop, this loop checks its condition at the end of the loop, ensuring that the loop is executed at least once. In this case, it resetsxto0and then printsx, incrementingxuntilxbecomes5.for (int i = 0; i < 5; i++) {...}: This is aforloop. It initializes an integerito0and repeats the loop untiliis less than5. The loop incrementsiby1at the end of each iteration. It printsion each iteration.for (int num : arr) {...}: This is afor-eachloop, used to iterate over an array or a collection. It prints each number in the arrayarr.for (String ch : str.split("")) {...}: This is anotherfor-eachloop. It splits the stringstrinto an array of substrings where each substring contains a single character, then iterates over this array, printing each character.