Java BASICS (4) JavaSE and javase

Source: Internet
Author: User

Java BASICS (4) JavaSE and javase

I have a general understanding of operators in the previous section. This section reviews program process control! Program flow control includes the sequence structure, judgment structure (if), selection structure (switch), and loop structure.

1. determine the structure

① The first format of the if statement:
If (conditional expression)
{
Execute the statement;
}

② The second format of the if statement:

If (conditional expression)
{
Execute the statement;
}
Else // otherwise
{
Execute the statement;
}

Note: When an if else operation has a specific result, it can be simplified to a ternary operator.

③ If statement format 3:
If (conditional expression)
{
Execute the statement;
}
Else if (conditional expression)
{
Execute the statement;
}
......
Else
{
Execute the statement;
}

2. Select Structure

Switch (expression)
{
Case value 1:
Execute the statement;
Break;
Case value 2:
Execute the statement;
Break;
......
Default:
Execute the statement;
Break;
}

Note: switch features limited data options, onlyByte,Short,Int,CharThese four data types are available. When selecting a switch, select the first valid option. If not, select default. The switch execution structure ends when braces or break are encountered. Otherwise, it is executed until the condition is true!

Special cases:

Int x = 2;
Switch (x) // byte, short, int, char.
{
Default:
System. out. println ("d ");
// Break;
Case 4:
System. out. println ("");
// Break;
Case 1:
System. out. println ("B ");
Break;
Case 3:
System. out. println ("c ");
Break;

}

Output d a B. Principle: there is no break when the default value is executed, and the default value is placed at the top of the list. Therefore, it will continue to be executed downward without judging other answers, execute the statements that can be executed in a sequential structure until the break or braces are reached, so the output order should be d a B;

 

If and switch Applications:

If:
1. determine the specific value.
2. Determine the interval.
3. Determine if the operation result is a boolean expression.

Switch:
1. determine the specific value.
2. The number of values is usually fixed.
We recommend that you use the switch statement for several fixed values, because the switch statement loads all the specific answers into the memory.
The efficiency is relatively high.

3. Loop Structure

①. While mode:

While (conditional expression)
{
Execute the statement;
}

②. Do while mode:

Do
{
Execute the statement;
} While (conditional expression );

Note: The do while statement must be executed at least once, regardless of whether the conditions are met.

③. For method:

For (initialization expression, cyclic condition expression, and cyclic Operation expression)
{
Statement execution; (loop body)
}

Note: The order in which the for expressions run is read-only once. When the cyclic condition is true, the system executes the cyclic body and then executes the operation expression after the loop, then, judge the cyclic condition and repeat the process until the condition is not met.

While and for can be exchanged. The difference is that the variables defined by for loop are released in the memory at the end of the for Loop, while the variables used in the while loop can still be used after the loop ends.

The simplest infinite loop format is: while (true), for (;). The reason why an infinite loop exists is that it does not know how many times it exists. Instead, it judges the Loop Based on certain conditions.

4. Other process control statements

Break statement: Application Scope: select the structure and loop structure.

Continue statement: applies to the loop structure.

Note: a, the two statements are out of the application scope and there is no significance.

B. These two statements are independent of each other and cannot be executed.

C. The continue statement ends this loop and continues the next loop.

D. The appearance of the label allows the two statements to apply to the specified range.

Example:

Examples of labels:

Xiaoqiang: for (int x = 0; x <3; x ++)
{
Wangcai: for (int y = 0; y <4; y ++)
{
System. out. println ("x =" + x );
Break xiaoqiang;
}

}

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.