Java Fundamentals of Control execution flow

Source: Internet
Author: User

conditional Statements
    • Contains only if
if (boolean-expression)    statement
    • Contains If-else
if (boolean-expression)    statementelse    statement
    • Free combination with else if multiple conditions
if (boolean-expression)    statementelse if (boolean-expression)    statementelse if (boolean-expression)    Statement ...
    • nested IF Else
if (boolean-expression) {    if (boolean-expression)        statement    else        statement} else    statement
Looping Statements
    • While loop
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 loop
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)
    • foreach statement
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
    • Return
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
    • Break, continue
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)
    • Label
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    }}
    • Summary
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

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.