Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Good programmer big data tutorials share big data's process control

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

Good programmer big data tutorials share big data's process control, the order in which the code is executed when the program is running, which is called the execution structure of the program. In Java, the execution structure of a program is divided into three types:

Sequential structure: the code is executed line by line from top to bottom, which is the basic structure of program execution.

Branch structure: code execution to a node encounters multiple branches, select one branch to execute, the other branches do not execute

Loop structure: a code segment needs to be executed repeatedly

The program adopts the sequential structure by default, and we can also modify the execution structure of the program through some statements, which are called flow control statements, which can be divided into branch flow control and cyclic flow control according to the modified execution structure.

1.3.1 Branch process Control-if

Basic grammar

If (condition) {

/ / Code snippet 1

}

Else {

/ / Code snippet 2

}

Logic: condition is a variable of type boolean, or an expression of a boolean result. If the value of condition is true, snippet 1 executes, otherwise snippet 2 executes

Advanced level

If (condition1) {

/ / Code snippet 1

}

Else if (condition2) {

/ / Code snippet 2

}

Else {

/ / Code snippet 3

}

Logic: first judge condition1, if condition1 is established, execute code segment 1; if condition1 is not established, then judge condition2, if condition2 is established, execute code segment 2, otherwise execute code segment 3

Special instructions

In the if-else statement, if there is only one statement in a code snippet, the curly braces can be omitted; but in terms of syntax specification, we generally do not omit.

1.3.2 Branch flow Control-switch

Basic grammar

Switch (variable) {

Case value1:

Case value2:

...

Default:

}

Logic: variable is a variable, and the switch statement captures the value of this variable; if the value of the variable is the same as the value after a case, the statement after the case is executed; if the value of the variable is not the same as any value, the value after the default is executed

Matters needing attention

There is penetration in the switch-case statement, that is, if the value of variable matches a value, all the subsequent code is executed from the case to the end of the switch statement, and the following case matching is skipped; to avoid penetration, you can use the keyword break to jump out of the switch statement

1.3.3 cycle flow control-while

Basic grammar

While (condition) {

/ / cyclic body

}

Logic: each loop line judges condition. If it is true, the loop body executes, and then judges condition; until condition is false and the loop ends.

Do-while

Do {

/ / cyclic body

} while (condition)

Logic: first execute the loop body, and then determine whether the loop condition is valid

Keywords break, continue and circular tags

Break

If used in a loop, it means to jump out of the loop; if used in a multi-tier loop, it will only jump out of the current one.

Continue

When used in a loop, it means to continue the loop. Starting with continue, all subsequent code is not executed and goes directly to the next loop.

Circular label

Generally used in multi-layer loops, you can add a label to each loop; you can cooperate with the break statement to end the specified loop directly. Or cooperate with the continue statement to directly terminate the execution of the specified loop and immediately enter the next loop

1.3.4 cyclic process control-for

Basic grammar

For (cycle start; cycle condition; cycle step) {

Cyclic body

}

Logic:

First execute the starting point of the loop, usually using and defining a variable that controls the loop, or giving the variable an initial value

Then judge whether the cycle condition is true, if the cycle condition is established, execute the loop body, then execute the loop step, and then re-judge the cycle condition.

If the loop condition is not established, the loop ends.

Special instructions

On grammatical ellipsis

The parenthesis of the for loop consists of three parts, all of which can be omitted:

Loop start: the definition of a variable can be placed outside the loop

Loop condition: if omitted, the default is true, which can be controlled to jump out of the loop in the loop body.

Cycle step size: the step size can be controlled in the cycle body

About curly braces

If there is only one sentence in the body of the loop, the curly braces in the body of the loop can be omitted, but for the sake of specification, we generally do not omit either.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report