Java basic Process Control and conditional statements, loop statements

Source: Internet
Author: User
Tags tag name terminates

Sequential structure

sequence structure of the program: If there is no process control in the code, the program executes in the written format from the top and the next line, and then executes the next statement after the execution of a statement, without judging and jumping to the end of the program.

If statement

If statements use a Boolean or Boolean value as the selection criteria, there are three structural forms: if translated into Chinese, that means if ..., just do ...

The first form of structure:
if (Boolean expression) {     conditional execution body}

The If followed by {} represents a whole-code block, which we call the conditional execution body, that is, the condition is true, executes this block of code.

Use of the IF statement note:

1): After the IF (Boolean expression), preceded by {}, there is no semicolon.

2): If the IF statement only controls a sentence, then you can not use {}. Enterprise specification: All require, regardless of if control a few words, all use curly braces.

3): When manipulating variables of type Boolean, the following code is not professional.

The second form of structure:
if (Boolean expression) {     conditional execution body A}else{     conditional execution body B}

If-else statements and ternary operators:

Semantically, the meanings are the same. Essentially, If-else is a statement structure, and the ternary operator is an operational symbol. The ternary operator must have a result that the expression must have a result and must have a return. And If-else, can not return what results, only control the structure of the statement.

Note: 1): You cannot use a semicolon in the if (Boolean expression). 2): You cannot use the Else statement directly.

The third form of structure:
if (Boolean expression a) {     conditional execution body}Elseif (boolean  expression b) {     conditional execution body B}Else  {     conditional execution body C}

Use Note:

1): Cannot use a semicolon in the if (Boolean expression).

2): cannot be in the else if (Boolean expression), using a semicolon.

3): You cannot use the else if statement directly, you must first use the IF statement.

4): The Else statement at this time, which represents the code that executes when all of the above conditions are false.

Switch statement

The format of the switch statement structure is as follows:

Switch(int-expression) { CaseA value: The expression result equals the condition 1 o'clock, execute this statement; Break; CaseB Value: The expression result equals the condition 2 o'clock, execute this statement; Break;. .. CaseC Value: Executes this statement when the result of an expression equals the condition N; Break;default: Execute this statement when none of the above values are satisfied; Break;//generally do not writeNote: The result of the expression after the case must be a constant. 

Switch usage details and usage Note:

The switch statement is suitable for matching judgments on multiple integer values, thus enabling branching control of the condition. Unlike the IF statement, the IF statement is followed by a Boolean expression, so the structure of the expression can be arbitrarily written, and switch is only the Boolean expression that is equivalent to the expression = = value.

, launched: In fact, if the statement is more powerful, switch is only the integer type of judgment, the use of more simple. The type of the numeric value here is an integer type other than long, which means that the type supported by switch is byte,short,char,int.

Essence: Switch can only support int types. (Byte,short,char, automatically promoted to int type)

The switch statement chooses only four types of basic data : Byte, short, char, int, no long.

The supported reference types are the wrapper classes for the above four basic data types (later), Byte, short, Character, Integer, and the string classes supported by the JAVA7 starting from JAVA5 and supported by the enumeration.

Case is only used to select an execution code for switch entry, if once entered a portal, the latter is actually lost the meaning of the judgment, that is, the code when entering the switch execution will be the case after the entry is ignored, will continue to execute, Until a break or return is encountered. (penetrating) so you can see that there is no {} symbol behind each case to represent a block of code.

Default indicates that all case values are incorrect, usually placed at the end of the switch, and do not need to use the break statement.

Options for if and switch:

If and switch belong to the selection statement, which means that the function is similar.

If: The criterion is a Boolean type.

Switch: The condition is the integer expression = = value of the int type.

It is found that switch can only be used to judge the value of an integer, and if it is a different type of judgment, only the IF statement. Select: switch is preferred if the integer expression is equal to the number of judgements. In other cases, use the IF statement.

Loop structure

Loop statement:

1): While statement

2):d o while statement

3): For statement

Three, you can complete the same function. It's just a grammatical difference.

While loop:

Format: while (Boolean expression) {loop body S}

While Loop features: first judge the expression, if true to execute the loop body, otherwise, skip the loop body.

Do and loop:
 Do {      loop body statement}while (Boolean expression);

Do While loop features: first execute a loop body, and then judge the expression, if true to execute the loop body, otherwise, skip the loop body. That is, the do-while is executed before judgment, and the loop executes at least once, even if the condition is false.

For loop:
 for (initialization statement; Boolean expression; post-loop action statement) {    loop body statement}

Initialize statement: Represents the initialization of a loop, executes only once at the beginning of the loop, defines a variable, and assigns a value.

Boolean expression: When the expression is false, the loop terminates and is true before the loop body is executed. Post-loop Action statement: The statement is called after each iteration of the loop, and the general statement is an increment or decrement operation.

Comparison of dead loops and loop statements

Loop statements are all the same, just different grammatical structures.

Tip: Three loops, is interchangeable, under normal circumstances, if the number of cycles specified, select for loop to be convenient point.

Generally, I, J and K are used as cyclic variables.

Performance-based analysis: For loop performance is higher. Because a variable is defined within a for loop and the For loop executes, it frees the variable's storage space.

Nested loops
If the number of cycles in the outer loop is m times, the number of cycles in the loop is n times, then the number of cycles in the inner loop needs m * n times. Nesting loops Note:      123. Avoid too many and too deep loop nesting structures as much as possible

Nested Loop performance optimization

Nested loop cases

Output Rectangle pattern ***************

output triangle pattern ***************

Output 99 multiplication table

Control Loop Structure Statements
 Break : Terminates the current loop. The statement after Stopbreak cannot be executed, so it cannot be written.

Continue statement: the continuation of the meaning. Skip. Skips the current loop and goes to the next loop operation. The statement after the continue cannot be executed, so it cannot be written.

return : means that the end loop is in the same way that the method ends, and the loop structure naturally ends. Note:The statements after break, continue, and return never have a chance to execute, so no more statements can be followed, or the compilation fails. -------------------------------------------------------breakand return can end the current loop:  break;

Control The outer loop: The tag is used at this point, and the tag is the alias that is given to the loop, but the alias is to satisfy the specification of the identifier. To control the outer loop, follow the alias of the loop after break or continue to OK    break   continue < tag name >;

Java basic Process Control and conditional statements, loop 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.