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

What are the statements of Java control structures?

王林
Release: 2023-04-30 10:10:06
forward
741 people have browsed it

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!

Related labels:
source:yisu.com
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