Road Java based learning, - notes day4

Road Java based learning, - notes day4-- if the switch

if


A first type
if (judgment statement) {
    statement body;
}

The second type
if (judgment statement) {
    statement 1;
} the else {
    statement 1;
}
third type
if (judgment statement 1) {
    statement 1;
} the else if (2 judges statement) {
    statements 2; '
} the else IF (decision statement 3) {
    statement body 3;

....
the else {
    statement body. 1 + n-;
}

Note: the results of the determination condition parentheses are boolean type;

Final else can be omitted, but is not recommended, may be provided outside the case of other types;

If the if statement is a statement of the control body braces {} may be omitted, but it is not recommended to be omitted;

if (判断语句)
语句体;

You need to pay attention 

int a = 10;//编译器认为定义变量是两条语句不是一条


switch

Implementation process:

The match value and the value 1 in the case using a small matching the unmatched parentheses, if the matching is successful, the corresponding execution statement, and was then introduced by a whole Brake switch;

2 If it fails to match with all the case, perform default.

switch (to be matched value) {
    Case 1 match value:
        Statement body 1;
        BREAK;
    Case 2 matches the value:
        Statement body 2;
        BREAK;
    Case Match 3:
        ; 3 statement body
        BREAK;
    ...
    default:
         statement body n + . 1;
         BREAK;
}

Note:} encounters a break or out; case only the matching values is constant.
The data value to be matched to the type of short byte int char (can enhance type int);
JDK5 increased after enumeration;
the JDK7 increased String String;
 BREAK may be omitted, penetration phenomena occur;

switch (week == 1){
        case 1:
        case 2:
        case 3:
        case 4:
        case 5:
            System.out.println("工作日");
            break;
        case 6:
        case 7:
            System.out.println("周末");
            break;
}

default put the first row with the same effect as the last line, is the last run default, case is not executing the match, before entering default.

Repeating the latter case the value defined are not allowed

if the switch comparison:

If the judgment of recommended if statement;

If you want to match the fixed option, it is recommended to use a switch statement.

Published 12 original articles · won praise 2 · Views 1945

Guess you like

Origin blog.csdn.net/ytzang/article/details/104328681