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

What are the structures of Java loops

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces the relevant knowledge of "what is the structure of Java loop". The editor shows you the operation process through an actual case, and the operation method is simple, fast and practical. I hope this article "what is the structure of Java loop" can help you solve the problem.

Java loop structure-for, while and do...while

Program statements with sequential structures can only be executed once. If you want the same operation to be performed multiple times, you need to use a loop structure.

There are three main loop structures in Java:

1. While cycle

2. Do... While cycle

3. For cycle

An enhanced for loop, which is mainly used in arrays, is introduced in Java5.

. . .

While cycle

While is the most basic loop, which has the following structure:

While (Boolean expression) {

/ / Loop content

}

As long as the Boolean expression is true, the loop executes forever.

. . .

Do... While cycle

For while statements, you cannot enter a loop if the condition is not met. But sometimes we need to execute at least once, even if the conditions are not met.

Do... The while loop is similar to the while loop, except that do... The while loop is executed at least once.

Do {

/ / Code statement

} while (Boolean expression)

Note: the Boolean expression is after the body of the loop, so the statement block is executed before the Boolean expression is detected. If the Boolean expression evaluates to true, the statement block executes until the Boolean expression evaluates to false.

. . .

For cycle

Although all loop structures can be represented in while or do...while, Java provides another statement-- for loops, which makes some loop structures simpler.

The number of times the for loop is executed is determined before execution. The syntax format is as follows:

For (initialization; Boolean expression; update) {

/ / Code statement

}

There are the following notes about the for loop:

-the initialization step is performed first. You can declare a type, but you can initialize one or more loop control variables, or it can be an empty statement.

Http://www.iis7.com/b/plc/

-then, detect the value of the Boolean expression. If true, the loop body is executed. If false, the loop terminates and the statement following the body of the loop begins to execute.

-after performing a loop, update the loop control variable.

-detect the Boolean expression again. Loop through the above process.

. . .

Java enhanced for cycle

Java5 introduces an enhanced for loop that is mainly used in arrays.

The syntax format of the Java enhanced for loop is as follows:

For (declaration statement: expression)

{

/ / Code sentence

}

Declaration statement: declare a new local variable whose type must match the type of the array element. Its scope is limited to the looping statement block, and its value is equal to the value of the array element at this time.

Expression: an expression is the name of the array to access, or a method that returns an array.

. . .

Break keyword

Break is mainly used in loop statements or switch statements to jump out of the whole statement block.

Break jumps out of the innermost loop and continues to execute the statements below the loop.

Syntax: the use of break is very simple, which is a statement in the loop structure: break

. . .

Continue keyword

Continue is suitable for any loop control structure. The function is to make the program jump to the next iteration of the loop immediately.

In the for loop, the continue statement causes the program to jump to the update statement immediately.

In while or do... In the while loop, the program immediately jumps to the judgment statement of the Boolean expression.

Syntax: continue is a simple statement in the body of the loop: continue

:::

Java branching structure-if … Else/switch

.

Java branching structure-if...else/switch

The sequential structure can only be executed sequentially, and can not be judged and selected, so the branch structure is needed.

Java has two branch structures:

1. If statement

2. Switch statement

. . .

If statement

An if statement contains a Boolean expression and one or more statements.

Syntax:

The syntax for the If statement is as follows:

If (Boolean expression)

{

/ / if the Boolean expression is the statement to be executed by true

}

If the value of the Boolean expression is true, the code block in the if statement is executed. Otherwise, the code following the If statement block is executed.

. . .

If...else statement

The if statement can be followed by the else statement, and when the Boolean expression value of the if statement is false, the else statement block is executed.

Syntax:

If... The usage of else is as follows:

If (Boolean expression) {

/ / if the value of the Boolean expression is true

} else {

/ / if the value of the Boolean expression is false

}

. . .

If...else if...else statement

The if statement can be followed by else if. Else statement, which can detect a variety of possible situations.

When using the if,else if,else statement, you need to be aware of the following:

The if statement has at most one else statement, and the else statement comes after all else if statements.

An If statement can have several else if statements, which must precede the else statement.

Once one of the else if statements is detected as true, the other else if and else statements will skip execution.

Syntax:

The format of the if...else syntax is as follows:

If (Boolean expression 1) {

/ / execute code if the value of Boolean expression 1 is true

} else if (Boolean expression 2) {

/ / execute code if the value of Boolean expression 2 is true

} else if (Boolean expression 3) {

/ / execute code if the value of Boolean expression 3 is true

} else {

/ / if none of the above Boolean expressions executes code for true

}

. . .

Nested if... Else statement

It is legal to use nested if-else statements. This means that you can use if or else if statements in another if or else if statement.

Syntax:

Nested if... The format of the else syntax is as follows:

If (Boolean expression 1) {

/ / execute code if the value of Boolean expression 1 is true

If (Boolean expression 2) {

Execute code if the value of Boolean expression 2 is true

}

}

You can nest else if...else like if statements.

. . .

Switch statement

The switch statement determines whether a variable is equal to a value in a series of values, each of which is called a branch.

Syntax:

The format of the switch syntax is as follows:

Switch (expression) {

Case value:

/ / statement

Break; / / optional

Case value:

/ / statement

Break; / / optional

/ / you can have any number of case statements

Default: / / optional

/ / statement

}

The switch statement has the following rules:

The variable type in the-switch statement can only be byte, short, int, or char.

A-switch statement can have multiple case statements. Each case is followed by a value to be compared and a colon.

The data type of the value in the-case statement must be the same as the data type of the variable and can only be constant or literal constant.

When the value of the variable equals the value of the case statement, the statement after the case statement starts execution and does not jump out of the switch statement until the breakstatement appears.

-when a break statement is encountered, the switch statement terminates. The program jumps to the statement execution after the switch statement. Case statements do not have to contain break statements. If no break statement appears, the program continues to execute the next case statement until the break statement appears.

The-switch statement can contain a default branch, which must be the last branch of the switch statement. Default executes when there is no case statement whose value is equal to the value of the variable. The default branch does not require a break statement.

This is the end of the introduction to "what is the structure of the Java loop?" Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

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

Development

Wechat

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

12
Report