If... Else if switch Branch Structure

Source: Internet
Author: User

Switch Branch Structure

I,Program running processCategory

1. Ordered Structure: Execute in sequence according to program order.

2. Branch Structure: Determine the execution sequence of the program based on the judgment.

3. loop structure.

Three elements of the branch structure: (1) there must be a clear judgment statement.

(2) The statement to be executed when the condition is true.

(3) statements to be executed when the condition is false.

Ii. Detailed discussion of the Branch Structure

1. The if... else... structure has three conditions.

(1) syntax structure:

If (Boolean expression ){

Statement 1;

Statement 2;

......

}

(2) syntax structure:

If (Boolean expression ){

Statement 1;

Statement 2;

......

}

Else {

Statement 1;

Statement 2;

......

}

(3) Statement Structure

If (Boolean expression ){

Statement 1;

Statement 2;

......

}

Else if (Boolean expression ){

Statement 1;

Statement 2;

......

}

Switch

Switch (expression ){//The result of the expression is first compared with constant 1. If it is the same, it is output. If it is other expressions, It is not judged. Break is paused,Constant 1 is different. It is compared with 2nd ,''''

Case constant 1: Statement 1; break;

Case constant 2: Statement 2; break;

......

Case constant N: Statement N; break;

[Default: Default statement]; optional if not written

}

Note: The expression in the switch can only be of four types

1. byte 2, short 3, int 4, char

Case constant 1: Statement 1; break;

Case constant 2: Statement 2; break;

......

Case constant N: Statement N; break;

Default: the default statement;

}

Note: The expression in the switch can only be of four types

1. byte 2, short 3, int 4, char

Public class testswitch {<br/> Public static void main (string ARGs []) {<br/> char c = 'a'; <br/> switch (c) {<br/> case 'B': <br/> system. out. println ('B'); <br/> break; <br/> case 'C': <br/> system. out. println ('C'); <br/> break; <br/> case 'A': <br/> system. out. println ('A'); <br/> break; <br/> default: <br/> system. out. println ('D'); </P> <p >}< br/>}Output:

In another case, look at the key code

Char c = 'a'; <br/> switch (c) {<br/> case 'B': <br/> system. out. println ('B'); <br/> break; <br/> case 'A': <br/> system. out. println ('A'); <br/> case 'C': <br/> system. out. println ('C'); <br/> break; <br/> default: <br/> system. out. println ('D ');
Output is a C

Case 'B': <br/> system. out. println ('B'); <br/> break; <br/> case 'C': <br/> system. out. println ('C'); <br/> break; <br/> case 'A': <br/> system. out. println ('A'); <br/> default: <br/> system. out. println ('D ');Output:
A
D

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.