Process Control--branching structure

Source: Internet
Author: User


Java provides two common branch control structures, the IF statement and the switch statement.

Where The IF statement uses a Boolean expression or a Boolean value as a branching condition for branch control ,

The switch statement is used to match multiple shaped values to achieve branch control .


If condition statement

The IF statement uses a Boolean expression or a Boolean value as a branching condition for branch control. There are 3 types of specific:

1), if () {}

2), if () {}else{}

3), if () {}else if () {}else{}

If the code block after If,else If,else has only one line of statements, you can omit the curly braces, because the single-line statement itself is a whole, without having to use curly braces to define them as a whole.

Note: The conditional execution body after the If,else,else if is either a block of code enclosed in curly braces, the block of code as a whole as a conditional executor, or a line of statements with a semicolon terminator, or even an empty statement.

An empty statement is a semicolon, so it's just the statement as the conditional executor. If you omit the curly brace of the conditional execution body after the if condition, the IF condition is only controlled to the first semicolon immediately following the conditional statement.

Personal recommendations using the IF statement:

1), as far as my personal coding style is concerned, I do not recommend omitting curly braces, even if the execution body has only one statement .

2), when using the If...else statement, be sure to deal with a smaller scope first, be sure to first deal with the branches that often occur .

For any if Else statement, there is no condition on the surface that looks like, else, which is the wrong idea, else means "otherwise", and else itself is a condition. the implied condition of else is to reverse the preceding condition .

public static void Main (string[] args) {//The following code console is always output--This branch masks all of the branches below. int age = 50;if > {System.out.prin TLN ("This branch shields all the branches below");} else if (age >) {System.out.println ("This branch will never be executed, forever Forever");}}



Switch Branch statement

The switch statement consists of a control expression and multiple case labels, and, unlike the IF statement, the data type of the control expression after the switch statement can only be byte,short,char,int four integer types, enumeration types, and

Java.lang.String type, cannot be a Boolean type.

The control expression behind the switch statement supports string types, starting with Java7 , and the recommended use of JAVA8,JAVA8 is another milestone version of Java after 5.

A switch statement often needs to be followed by a block of code following the case label, which is identified as the code block. The syntax format for the switch statement is as follows:

switch (expression) {case condition1:{statement (S1) break;} Case condition2:{statement (S2) break;} ... case conditionn:{statement (SN) break;} Default:{statement (s)}}

The last default in the switch statement means that the value of the expression is not the same as the value of the expression in all previous case. As an example:

public static void Main (string[] args) {String season = "Summer"; switch (season) {case "Spring": {System.out.println ("Here is Spring Yo"); break;} Case "Summer": {System.out.println ("Here is Summer Yo"); Default:{system.out.println ("Only fall or Winter");}}

There are two notable places to use when using the switch statement:

1), thedata type of the expression after the switch statement can only be of type BYTE,SHORT,INT,CHAR4, string and enum type

2), If you omit the break of the code block after the case, a trap is introduced. Because of this limitation, I usually rarely use the switch statement, it is recommended that you do not use more, with if+else enough

public static void Main (string[] args) {//The following code demonstrates that the code execution block has fewer break encoding traps. All branches will be executed after the first case, and it is not recommended to use the switch statement string season = "Linkinpark"; switch (season) {case "Nightwish": { SYSTEM.OUT.PRINTLN ("the statement here will not be executed");} Case "Linkinpark": {System.out.println ("the statement here will be executed");} DEFAULT:{SYSTEM.OUT.PRINTLN ("the statement here will also be executed");}}}



Process Control--branching structure

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.