2.3switch case 语句注意事项。

#include<stdio.h>

int main()
{
    void action1(int, int),action2(int, int);
    
    char ch;
    int a=15, b=23;
    
    ch = getchar();
    switch(ch)
    {
        case 'a':                     //没有语句,则与下一case共用语句。且属于同一层,一起break。 
        case 'A':action1(a,b);break;  //void的类型没有返回值,这里为调用函数,也是一个表达式。 
        case 'b':                     //case后只能是常量或者常量表达式,故'a'是ASCII而不能是7<x&&x<8(这样逻辑表达结果为常量1)。 
        case 'B':action2(a,b);break;
        
        default: putchar('\a');    break;//可以没有default,这样流程直接走完switch,进入下一段。 
    }
    return 0;
 } 
 
 void action1(int x, int y)
 {
     printf("x + y = %d\n", x + y);
 }
 
 void action2(int x, int y)
 {
     printf("x * y = %d\n", x * y);
 }

 

posted @ 2016-08-17 11:06  VRednow  阅读(352)  评论(0编辑  收藏  举报