Control Structures in Java
Control Structures in Java
if Statement
Use the if statement to specify a block of Java code to be executed if a condition is true.
Syntax:
if (condition) {
// block of code to be executed if the condition is true
}
else Statement
Use the else statement to specify a block of code to be executed if the condition is false.
Syntax:
if (condition) {
// block of code to be executed if the condition is true
} else {
// block of code to be executed if the condition is false
}
else if Statement
Use the else if statement to specify a new condition if the first condition is false.
Syntax:
if (condition1) {
// block of code to be executed if condition1 is true
} else if (condition2) {
// block of code to be executed if the condition1 is false and condition2 is true
} else {
// block of code to be executed if the condition1 is false and condition2 is false
}
Syntax:
variable = (condition) ? expressionTrue : expressionFalse;
Example
int time = 20;
String result = (time < 18) ? "Good day." : "Good evening.";
System.out.println(result);
Syntax:
switch(expression) {
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}
The switch expression is evaluated once.
The value of the expression is compared with the values of each case.
If there is a match, the associated block of code is executed.
The break and default keywords are optional, and will be described later in this chapter
break Keyword
When Java reaches a break keyword, it breaks out of the switch block.
This will stop the execution of more code and case testing inside the block.
while Loop
The while loop loops through a block of code as long as a specified condition is true:
Syntax:
while (condition) {
// code block to be executed
}
Example:
int i = 0;
while (i < 5) {
System.out.println(i);
i++;
}
do/while Loop
The do/while loop is a variant of the while loop. This loop will execute the code block once, before checking if the
condition is true, then it will repeat the loop as long as the condition is true.
Syntax:
do {
// code block to be executed
}
Example:
int i = 0;
do {
System.out.println(i);
i++;
}
while (i < 5);
for Loop
When you know exactly how many times you want to loop through a block of code, use the for loop instead of
a while loop:
Syntax:
for (statement 1; statement 2; statement 3) {
// code block to be executed
}
Statement 1 is executed (one time) before the execution of the code block.
Statement 2 defines the condition for executing the code block.
Statement 3 is executed (every time) after the code block has been executed.
Example:
for (int i = 0; i < 5; i++) {
System.out.println(i);
}
for-each Loop
There is also a "for-each" loop, which is used exclusively to loop through elements in an array:
Syntax:
for (type variableName : arrayName) {
// code block to be executed
}
Example:
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
for (String i : cars) {
System.out.println(i);
}
Java Arrays
Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value.
To declare an array, define the variable type with square brackets:
String[] cars;
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
int[] myNum = {10, 20, 30, 40};
Example:
for (int i = 0; i < 10; i++) {
if (i == 4) {
break;
}
System.out.println(i);
}
Example
for (int i = 0; i < 10; i++) {
if (i == 4) {
continue;
}
System.out.println(i);
}
******