While, do{...}while, for loop, switch, if branch in C voice#Detailed talk

One, what is a cycle?

Generally speaking, a loop is to do one thing repeatedly. The language converted on the computer is to run the code in it repeatedly when the conditions are met, and continue to run downwards until the conditions are no longer met.

Second, why do we need loops?

Sometimes our program needs to do some repetitive instructions or actions. However, it would be unrealistic for the programmer to write out these repeated instructions or actions one by one. In the face of this situation, we need our loop structure , so that programmers can simplify the implementation scheme.

1.1 while loop  

The while loop is one of the most basic loop structures in the C language. Its usage is very simple, including keywords and loop conditions.

Loop when the loop condition is true, and jump out of the loop when the condition is not true. Note that the condition is sometimes not constant, and while has no header file.

example: 

int main()
{
 int arr = 0;
    while(arr < 5)
    {
      arr++;
    }
    return 0;
}

 Explanation : We usually write loop conditions in parentheses, and we also write conditions in the form of operations, so we have done two things for a long time, one is to perform calculations, and the other is to perform conditional judgments. Like this arr++ is a fast operation code. When arr<5, the operation of arr++ will be executed all the time. During the operation of arr++, arr is changing, so when arr is equal to 5, it will jump out of the loop. while loops five times.

Be careful not to write while as an infinite loop.

 Flow chart analysis:

 1.2 do{...}while loop

 The do{...}while loop is almost the same as the while, and the do{...}while loop is always one more time than the while. It will first run the code once and then judge the condition. So do{...}while will execute the code block at least once. Of course, if the value of the control expression is true, then another loop will continue; if it is false, the loop ends.

Comparison example:


int main()
{
	int i = 1;//初始化
	do
	{
		printf("%d ", i);
		i = i + 1;//调整部分
	} while (i  == 0);//判断条件
	
	return 0;

If this piece of code is just the judgment condition of while, it will not be executed at all, because i will never be equal to 0, but if it is a do{...}while loop, it will be executed once, and printf and i = i + 1.

1.3 for loop

for is also one of the most basic loop structures in C language, and it is even more used than while loop, and its understanding is relatively simple.

for (initialization expression; conditional expression; loop variable change expression) {

        // loop body

    }

There must be two ;; in the parentheses of the for loop, even if their statements are empty. The initialization expression can be defined inside or outside the brackets, but the initialization expression must be assigned. The conditional expression can be written or not. If it is not written, it will be an endless loop, which is generally meaningless. The loop variable change expression can be written in parentheses or in the curly braced loop body, it's up to you .

Let's look at one:


int main()
{
	int i = 0;
	for (i = 0; i < 10; i++)
	{
		printf("%d ", i);
	}
	return 0;

printf loops 10 times.

Third, what is a branch?

A branch is a multi-condition with many options, such as if....then...or if...if...if...then... judge. It is a logical analysis.

Fourth, why do we need branches?

In reality, we have many choices, and we have different responses to different choices. When converted to a computer, there are different branches, and different code blocks are executed in different branches.

2.1 switch branch

The switch branch is one of the two major branches in the C voice branch. Compared with the if branch, it is simpler to use. When it supports many choices, it will be more readable than if.

int main()
{
    int day = 3;
    switch(day)
    {
         case 1 :
        printf("星期一\n");
        break;
         case 2 :
        printf("星期2\n");
        break;
         case 3 :
        printf("星期3\n");
        break;
         case 4 :
        printf("星期4\n");
        break;
         case 5 :
        printf("星期5\n");
        break;
         case 6:
        printf("星期6\n");
        break;
         case 8 :
        printf("星期7\n");
        break;
        default:
        printf("we");
        break;
    }
    return 0;
}

Explanation: Here, 1 to 8 are used to select the printing of printf. The expression in its brackets is a specific variable, and the case is a part of Switch, indicating different choices. The value behind it must be a specific value, which is not allowed It is a variable, it cannot be a string , you need to use if to judge the string; default is also a part of switch, but it can be omitted, and it can also be placed after any case.

2.2 if branch

The if branch is one of the two major branches in the C voice branch. It is used more than switch. If can be written alone or in conjunction with else. if is equivalent to if, else is equivalent to then, of course if == else if, else if can be written multiple times.

for example:

		int grade = 88;
		if(grade>100 || grade<0){
			 printf("请输入合理成绩!");
		}
		else if(grade==100){
			 printf("恭喜你100分");
		}
		else if(grade>80 && grade<=90){
			 printf("您的成绩为"+grade+"");
		}
		else if(grade>=60 && grade<=80){
		     printf("您的成绩为"+grade+"");
		}
		else{
			 printf("对不起您成绩太差");
		}
	

In parentheses are the judgment conditions.

Five, the role of break and continue

1. break is used to jump out of a loop body or completely end a loop . It can not only end the loop it is in, but also end its outer loop.

break can only be used in the body of the loop and the body of the switch statement.

When break appears in the body of the switch statement in the loop body , the function is only to jump out of the switch statement body , and it cannot terminate the execution of the loop body . If there is no break in the body of the switch case statement, it will run to each case starting from the value specified in the case .

2. The function of the continue statement is to skip the remaining unexecuted statements in the current loop body, and immediately proceed to the next loop condition judgment, which can be understood as just aborting (skipping) this loop, and then starting the next loop. The continue statement does not terminate the entire loop.


Family members, come here, use your little hands to get rich, give me a little attention, like me, your support is the driving force for my creation, I will continue to publish high-quality articles for everyone to learn together.

Welcome to learn and communicate together, criticize and correct.

Guess you like

Origin blog.csdn.net/2301_77479336/article/details/130087491