Home >Java >javaTutorial >How to use Java language flow control statements
The Java language is a high-level programming language, and its flow control statements are a very important part of programming. In Java, flow control statements can be used to control the execution sequence of the program, determine conditions, and execute a certain code block in a loop to achieve program flexibility and controllability.
The flow control statements of Java language are mainly divided into three types: sequence structure, selection structure and loop structure. Next, we will introduce the use of these three flow control statements in detail.
1. Sequential structure
Sequential structure means that the program is executed sequentially in the order in which the code is written, without any judgment or loop operation. Sequential structure is the basis of Java programs, because all program execution must have an entry. The entry of the program can be regarded as both the starting point and the sequential structure.
Sample code:
public static void main(String[] args) { int a = 1; int b = 3; int c = a + b; System.out.println("a + b = " + c); }
The above code demonstrates the basic usage of the sequential structure. The program is executed in the order of the code. First, calculate the sum of a and b, then assign the result to c, and finally Output the value of c.
2. Selection structure
The selection structure determines the order of program execution based on whether the judgment condition is true or false, and is divided into if statements, switch statements and ternary operators. Let's take a look at how to use these three selection structures separately.
1. if statement
The if statement can determine whether to execute a certain code block based on the true or false condition. Its basic syntax is as follows:
if (条件) { 执行语句; }
Sample code:
public static void main(String[] args) { int age = 18; if (age >= 18) { System.out.println("你已成年"); } else { System.out.println("你未成年"); } }
The above code demonstrates the basic usage of the if statement. It determines whether you are an adult based on the value of age. If you are an adult, it will output "You are an adult", otherwise it will output "You are underage".
2. Switch statement
The switch statement also determines whether to execute a certain code block based on the true or false condition, but its judgment condition is fixed and can only be integer, character, and Enumeration type, its basic syntax is as follows:
switch (表达式) { case 常量1: 执行语句; break; case 常量2: 执行语句; break; ... default: 执行语句; break; }
Sample code:
public static void main(String[] args) { char grade = 'B'; switch (grade) { case 'A': System.out.println("优秀"); break; case 'B': System.out.println("良好"); break; case 'C': System.out.println("及格"); break; case 'D': System.out.println("不及格"); break; default: System.out.println("错误的成绩"); } }
The above code demonstrates the basic usage of the switch statement, judging the grade level based on the value of grade, and then outputting the corresponding evaluation.
3. Ternary operator
The ternary operator is similar to the if statement, except that its usage is simpler and clearer. Its basic syntax is as follows:
(条件) ? 真值 : 假值;
Sample code:
public static void main(String[] args) { int age = 18; String result = (age >= 18) ? "你已成年" : "你未成年"; System.out.println(result); }
The above code demonstrates the basic usage of the ternary operator. It determines whether you are an adult based on the value of age. If you are an adult, it will output "You are an adult", otherwise it will output "You are underage".
3. Loop structure
The loop structure means that the program can repeatedly execute a certain code block until the stop condition is met. Therefore, the loop structure is the most important component in the program iteration process. part. In Java, loop structures are mainly divided into for loops, while loops and do-while loops.
1. for loop
The for loop is a counter-controlled loop structure, characterized by the known number of loops. Its basic syntax is as follows:
for (初始化; 条件判断; 步进) { 循环操作; }
Sample code:
public static void main(String[] args) { for (int i = 1; i <= 10; i++) { System.out.print(i + " "); } }
The above code demonstrates the basic usage of the for loop, which outputs all numbers from 1 to 10.
2. While loop
The while loop first determines whether the condition is true, and then performs the loop operation. Its basic syntax is as follows:
while (条件) { 循环操作; }
Sample code:
public static void main(String[] args) { int i = 1; while (i <= 10) { System.out.print(i + " "); i++; } }
The above code demonstrates the basic usage of while loop, which outputs all numbers from 1 to 10.
3. do-while loop
The do-while loop is similar to the while loop, except that it first performs a loop operation and then determines whether the condition is true, so the do-while loop is at least It will be executed once, and its basic syntax is as follows:
do { 循环操作; } while (条件);
Sample code:
public static void main(String[] args) { int i = 1; do { System.out.print(i + " "); i++; } while (i <= 10); }
The above code demonstrates the basic usage of do-while loop, and the loop outputs all numbers from 1 to 10.
4. Summary
Java's flow control statement is a very important part of program design. Its use methods are divided into three types: sequential structure, selection structure and loop structure.
Sequential structure: The program is executed sequentially in the order in which the code is written, without any judgment or loop operation.
Selection structure: The order of program execution is determined based on the true or false judgment conditions, which are divided into if statements, switch statements and ternary operators.
Loop structure: The program can repeatedly execute a certain code block until the stop condition is met. It is divided into for loop, while loop and do-while loop.
The above is the detailed content of How to use Java language flow control statements. For more information, please follow other related articles on the PHP Chinese website!