当前位置:网站首页>009 basics of C language: C loop

009 basics of C language: C loop

2022-06-27 04:24:00 Prison plan progress 50%

One : summary

sometimes , You may need to execute the same piece of code multiple times . In general , Statements are executed sequentially : The first statement in the function is executed first , And then there's the second statement , And so on .

 

Two :while loop

 When a given condition is true , Repeat a statement or group of statements . It tests the condition before executing the loop body .
while(){
    
}
 example :
	#include <stdio.h>
	int main(){
    
		int a = 10;
		while(a<20){
    
			printf("a is : %d \n", a);
			a++;
		}
		return 0;
	}
 result :
	┌──(rootkali)-[~/Desktop/c_test]
	└─# ./while 
	a is : 10 
	a is : 11 
	a is : 12 
	a is : 13 
	a is : 14 
	a is : 15 
	a is : 16 
	a is : 17 
	a is : 18 
	a is : 19 

 

3、 ... and :for loop

Execute a sequence of statements multiple times , Simplify the code for managing loop variables .

for ( init; condition; increment )
{
    
   statement(s);
}

Here is for The control flow of the loop :

init Will be executed first , And only once . This step allows you to declare and initialize any loop control variables . You can also write nothing here , As long as a semicolon appears .

Next , Will judge condition. If it is true , Then the loop body . If it is false , The loop body is not executed , And the control flow will jump to the next for The next statement of the loop .

The execution of the for After cycling the subject , Control flow will jump back up increment sentence . This statement allows you to update the loop control variables . This statement can be left blank , As long as a semicolon appears after the condition .

The condition is judged again . If it is true , Then execute the loop , The process repeats itself ( Cycle subject , Then increase the step value , Then judge the conditions again ). When the condition becomes false ,for Cycle termination .

 example :
	#include <stdio.h>
	int main(){
    
		for(int a = 10; a < 20; a = a+1){
    
			printf("a is : %d \n", a);
		}
		return 0;
	}
 result :
	┌──(rootkali)-[~/Desktop/c_test]
	└─# vim for.c        
	┌──(rootkali)-[~/Desktop/c_test]
	└─# gcc for.c -o for    
	┌──(rootkali)-[~/Desktop/c_test]
	└─# ./for  
	a is : 10 
	a is : 11 
	a is : 12 
	a is : 13 
	a is : 14 
	a is : 15 
	a is : 16 
	a is : 17 
	a is : 18 
	a is : 19 

 

Four :do…while loop

Unlike for and while loop , They test the loop conditions at the loop head . stay C In language ,do…while A loop is to check its condition at the end of the loop .do…while Circulation and while Circulation similar , however do…while The loop ensures that the loop is executed at least once .

do
{
    
	statement(s);
}while( condition );
 example :
	#include <stdio.h>
	int main(){
    
		int a = 10;
		do{
    
			printf("a is : %d \n", a);
			a = a + 1;
		}while(a < 20);
		return 0;
	}
 result :
	┌──(rootkali)-[~/Desktop/c_test]
	└─# vim dowhile.c
	┌──(rootkali)-[~/Desktop/c_test]
	└─# gcc dowhile.c -o dowhile   
	┌──(rootkali)-[~/Desktop/c_test]
	└─# ./dowhile 
	a is : 10 
	a is : 11 
	a is : 12 
	a is : 13 
	a is : 14 
	a is : 15 
	a is : 16 
	a is : 17 
	a is : 18 
	a is : 19 

 

5、 ... and : Nested loop

Can be in while、for or do…while Use one or more loops within the loop .

 grammar :
C  In language   nesting  for  loop   Sentence syntax :
	for ( init; condition; increment )
	{
    
	   for ( init; condition; increment )
	   {
    
		  statement(s);
	   }
	   statement(s);
	}
C  In language   nesting  while  loop   Sentence syntax :
	while(condition)
	{
    
	   while(condition)
	   {
    
		  statement(s);
	   }
	   statement(s);
	}
C  In language   nesting  do...while  loop   Sentence syntax :
	do
	{
    
	   statement(s);
	   do
	   {
    
		  statement(s);
	   }while( condition );
	}while( condition );

 

6、 ... and : Infinite loop

#include <stdio.h>
int main ()
{
    
   for( ; ; )
   {
    
	  printf("hello cccccccc \n");
   }
   return 0;
}

 When the conditional expression does not exist , It is assumed to be true . You can also set an initial value and an incremental expression , But in general ,C  Programmers tend to use  for(;;)  Structure to represent an infinite loop .

 Be careful : You can press  Ctrl + C  Key to terminate an infinite loop .

 

7、 ... and : Loop control statement

		 Control statement 													 describe 
	break  sentence 							 End  loop  or  switch  sentence , The program flow will continue to execute immediately after  loop  or  switch  The next sentence of .
		break;
		
	continue  sentence 						 Causes the loop to skip the rest of the body , Immediately restart the test conditions .
		continue;
		
	goto  sentence 							 Transfer control to the marked statement . But it is not recommended to use  goto  sentence .
		goto label;
		..
		.
		label: statement;
原网站

版权声明
本文为[Prison plan progress 50%]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/178/202206270404363827.html