Process control statement _

Source: Internet
Author: User

For Loop format:

For (initialization expression; loop condition expression; Operation expression after loop ){
Execution statement
}

Public ClassFordemo {Public Static VoidMain (string [] ARGs ){//Int x = 3; space is opened in the memory only when the program reads it, and the memory space is released as long as the for loop ends.//The first must be a valid expression, and the second must be a conditional expression.        For(IntX = 3; x <8; X ++) {System. Out. println ("X =" +X );//X is valid only in the brackets.}//System. Out. println ("x =" X );//Compilation error. The variable X cannot be found.                        IntY = 3;While(Y <8) {System. Out. println ("Y =" +Y);} system. Out. println ("Y =" + y );//The result prints the value of Y.                        /*1. the variable has its own scope. For example, if the incremental definition used to control the loop is in the for statement, the variable is only valid for statement execution in the for statement, this variable is released in the memory. 2. for and while can be exchanged, which can be used, but if you need to define loop increments, use for to save memory*/}}

 

 

Common problems with for loop usage

For(System. Out. println ("A"); system. Out. println ("B"); system. Out. println ("C") {System. Out. println ("D");}//No result. The conditional expression is neither true nor false, so no result is returned.
Public ClassFortest1 {Public Static VoidMain (string [] ARGs ){IntX = 1;For(System. Out. println ("A"); x <3; system. Out. println ("C") {System. Out. println ("D"); X++;//It can also be placed behind the for brackets and separated by commas.}//The result is: A D C//For (multiple expressions can be separated by commas)}}

 

 

The simplest form of infinite Loops

For (;) {} // true if the for statement condition expression is left blank

While (true ){}

Process control statement _

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.