for loop statement in C language

Today I will share with you the basic syntax and basic usage of the for loop statement in C language. As usual, first of all, let's understand the grammatical format of the for loop

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

The above is the basic grammatical format of the for loop, let me briefly introduce it.

Expression 1: Set the initial condition, which is executed only once, and can set initial values ​​for multiple variables.

Expression 2: Loop condition expression, to determine whether to loop training.

Expression 3: Execute after executing the loop body.

It should be noted here that expression 1 is only executed once at the beginning of the loop, while expression 3 is executed after the loop ends. Expression 2 can be omitted. After omitting, the default value is 1. If it is judged to be true, the for loop will become an endless loop.

The most common format for writing code on a daily basis

for(定义循环变量初始值;循环条件;循环变量增值)
	语句;

For example, use a for loop to print the numbers 1-10

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
int main()
{
	int a = 0;
	for (a = 1; a < 11;a++)
	{
		printf("%d\n", a);
	}
	return 0;
}

 

 

Let me briefly explain the execution process of the above code, first define the integer variable a==0, enter the loop, define the initial value of the variable a as 1, judge the condition that a<11 is true, then output a, and finally a++, then We know that variable initialization is performed only once, variable

The value-added is executed after the loop ends, and a==1 is only executed once at the beginning. After assigning a value of 1, it has nothing to do with the loop. When a==1 enters the judgment condition <11 is true, the value of a is output as 1, and then a self-increment == 2, judge <11 again as true, output a value of 2, a self-increment again, and so on, and the cycle ends when the value of a reaches 11.

So since a==1 is only executed once and a++ is executed every time, can a==1 be placed outside the loop and a++ be placed in the loop body, of course.

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
int main()
{
	int a = 1;
	for (; a < 11;)
	{
		printf("%d\n", a);
		a++;
	}
	return 0;
}

 

 

However, it should be noted that even if expression 1 and expression 3 are removed, the semicolon inside () cannot be removed.

 break statement in for loop

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
int main()
{
	int i;
	for (i = 1; i < 11;i++)
	{
		if (i==5)
		{
			break;
		}
		printf("%d\n", i);
	}
	return 0;
}

 

 

When we added an if statement and a break to the code we just realized, the execution result changed. It can be seen that the loop ends when the execution reaches i==5, then we can easily know from this result that break means stop in the for loop.

continue in a for loop

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
int main()
{
	int i;
	for (i = 1; i < 11;i++)
	{
		if (i==5)
		{
			continue;
		}
		printf("%d\n", i);
	}
	return 0;
}

 

 When we replace the break in the above code with break, we find that the result has changed again, and we can see that the result does not output 5, then we can know that continue means to skip the current loop and execute the next loop.

Knowing the basic grammatical format of the for loop, let's do a small exercise.

Find 1! ——n! and

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
int main()
{
	int numb, i, n, ret = 1, sum = 0;   
	scanf("%d", &numb);					
	for (n = 1; n <= numb; n++)
	{
		ret = 1;
		for (i = 1; i <= n; i++)
		{
			ret *= i;
		}
		sum += ret;
	}
	printf("%d\n", sum);
	return 0;
}

 let me explain briefly

//If numb=3; when n=1, n<3, enter the loop, n=1, i=1, i==n, enter the inner loop, ret=ret*i=1, i++=2>n, The inner loop jumps out, sum=ret+sum=1,

//n++=2<=3 , enter the second outer loop, i=1, n=2, i<n, enter the inner loop, ret=ret*1=1, i++=2==n, loop ret again =ret*i=2, i++=3>n, jump out of the inner loop, sum=ret+sum=1+2, until n=3, sum=9

Then we can see that the above code nests a for statement in the outer for statement, then we can know that the foe loop can support nesting.

The above is today's sharing, so how much do you know about the for loop?

 

Guess you like

Origin blog.csdn.net/qq_53086187/article/details/124057912