switch case statement syntax

General form:
Switch (expression) {
Case 1 constant expression: statement 1;
Case 2 constant expressions: sentence 2;
...
Case constant expressions n: n-sentence;
default: Statement 1 + n-;
}

First calculate the mean value of the expression, then one by one, and relatively constant expressions after the case, such as if the comparison continues down, if has been unequal, after the default statement is executed; if a constant equal to a certain expression, from this expression after the statement begins execution, and execute the statement after all subsequent case.
; If the statement is true only if it is determined the implementation of this decision statement, executing the if statement to come out, if not perform other statements: Unlike the if statement
after the switch statement but does not execute the statement after the judgment is true out of the loop, but continues after all the case statements. Increase after each case statement break statement, so that each time after the execution can jump out of the switch statement, so as to avoid undue output results.

#include<stdio.h>
int main(){
	int week=3;
    while (week>0){
        switch (week){
            case 1: printf("星期一\n");
            case 2: printf("星期二\n");     //break;
            default: printf("其他\n");
        }
        printf("week = %d\n", week);
        week--; 
        printf("week = %d\n", week);

    } 
}

operation result:
Here Insert Picture Description

Published 92 original articles · won praise 7 · views 3719

Guess you like

Origin blog.csdn.net/dajiangyou123456/article/details/104533980