Java Learning notes-sixth flow control statements

Source: Internet
Author: User
Tags case statement

The sixth chapter is familiar with the Java Process Control statement

Java Program flow control is divided into sequential structure , selection structure , loop structure and jump statements .

    1. Sequential structure: execute from top to bottom according to program code, until the end of the program, without any judgment and jump.

    2. Select structure (branch structure): determine the given condition, according to the process of judging the control program. Include the IF statement and the switch statement.

      2.1 If statement: determines the process of a program by judging the value of a given expression. There are three types of common if statements:

      (1) if (expression) {

      Statement

}

(2) if (expression) {

Statement

}else{

Statement

}

(3) if (expression) {

Statement

}else if (expression) {

Statement

}

...//can have 0 or more else if statements

else{//The last else statement can be omitted as appropriate

Statement

}

The result of expressions expression can only be boolean, that is, the result of this expression can only be true or false

Executes the code block for if if the expression is true, otherwise, if there is else, the code block of else is executed.

(2) and (3) are interlinked, (3) If there is no else if code block becomes (2).

The Else statement must be paired with if or else if, else is always paired with the IF or else if if is closest to it, and you can change the pairing relationship by curly braces.

Note: Code that is enclosed in a Process control statement is called a block of code, and a block of code is usually executed as a whole (unless you run into a keyword such as break, continue, return, or an exception), this block of code is also known as the conditional execution body.

2.2 Switch statement (multi-branch statement): Implement multi-branch control of the program by data matching, the syntax format is as follows:

switch (expression) {

Case value 1:

Statement1;

Break

Case Value 2:

Statement2;

Break

...//can have multiple case code blocks

Case Value N:

STATEMENTN;

Break

Default

Break

}

The return value type of expression can only be char, BYTE, short, or int.

The switch statement obtains the return value of expression first, and then matches the value of each case statement in turn according to the return value, and executes the corresponding code block if the match succeeds.

The value values corresponding to the case statements must be constants, and the values of each value should be different.

The break statement is used to jump out of the switch statement after the corresponding Case branch statement is executed, otherwise the subsequent statement is executed sequentially. In some cases, you can omit the break statement in the corresponding code block if you want to perform a set of identical actions for several different case values.

Default is optional, and the default code block is executed when the value of the expression does not match any of the value values. If there is no default statement, the program does nothing and jumps directly out of the switch statement.


3. Loop structure: The loop structure can be used to execute a piece of code repeatedly until the loop condition is not met. The loop structure mainly has for, while, do-while three kinds of circular statements.

3.1 For statement : Applies to cases where the number of loops is known explicitly. The syntax format is as follows:

For (initialization;condition;iteration) {

Statement

}

Loop initialization (initialization): Executes only once before the start of the loop, usually where the iteration variable is defined, which is applied as a counter that controls the entire loop.

Conditional expression (condition): A Boolean expression that executes the statement statement in the loop body if the value is true and ends the loop if it is false.

Iterator (iteration): usually an increment or decrement operation expression of an iterative variable, executed when the loop body (statement) is executed.

3.2 While statement: a loop that is typically used to repeat an indeterminate number of times. The syntax format is as follows:

While (expression) {

Statement

}

Expression is a Boolean expression, and the while statement first obtains the return value of expression, and when the return value is true, the statement in the loop body executes statement, otherwise, the loop ends.

3.3 do-while Statement:The difference between the do-while and while is that the first cycle, while is to first determine the cycle condition, and then cycle, if the condition is false, then the loop body will not be executed. The Do-while statement is to execute the loop body before the judgment, that is, the Do-while cycle will execute at least once the loop body .


4. Jump statement : Through the jump statement can achieve the program flow jump. The jump statements in Java include break statements, continue statements, and return statements.

4.1 Break statement:The break statement can be used inside the loop statement to end the loop. For example:

Public static void Main (string[] args) {

int i=0;

while (I<10) {

i++;

if (i==5) {

Break

}

System.out.println (i);

}

System.out.println ("End of cycle");

}

Run result: After output 1,2,3,4,5, the "Loop End" will be output.


4.2 Continue statement: can only be used inside a loop statement to skip this loop and continue with the next loop. The use of the continue statement in the while and Do-while loop structure means that the jump to the loop condition continues execution, whereas the use of the continue statement in the For loop structure means that the jump to the iteration statement continues execution. For example:

Public static void Main (string[] args) {

int i==0;

while (i<4) {

i++

if (i==2) {

Continue

}

System.out.println (i);

}

System.out.println ("End of cycle");

}

After running the output will be: 1,3,4, Loop end


4.3 return statement:The return statement is used in the method to terminate the execution of the current method, return to the statement that called the method, and proceed with the execution of the program. The syntax format is as follows:

return [expression];

The return statement can be followed by a returned value or without.

Expression expressions can be constants, variables, objects, and so on. The data type of the expression after the return statement must be the same as the data type of the method declaration.

When the program executes the return statement, the value of the expression is evaluated first, and the value of the expression is returned to the statement that called the method.

The block of code that is behind the return statement is not executed, so the return statement is usually at the end of the code block.


5. Note Usage for Java programs:Java has three ways to annotate, one-line comments, multiple lines of comments, and document annotations, respectively.

Single-line comment: begins with a double slash "//" and terminates at the end of the bank. Used for a short description of a line of code.

Multiline Comment: starting with "/*" and ending with "*/", all characters are the contents of a multiline comment. Typically used for descriptions of files, methods, data structures, and so on, or algorithms. Generally located in front of the method, as a guide, you can also be placed in other locations as needed.

Document comments: start with "/**" and End With "*/", where all characters are the contents of the document comments. Document annotations are primarily intended to support the JDK tool Javadoc, which, through Javadoc, generates HTML-formatted code reports, so that document comments should be written before classes, constructor methods, member methods, and definitions of constants or variables.

Java Learning notes-sixth flow control statements

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.