当前位置:网站首页>C language selection type supplement

C language selection type supplement

2022-06-21 14:41:00 Yangasang 815

Last time we talked about c In language if,else if ,else,switch Use of statements ,

Although more comprehensive , But there are still deficiencies , So I will do a little this time Add .

Let's start with grammar ,

Grammatical structure
  if( expression ) sentence ;
     if( expression ) sentence 1;else sentence 2;
     if( expression ) sentence 1;else if sentence 2;else sentence 3;
  expression coca && || !( or And Not )
If the expression holds And execute multiple statements : Use -{} Generate code blocks  ->  { sentence };
  

Then let's talk about Dangling code The problem of :

For example, the following two codes :

int main() {
    int a = 0;
    int b = 2;
    if (a == 1)
        if (b == 2)
            printf("hehe\n");
        else printf("haha\n");
    return 0;// Nothing there?
}

int main() {
    int a = 0;
    int b = 2;
    if (a == 1)
        if (b == 2)
            printf("hehe\n");
    else 
        printf("haha\n");
    return 0;// Still not
}
// The second code puts else With the first if alignment , Try with the first if Form a selection statement , however , It's not right ,
// The real situation of the code is that of the first code , It's the second one. if And else Form statement ,
// In this code , Structure in order , From the top down , In judging a Is it equal to 1 when , because a=0, So the statement doesn't hold ,
// So the statement will not run any further , So nothing is printed .

// change a Value ; Make it established , as follows ;
int main() {
    int a = 0;
    int b = 2;
    if (a == 0)
        if (b == 2)
            printf("hehe\n");// Print  hehe 
        else printf("haha\n");
    return 0;
}
// Conclusion :else There is no match with the nearest one if Statement matching

The third point , about Optimization of writing method .

//if Contrast of writing forms :
// Code 1:
if (condition) {
    return x;
}
return y;
The code is established : Conditions established return x, Don't set up , return y.
// But the code is not readable ,
// Can be optimized downwards :
// Code 2:
if (condition) {
    return x;
}
else {
    return y;
}

And finally , About switch sentence .

switch sentence : It has to be integer ,case Must be followed by a constant . Start where the integer constant expression satisfies .
/*switch( Integer expression ){
Statement item
}*/
// About statement items :
//case Integral constant expression : sentence ;break;
// default : sentence ;( except case Other conditions )

Special form :

/*int main() {
    int s;
    scanf("%d", &s);
    switch (s) {
    case 1:
    case 2:
        printf("nihao");
        break;// A kind of
    case 3:
    case 4:
        printf("buhao");
        break;// A kind of
    }
    return 0;
}*/

This kind of writing is OK .
//case And default There is no order . And there can be if sentence , Allow nesting

原网站

版权声明
本文为[Yangasang 815]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202221413067811.html