Java Basic Three

Source: Internet
Author: User

Topic: Judging Statement Looping statements

I. Judgment statement 1, single branch
1 if (condition) {23// Insert code here, indicating if the condition in the execution of statement 4 }

Attention:
1. The condition must be Boolean
2.if with only one line of code, you can omit {}

1   int b=3,c=4,d=5; 2         if (b<c) 3             if (b>D) 4                 System.out.println (d); 5             Else 6                 System.out.println (c);

Output is 4

2. Dual Branch
1 if (condition) {2   // condition set up output 3 }else{4// condition not established output 5 }

Attention:
1.else can not bring conditions;
2.if,else with only one line of code, you can omit {}

3. Multi-Branch if
1 if (condition) {23 } Else if (condition) {4   can have more than 5 }else{6   Can omit 7 }

Attention:
1.if,else if can be conditional
2.if,else If,else with only one statement, you can omit {}

4, multi-branch switch
1 Switch(expression) {2  CaseConstant value://This label can have multiple3 code block;4       Break;5 6 default://can omit7 code block:8      Break;//can omit9 Ten}

Attention:
1. The value type of the expression matches the constant value type in the label;
2. The expression type must be Byte,short,int,char,string,enum
3. Labels cannot be the same;
4. Support Tag stacking.

The difference between multiple if and switch:
    • Switch can only be judged by the equivalent condition, and multiple if any judgment can be used (more for interval judgment)
    • Equivalent judgment switch efficiency is high.
Second, the circular statement

Fixed loop for

Non-stationary while Do-while

Number of fixed cycles

1 // an initial value; a terminating value; an iteration. 2  for (int i=0;i<5;i++) {3    // Loop Body 4 }

First condition judgment, after loop operation.

Non-fixed number of cycles:

1  while (condition) {2   loop body;// cyclic operation 3 }

First condition judgment, after loop operation.

1  Do {2   loop body; 3 4 }while (condition);

Execute first, then judge.

Java Basic Three

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.