Chapter 4 Control Flow in Java
Control flow is fundamental in programming as it governs the order of execution of the program. This chapter will delve into conditional statements (if, else, switch) and loop constructs (for, while, do-while) in Java.
Conditional Statements¶
Conditional statements make decisions in your code depending on certain conditions.
if Statement¶
The if statement checks a condition and executes a block of code if the condition is true:
if (condition) {
// Code to be executed if the condition is true
}
int temperature = 35;
if (temperature > 30) {
System.out.println("It's a hot day!");
}
In this example, the condition inside the if statement is temperature > 30. As the value of temperature is 35, the condition is true, so the statement "It's a hot day!" gets printed.
else Statement¶
The else statement is used with an if statement to provide an alternative action when the if condition is not met (is false):
if (condition) {
// Code to be executed if the condition is true
} else {
// Code to be executed if the condition is false
}
Building upon the previous example, we can add an else statement to print "It's not a hot day." when the temperature is 30 or below:
int temperature = 25;
if (temperature > 30) {
System.out.println("It's a hot day!");
} else {
System.out.println("It's not a hot day.");
}
switch Statement¶
A switch statement allows a variable to be tested for equality against a list of values:
switch(variable) {
case value1:
// Code to be executed if variable equals value1
break;
case value2:
// Code to be executed if variable equals value2
break;
// More cases...
default:
// Code to be executed if variable doesn't match any values
}
switch statement like this:
int dayOfWeek = 1;
switch(dayOfWeek) {
case 1:
System.out.println("It's Monday");
break;
case 2:
System.out.println("It's Tuesday");
break;
// More cases...
default:
System.out.println("Invalid day");
}
Loop Statements¶
Looping constructs in Java allow you to execute a block of code multiple times, which is especially useful when working with collections of data.
for Loop¶
A for loop is used when you know how many times the loop should run:
for (initialization; condition; increment/decrement) {
// Code to be executed on each loop
}
for loop that prints the numbers from 1 to 5:
for (int i = 1; i <= 5; i++) {
System.out.println(i);
}
while Loop¶
A while loop is used when you want to loop through a block of code as long as a certain condition is met:
while (condition) {
// Code to be executed as long as the condition is true
}
For instance, if you want to print the numbers from 1 to 5 with a while loop, you could do:
int i = 1;
while (i <= 5) {
System.out.println(i);
i++;
}
do-while Loop¶
A do-while loop is similar to the while loop, but it tests the condition after executing the code block, so the block gets executed at least once:
do {
// Code to be executed
} while (condition);
The same task can be achieved with a do-while loop as follows:
int i = 1;
do {
System.out.println(i);
i++;
} while (i <= 5);
Enhanced for Loop (for-each Loop)¶
The enhanced for loop, or the for-each loop, is another looping construct in Java primarily designed for iterating over arrays or collections (like lists):
for (type variable : array) {
// Code to be executed
}
In this syntax, type is the data type of the items in the array, variable is a new variable to represent each item in the array, and array is the array that you want to iterate over.
For example, if we have an array of integers and we want to print each number, we could use an enhanced for loop like this:
int[] numbers = {1, 2, 3, 4, 5};
for (int number : numbers) {
System.out.println(number);
}
In this case, number represents each item in the numbers array. During each iteration, number takes on the value of the current item, and the System.out.println(number); statement prints the current number. So, this code will print the numbers from 1 to 5, one per line.
The enhanced for loop is easier to write and read when you need to iterate over every item in an array or a collection, but it provides less flexibility than the traditional for loop. For example, you can't use the enhanced for loop when you need the index of the current item or when you want to iterate over a subset of the array.
You've now learned about controlling the flow of your Java programs using conditional statements and loops. These constructs allow your programs to make decisions and execute code repetitively.