Java program logic control, Java loop __java

Source: Internet
Author: User

Program logic, mainly divided into three kinds
Sequential structure: executed from front to rear, each statement executed only once
Branch structure: Select a branch by judging the condition
Loop structure: Loop program Statement branching structure if else

1. if (Boolean expression) {
    conditional execution statement
    }
 2. if () {}else{}
 3. if () {}else if () {}
 4. if () {}else if () {}else if () {} else{}
Switch
Determine the condition, match the corresponding char,byte,short,int,string (JDK1.7), enum type (JDK1.5)   
Note that Boolean expressions cannot be used. cannot be floating number and long, only one slot,4 byte.
Switch (integer | character | enum | String) {
    case content: {
        execution when content satisfies; break
        ;
    }
    Case content: {
        Execute when content is satisfied; break
        ;
    }
    Case content: {
        Execute when content is satisfied; break
        ;
    }
    Case content: {
        Execute when content is satisfied; break
        ;
    }
    Case content: {
        Execute when content is satisfied; break
        ;
    }
    Default: {
        Execute when content is not satisfied;
Attention
You need to add a break, jump out of the branch structure, or the following branch after the match, will output
switch cannot judge Boolean expression, can only judge the content
Loop Structure
When the code needs to be repeated, the loop can be used to control the while loop for the doing while loop

while
while (Boolean expression) {
    circular statement;
    modify Loop end condition;
    }

} for

for (define variable; loop end condition; cyclic condition change) {
        circular statement
}
Todo
do{
    Circular statement;
    Modify the loop end condition;
}while (boolean expression); Perform one first
If you do not know the number of loops with a while loop if you know the number of loops with a while () behind the For loop do, note the semicolon for (int i=0;;;; i++) {}//dead loop, no loop end condition for (; i<3;i++) {}//error, error: symbol I for (int i=0;i<3;) {}//dead loop for (;;) {}//dead loop Loop ControlContinue exit this cycle, the following does not execute, continue the next cycle break exit the current entire loop, can only exit a Break out of multiple loops
//1, add loop tag outer:for () {inner:for () {if () {Break:out
                    ER;
    }}//2, add judgment token Boolean flag = FALSE;
                            for (;&&!flag;) {inner:for () {if () {flag = true;
                            break; }
                       }
                }
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.