Another new face emerges — @phvismin just made waves with 16 #NFTs 🌊🔥 Huge thanks for bringing such strong energy into the NFToa ecosystem. Moves like this push us all forward 🙌 #eCash $XEC #NFTCommunity #NFTCollection #NFTCollectors #nftprimearts #CryptoMarket #CryptoRecovery pic.twitter.com/T9zkiKPDGj
— NFToa (@nftoa_) September 2, 2025
As in VB, Java also provides program control structures for selection and repetition. The commands used in these control structures are also almost similar.

Program Control Structure
1. Election Structure
The selection structure can use if, if ... else, and if ...else ... if. This is not much different from what you have learned in Chapter 5 and Chapter 7. Consider the following program code snippets.
Example 8.6. Using if
int nilai = 68;
if( nilai > 60 ) System.out.println("Selamat anda lulus!");Example 8.6 uses only if to create a selection structure. If the value is more than 60 then the program will display the output "Congratulations you passed!"
Example 8.7. Using if ... else
int nilai = 68;
if(nilai > 60) System.out.println("Selamat anda lulus!");
else System.out.println("Anda tidak lulus!");In example 8.7, we use the if ... else structure. If the value is more than 60 then it will display the output "Congratulations you passed!" but if not (else) then the program will display the output "You failed!".
Example 8.8. Using if ... else ... if
int nilai = 68;
if( nilai > 90 ){
System.out.println("Nilai anda sangat baik!");
}
else if( nilai > 60 ){
System.out.println("Nilai anda baik!");
} else{
System.out.println("Anda tidak lulus");
}Example 8.8 is an extension of example 8.7. If the value is more than 90 then the program will display the output "Your value is very good!", but if it is less than 90 and more than 60 (else if) then the program will display the output "Your value is good!" and if not both (else) then the program will display the output "You failed"
The selection structure also allows us to select many alternatives. However, if using if it will be very complex. Java provides the switch command. This command has the same function as Select .. case in VB. Consider the following example.
Example 8.9. Use of switches
public class SwitchControl {
public static void main(String[] args) { int a=2; int b; switch(a) { case 1: b = a + 1; break; case 2: b = a + 2; break; case 3: b = a + 3; break; case 4: b = a + 4; break; default: b = 0;
}
System.out.println("Nilai b: " + b);
}
}What is the result of the program code in example 8.9 above? If your answer is 4, it means you have understood how the switch command works. Switch will check whether there is a case that has the same value as a, namely 2. The check starts from the first case, namely 1. The break statement must be written to stop the search for the next case. Try removing the break statement and running the program code above. What are the results?
2. Repetition Structure
There are three forms of looping structures in Java, namely for, while and dowhile. In principle, these looping forms are the same as what you have learned in Chapters 5 and 7. Here are examples of looping forms.
Example 8.10. Using for in Java
public class ForLoop {
public static void main(String[] args) { int j=4;
for (int x=0; x < 5; x++) {
System.out.println("Nilai x: " + x);
System.out.println("Nilai j: " + j);
System.out.println();
j--;
}
}
}The general syntax for is: for (initial value; condition; increment) then continued with the part to be repeated. Pay close attention to how to use the for structure in example 8.10 above. The value x = 0 is the initial value. While x < 5 is the condition that must be met for the repetition to be done. The value x ++ is the increment. Remember that writing x ++ has the same meaning as x = x + 1. Try running the program code above. What do you think the output of the program is?
Example 8.11. Using while in Java
public class WhileLoop {
public static void main(String[] args) {
int y = 4;
while ( y > 0 ){
System.out.print(y); y--;
}
}
}In this example 8.11 we use while to create a loop. In while we need to initialize the variables before entering the while part. We initialize the variable y with the value 4. The condition that must be met in while is y>0. In this example the counter is decreasing (note the y-- part). So what will be printed on the screen is 4321. What if we remove the counter line (y--)? How many times will the loop occur?
Example 8.12. Using do-while in Java
public class ContohDoWhile {
public static void main(String[] args) { int z=3;
do {
System.out.println("Java"); z++; } while (z < 6);
}
}Example 8.12 shows how we can use do-while to repeat the printing of the word "Java". Note the syntax of do-while in this example. Do-while also requires initialization and a counter in order to loop. Try running the program above. How many times will the word "Java" be printed? Now replace the condition in while with z < 1. Does the program still print the word "Java"? Why? Reread Chapter 7 on loop control structures to understand this.
3. Using Break and Continue
The break statement has three functions, namely:
- Stops the selection on a switch statement.
- Stops the looping process or exits the loop body.
- Exit a specific label block.
We have learned about the use of break in switch statements in the selection structure. We will now learn how to use break in repetition. Consider the following example.
Example 8.13. Use of breaks in repetition
class BreakPengulangan {
public static void main(String[] args) {
for (int i=0; i<10; i++) { if (i == 5) { break;
}
System.out.println("baris ke-" + i);
}
System.out.println("Ini setelah break pengulangan");
}
}In example 8.13, the loop should occur 10 times (starting from 0 to 9). However, because there is a statement if (1 == 5) {break;}, the loop will stop when the value of i = 5. Then the program flow will exit the body and execute the command after the end of the loop.
The continue statement is used to force the program to continue the looping process. Consider the following example.
Example 8.14. Use of continue
String nama[] = {"Joni", "Riko", "Denis", "Riko"}; int hitung = 0;
for( int i=0; i<names.length; i++ ){ if( !nama[i].equals("Riko") ){ continue;
} hitung++;
}
System.out.println("Ada " + hitung + " Riko dalam daftar");Example 8.14 is a program fragment to find the number of names Riko in a collection of names. The statement if(!names[i].equals("Riko") means if the contents of the name variable are not "Riko" then run the continue command. The placement of this continue statement forces the program to repeat directly without having to run the command below continue. This means that the hitung++ line will not be executed. So if we execute the program code above the result is 2.
