Java Share Fourth day

Source: Internet
Author: User

While loop:

while (Boolean_expression) {

Statements

}

At execution time, if the result of the Boolean expression is true, then the action in the loop will be executed, which will continue as long as the result of the expression is true

Do...while Cycle

do{

Statements

}while (Boolean_expression);

The Do...while loop is similar to a while loop, unlike a do...while loop that is guaranteed to execute at least once

For loop

The For loop is a loop control structure that can effectively write loops that need to be executed in a specific number of times.

for (initialization; Boolean_expression; Update) {

Statements

}

The following is a loop that controls a process:

The initial process is executed first, and only once, this step can declare and initialize any loop control variables, without having to put a declaration here, as long as a semicolon appears.

Next, the Boolean expression evaluates, if true, the loop body is executed, and if False, the loop body does not execute and the process controls the jump to the next statement in the past for loop.

After the loop body is executed in the For loop, the control flow jumps back up to the UPDATE statement, which allows any loop control variable to be updated, and the statement can be left blank, as long as a semicolon appears after the Boolean expression.

The Boolean expression now evaluates the calculation again, if it is true, the loop executes and repeats the process (the loop body then updates the step and then the Boolean expression), after which the Boolean expression is False, then the loop terminates

For loop in Java enhanced version:

for (declaration:expression) {

Statements

}

Declaration: A new declaration block variable, which is compatible with the elements in the array being accessed. The variable will be available within the block and its value will be the same as the current array element.

Expression: This calculation results in a loop array required. An expression can be an array variable or a method call that returns an array.

Example:

 Public classTest { Public Static voidMain (string[] args) {int[] numbers = {10,20,30,40,50};  for(intx:numbers)            {System.out.println (x); System.out.println (","); } System.out.println (""); String[] Names={"A", "B", "C"};  for(String name:names) {System.out.println (name+","); }    }}

The Break keyword:

The keyword break is used to stop the entire loop;

The Continue keyword is used to stop this loop and go to the next iteration

Java Share Fourth day

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.