当前位置:网站首页>C language -- 14 loop statement for

C language -- 14 loop statement for

2022-06-10 18:15:00 Try!

1、 Loop statement for The grammar of

for ( expression 1; expression 2; expression 3)
	 loop ;

among , expression 1 Is initialization ; expression 2 Is a judgment ; expression 3 It's adjustment .
Example : use for Loop printing 1-10.

int main()
{
    
	int i = 0;
	for (i = 1; i < 11; i++)
		printf("%d ",i);
	return 0;
}
1 2 3 4 5 6 7 8 9 10

There's a point to make , If the judgment part is omitted , It means that the result of judgment is always true , The code goes into an infinite loop . Like the following code : Printing will continue after running haha

int main()
{
    
	for (;;)
	{
    
		printf("haha\n");
	}
	return 0;
}

 Insert picture description here
Add some conditions :

int main()
{
    
	int i = 0;
	int j = 0;
	for (i = 0; i < 3; i++)
	{
    
		for (j = 0; j < 3; j++)
		{
    
			printf("haha\n");
		}
	}
	return 0;
}

The code above will print 9 Time haha, Now omit i=0 as well as j=0 Conditions , Check the running results and you will find Print only three times

int main()
{
    
	int i = 0;
	int j = 0;
	for (; i < 3; i++)
	{
    
		for (; j < 3; j++)
		{
    
			printf("haha\n");
		}
	}
	return 0;
}

Because this program is executed first j<3 The three cycles of , After the end of the cycle ,j The value of is 3; here i=1,j=3, No longer satisfied j<3 The condition of the , So no more printing haha

2、for In the loop break as well as continue

stay for There are also... In the loop break as well as continue Of , Their meaning and in while The meaning in the cycle is almost the same , The difference is small .
About break, The code is as follows , The print result is still :1 2 3 4

int main()
{
    
	int i = 0;
	for (i = 1; i < 11; i++)
	{
    
		if (i == 5)
			break;
		printf("%d ", i);
	}
		
	return 0;
}

About continue, The result is 1-4,6-10( And while In the loop continue The execution results of are different ), Add the break Change it to continue that will do . The operation results are as follows :

1 2 3 4 6 7 8 9 10

The running result shows that 5, Because when i be equal to 5 when , Will skip the printed statement , But after skipping printing, you have to execute i++, So the back 6-10 Will still be printed .
for In the cycle continue Will jump to the adjustment section ; and while In the cycle continue Will jump out of the loop .

3、for Some suggestions for loop variable control of statements

  • Not in for Modify the loop variable in the loop body , prevent for The cycle is out of control
    The following code belongs to changing loop variables in the loop body , This is not good
int main()
{
    
	int i = 0;
	int j = 0;
	for (i = 0; i < 3; i++)
	{
    
		printf("%d ",i);
		int j = 0;
		for (j = 0; j < 3; j++)
		{
    
			printf("haha\n");
		}
	}
	return 0;
}
  • Suggest for The loop control variable value of the statement adopts “ Front closing back opening section ” How to write it .
    for (i = 1; i < 10; i++) This way of writing is “ Left closed right away ” Section ;for (i = 1; i <= 9; i++) Such a statement has the same meaning as the writing of "left close right open" , But it's not recommended to write . The writing method of "left close and right open" can make the program more readable .

4、for A variation of the cycle

It is also possible to control the loop with two variables .

int main()
{
    
	int x, y;
	for (x = 0, y = 0; x < 2 && y < 5; ++x, y++)
	{
    
		printf("haha\n");
	}
	return 0;
}

The run result will print two haha

5、 subject

See how many times the following code will loop ?

int main()
{
    
	int i = 0;
	int k = 0;
	for (i = 0, k = 0; k = 0; i++, k++)
	{
    
		k++;
	}
	return 0;
}

Running results :


The running result is nothing , Because at the break of the condition k=0 Of is assignment , take 0 Assigned to k,0 For false , This statement here is not a judgment condition , So it's going to cycle 0 Time , Print no results .

原网站

版权声明
本文为[Try!]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/161/202206101730494880.html