Home > Java > javaTutorial > body text

Detailed explanation of process control in java

高洛峰
Release: 2017-03-12 13:57:10
Original
1620 people have browsed it

Java programs control the execution flow of methods through control statements to achieve specific functions

There are three main process control structures in Java

Sequential structure

Selection structure

Loop structure.

The combination of the three structures can solve any complex problem.

Branch statements are also called conditional statements

Conditional statements allow part of the program to be selectively executed based on the value of certain expression.​​ ​

​ JavaProgramming language supports dual-way if and multi-way switch branch statements.


## If-

else Statement

In the Java programming language if () uses a Boolean expression instead of a numeric value .

The form is as follows

if(Boolean type expression 1){

          Statement 1;

}

The following code can Write according to the actual situation

            else if (expression 2) {

            Statement 2;

##             Statement 3;

#}

          else{

              Statement n;

}

##          

Switch statement

The value of the expression must be compatible with an integer or an enumeration type

Constant

The value includes byte, short, int and char and cannot be a

string

or

Object

also cannot be a long value

switch statement syntax format


switch(integer or character type

variable

){                 case const1:

           statement1;           break; break; ## Break here means jumping out of this branch.

If there is no break statement as the end sentence of a certain case code segment, the execution of the program will continue to the next case without checking the value of the case expression.

When the value of a variable or expression cannot match any case value, the optional default character (default) indicates the program code case that should be executed; it can be followed by a direct constant value.

Loop structure

The main loops used in java are

for loopwhilewhile

and do while. Not much but you still need to remember the following three forms of loop statements

for loop

for (initialization statement; loop condition; incremental expression){

Loop body )

}

# The running program will output a hundred times "Hello World"

# 接 Next we will write a whiledo ## stereotyped with the same function. #while loop

       

while(loop condition){

Execution statement

----

}


## Based on

Conditional judgment determines whether to execute the execution statement within the curly brackets.


The execution process first determines the loop condition when executing the while statement. If the loop condition is false, the subsequent code of the while statement will be executed directly. If the loop condition is true, the loop body code will be executed. Then judge the loop condition until the loop condition is not established.


##                      

do-while

The syntax format of the statement is #       } while loop condition

Syntax Description In the do-while statement, the loop body part is the code part that is repeatedly executed. The loop condition refers to the condition for the loop to be established. The loop condition is a boolean type. When the value is true, the loop is executed, otherwise the loop ends and the entire statement ends with a semicolon.

Execution process When the do-while statement is executed, the loop body is first executed and then the loop condition is judged. If the loop condition is not established, the loop ends. If the loop condition is established, the loop body continues to be executed. After the loop body is executed, the loop condition is judged in sequence. analogy.


The above is the detailed content of Detailed explanation of process control in java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template