conditional Statements
if (boolean-expression) statement
if (boolean-expression) statementelse statement
- Free combination with else if multiple conditions
if (boolean-expression) statementelse if (boolean-expression) statementelse if (boolean-expression) Statement ...
if (boolean-expression) { if (boolean-expression) statement else statement} else statement
Looping Statements
while (boolean-expression) statement
Do statementwhile (boolean-expression);
While and do-while the only difference: statements in Do-while execute at least 1 times
for (initialization; Boolean-expression;step) statement//initialization: Initialization expression//boolean-expression: Boolean expression//both can be empty. Eg:for (;;) Equivalent while (true)
Comma operator for (INIT1,INIT1; BOOLEAN-EXPRESSION;STEP1,STEP2) eg:for (int i=1,j=i+10;i<5;i++,j=i*2)
You do not have to create an int variable to count the sequence of access items, and foreach automatically generates each item
foreach can also be used with any Iterable object
eg:private static void Testforeach () {for (char c: "Hello world!"). ToCharArray ()) { System.out.print (c + "");} }
return, Jump
Use: 1. Develop a method to return what value 2. It causes a method to exit and return that value
If there is no return statement in the method that returns void, there will be an implicit return at the end of the method therefore, there is not always a return statement in the method, but if a method declares that it will return something other than void, You must ensure that each statement returns a value
In the body part of any iteration statement, you can use break and continue to control the flow of the loop
Break: Used to forcibly exit a loop without executing the remaining statements in the loop
Continue: Stops executing the current iteration and then returns to the beginning of the loop, starting the next iteration (skipping this loop)
label1:out-iteration { inner-iteration { //.. break;//interrupts internal iterations, back to external iterations //... continue;//execution point back to internal iteration //... Continue label1;//interrupts internal and external iterations, jumps to Label1 to continue from an external iteration /... Break label1;//interrupts all iterations and goes back to Label1, but does not re-enter the Iteration }}
1. The general continue will be returned to the beginning of the most inner loop (top) and continue to execute 2. The labeled continue will reach the position of the label and re-enter the loop immediately following the label; 3. A normal break breaks and jumps out of the current loop; 4. A tagged break breaks and jumps out of the loop that the label refers to.
Switch Statement
Switch (integral-selector) {case integral-value1:statement;break; Case Integral-value1:statement;break; Case Integral-value1:statement;break; Case Integral-value1:statement;break; //... Default:statement;}
Integral-selector: An integer selection factor, which is an expression capable of producing an integer value Integral-selector matches the Integral-value, the execution of the corresponding statement break is optional, and if omitted, the following case statement continues Until a break is encountered; (This is true if multiple conditions are required to execute the same statement)
Integral-selector must be an integer value such as int or char
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Control execution process for Java Fundamentals