The world of C language (6)

foreword

Hello, C language (select statement, loop statement)

The choice of sentences

In life, we are often faced with choices. What time will we start today? What to eat for lunch? Somehow, what we do is what we choose to do.

In C language, our common select statements are:

1.if-else statement

#include<stdio.h>
int main()
{
    char a[]={“Will you marry me?”};
    int a=0;
    scanf("%d",a);//输入a的值
    //判断并输出
    if(a==1)
    {
        printf("YES!");
    }
    else
    { 
        printf("NO!");
    }
    return 0;
}

If(), put an expression in parentheses. If the expression is true (not 0), the statement enclosed in if curly braces is executed, otherwise (else), the statement enclosed in else curly braces is executed.

The following is the standard code segment of the if-else statement

if ( 条件表达式 )
{
    语句1;
}    
else
{
    语句2;
}

2. Only the case of if

Of course, there are only if things in life, there are no other things,

For example: Your girlfriend said: "If you don't go shopping with me, you're done." (In this case, there is no room for maneuver)

So when we use the If statement, we can also omit the else

if(条件表达式)
{
      语句1;
}

3.if parallel situation.

"If you don't go shopping with me (if), you're done (statement 1), (else if) unless..." 

next code

if(条件表达式1)
{
    语句1;
}
else if(条件表达式2)
{
    语句2;
}

One more important thing: else is only paired with the if that is closest to it. 

2. switch statement

The standard form of a switch statement is as follows:


switch(表达式){
	case 常量值1:语句体1;break;
	case 常量值2:语句体2;break;
	default:语句体n+1;break;
}

case means case in English, here we can see it as a small box, the code is as follows:


switch(表达式){
	case 1:语句体1;break;
	case 2:语句体2;break;
	default:语句体n+1;break;
}

When we calculate the value of the expression after the switch, we enter the selection body

Come to case1 (small box 1) and see if 1 is equal to the value of the expression. If it is equal, we open the small box (that is, enter the statement body 1), and after running, it ends with break.

If none of the small boxes are equal to the value of the expression, then we execute the statement after default and end.

Second, the loop statement (we will one day jump out of the loop)

1. Types of loop statements

for(表达式1;表达式2;表达式3)
{
    循环体语句;
}

while(表达式)
{
    循环体语句;
}

do
{
    循环体语句;
} while(表达式);

1. When we generally use the for loop, we know the number of its loops.

Expression 1 is the initialization of the loop variable, expression 2 is the loop condition, and expression 3 is the operation to be performed after the loop ends.

(expressions 1, 2, 3 can sometimes be empty statements)

#include<stdio.h>
int main()
{
    for(int i=0;i<5;i++)
{
    printf("Don't give up!");
}
    return 0;
}

2.while (expression)

The loop body statement is executed when the expression is true, and not executed if it is false.

#include<stdio.h>
int main()
{
    int i=0;
    while(i<5)
{
    printf("Don't give up!");
}
    return 0;
}

3.do{} while();

Do it first and then judge the condition. The statement in do{} is executed first, and then use while() to judge whether the condition is still satisfied. If it is satisfied, repeat the loop again, otherwise, jump out of the loop.

Compared to a separate while() loop statement, it is done once more.

#include<stdio.h>
int main()
{
    int i=0;
    do
{
    printf("Don't give up!")
}while(i<5);
    return 0;
}

That's it for today's sharing. I'm a college student majoring in computer science. There are bound to be mistakes in the blog post. I hope you can correct me.

 

Guess you like

Origin blog.csdn.net/m0_60653728/article/details/122053489