Home >Java >javaTutorial >What are the statements of Java control structures?

What are the statements of Java control structures?

王林
王林forward
2023-04-30 10:10:06756browse

if structure

if (conditional expression){//execution code}else if (conditional expression){//execution code}else{//execution code}

The result of the conditional expression is true|false, the specific form:

1, direct Boolean variable

2, the result of the comparison operator or the result of the logical operator Result

3. In Java, non-zero numeric types or non-empty strings cannot be used to return Boolean values

switch structure

switch (Variable name) {case literal constant 1://execution code break;case letter constant 2://execution code break;default://execution code}

1. Variable types include integer and char and String type

2. The literal constant in case should be consistent with the variable type: integer type 3. char type 'a', String type "Hello"

3. break, used to jump out switch structure; default, defines the default execution code. When there is no break, the code will be executed at the end. In fact, neither break nor default are necessary.

4. When executing the switch statement, first compare the value of the variable with the case value one by one. If they match, the corresponding code will be executed from there.

If there is no break statement after the match, the match will no longer be performed and subsequent code will be executed directly. The switch will not be exited until a break statement is encountered or all code is executed.

When there is no break in switch, the default statement will be executed at the end.

while structure

while (loop condition){//execution code}

do...while structure

do{//execution code}while(conditional expression);

for loop structure

for(loop variable; loop condition; change loop variable ){//Execution code}

1. The positions of the three expressions of loop variable, loop condition and changing loop variable cannot be changed. Two; must be retained, but the loop variable can be written outside for, Write loop conditions and expressions that change loop variables internally

2. Loop variables, loop conditions, and expressions that change loop variables can have more than one expression, and expressions are separated by,.

Enhanced for loop

int[] a={1,2,3,4,5,};for(int b:a){System.out .println(b);}

The above is the detailed content of What are the statements of Java control structures?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete