Java for Loop

Source: Internet
Author: User

For loop

The number of times the For loop executes is determined before execution. The syntax format is as follows:

 for (initialize; Boolean expression; update) {// code statement }

There are several explanations for the For loop:

    • The initialization step is performed first. You can declare a type, but you can initialize one or more loop control variables, or you can be an empty statement.
    • Then, the value of the Boolean expression is detected. If true, the loop body is executed. If False, the loop terminates, starting execution of the statement following the loop body.
    • After the loop is executed, the loop control variable is updated.
    • Re-detects the Boolean expression. The loop executes the above procedure.
Java Enhanced for loop

JAVA5 introduces an enhanced for loop that is primarily used for arrays.

The Java enhanced for loop syntax format is as follows:

 for (Declaration statement: expression) {// code sentence }

Statement statement: declares a new local variable that must have a type that matches the type of the array element. Its scope is scoped to the Loop statement block, whose value is equal to the value of the array element at this time .

expression: The expression is the name of the array to access, or is the method that returns the value to the group.

 Public classTest { Public Static voidMain (String args[]) {int[] numbers = {10, 20, 30, 40, 50}; for(int x:numbers ) {System.out.print (x); System.out.print (",");} System.out.print ("\ n"); String [] Names={"James", "Larry", "Tom", "Lacy"}; for( String name:names ) {System.out.print (name); System.out.print (",");}}}
Continue keywords

The Continue is suitable for any loop control structure. The function is to let the program jump immediately to the next iteration of the loop .

In the For loop, the continue statement causes the program to jump immediately to the UPDATE statement.

In the while or Do...while loop, the program immediately jumps to the judgment statement of the Boolean expression.

Java for Loop

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.