Xiao Zhangzhang takes you 0 basics to learn C language loop (for loop statement, while loop statement, do-while loop statement)

Detailed explanation of loop statements in c language (0 basics can also be understood)



Preface

The loop statement is an indispensable part of the program. Today Xiao Zhang Zhang will show you the loop statement .


One, for loop

In the C language, the for statement is called a loop statement, which can be executed repeatedly in the C language.
The general form of the for statement is:

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

Note:
1. In the for loop statement, use two semicolons to separate the three expressions, but there is no semicolon after the for, because for and the following loop body statement are combined as a complete statement.
2. When the for statement is executed, as in the flowchart, first calculate expression 1; when judging expression 2; if the result is "true", execute the loop body statement, and then calculate expression 3, and then loop; if "false" ", the result loops, and the execution of the next statement of for continues.


Insert picture description here
Friendly Tips:
1. The execution order and writing order of the three expressions in the for statement and the loop body statement are different, and the calculation expression 3 is after the loop body statement is executed.
2. As shown in the figure above: in the execution of the meat and for loop statements, one of the expressions is executed once, and the rest are executed repeatedly.
In simple terms: expression 1 (initial value expression), expression 2 (conditional expression), expression 3 (step length expression), loop body statement (the statement to be executed repeatedly can only be one).
Note: If the loop body statement is composed of multiple statements, it must be expanded with braces to become a compound statement.


Simple code display: look at how the for loop statement calculates 1+2+3...+100

#include <stdio.h>
int main()
{
    
    
	int i, sum=0;
	for(i=1; i<=100; i++)
	{
    
    
		sum+=i;
	}
	printf("计算得出:sum=%d\n",sum);
	return 0;
}

Insert picture description here



Two, while loop


In addition to the for loop statement introduced above, the while statement can also implement loops, and the while loop is broader. Its general expression is:
	      while(表达式)
	     		循环语句;

Insert picture description here

The while statement has a simple structure, with only one expression and one loop statement (loop condition loop body).
Note:
1. Because the result of the while statement is simple, the while statement must include operations that ultimately change the true or false of the loop condition.
2. The () in the while statement cannot be omitted, and the expression type is not restricted. If there are multiple statements, {} must be used. The statements in the loop body that change the conditions of the loop body are loop statements and can be empty statements.


Simple code display: look at how the while loop statement calculates 1+2+3...+100

#include <stdio.h>
int main()
{
    
    
	int sum = 0, i = 1;
	while (i<=100)
	{
    
    
		sum += i;
		i++;
	}
	printf("计算得出:sum=%d\n",sum);
	return 0;
}

Insert picture description here



Three, do-while loop


The do-while statement is different, it executes the loop body first, and then executes the loop condition.
General form:

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

Insert picture description here


Simple code display: look at how the do-while statement calculates 1+2+3...+100

#include<stdio.h> 
int main(void)
{
    
    
	int sum=0;
	int i=1;	
	do{
    
    
		sum=sum+i;
		i++;
		}while(i<=100);
printf("计算出sum=%d\n",sum);
}
return 0;

Insert picture description here



Four, thinking


Now that we have learned so many loop statements, how do we distinguish them and how to use them? ? ?

Let's talk about this topic next time.


If this article is helpful to your doubts, you might as well give me more motivation to continue to the next chapter with one click and three consecutive sessions. Thank you.
This article is written by myself, there must be some deficiencies, I hope to correct it!

(﹡ ˆOˆ ﹡)

Guess you like

Origin blog.csdn.net/qq_51932922/article/details/114169784