C language advanced: 7, loop statement

The basic way the loop statement works:

    Determine whether to execute the loop body through a conditional expression;    

    Conditional expressions follow the principles of if statement expressions;

The difference between do, while, and for statements:

        The do statement is executed first and then judged, and the loop body is executed at least once;

        The while statement is first judged and then executed, and the loop body may not be executed;

        The for statement is judged first and then executed, which is more concise than while.

The loop of do...while:

	do
	{
		//loop
	}
	while(condition);

The while statement loops:

	while()
	{
		//loop
	}

The looping method of the for statement:

for(i=0; condition; i++)
{
	//loop
}

Observe the following sample code to experience three different loops:

int f1(int n)
{
	int i=1;
	int s=0;
	
	if(n>0)     //Be careful to judge first
	{
		do
		{
			s += i;
			i++;
		}while(i <= n);
	}
	
	return s;
}

int f2(int n)
{
	int ret=0;
	
	while(n>0)
	{
		ret + = n;
		n--;
	}
	
	return ret;
}

int f3(int n)
{
	int ret=0;
	int i=0;
	
	for(i=0; i<=100; i++)
	{
		ret + = i;
	}
	
	return ret;
}

Focus: The difference between break and continue:

            break means to terminate the execution of the loop;

            continue means to terminate the current loop and enter the next loop execution.

Question: Can the continue keyword be used in a switch statement? Why?

        can not. continue, for looping.

Determine what the following sample code will output?

#include <stdio.h>

void f1(int n)
{
	int i=0;
	for(i=1; i<=n; i++)
	{
		if( i%2 == 0)
		{
			break; //Conditional, jump out of the loop directly
		}
		printf("%d\n", i);
	}
	
	printf("\n");
}

void f2(int n)
{
	int i=0;
	for(i=1; i<=n; i++)
	{
		if( i%2 == 0)
		{
			continue; //If the conditions are met, jump out of this loop and enter the next loop, if the conditions are not met, print
		}
		printf("%d\n", i);
	}
	
	printf("\n");
}

intmain()
{
    f1 (10);
    f2(10);
	return 0;
}


The result of compiling and running with Gcc under Linux is:

delphi@delphi-vm:~/will$ gcc test.c
delphi@delphi-vm:~/will$ ./a.out
1

delphi@delphi-vm:~/will$ gcc test.c
delphi@delphi-vm:~/will$ ./a.out
1
3
5
7
9

do...break statement: End the loop body violently without causing a memory leak.

Observe the following sample code to experience the effect of do...break:

do
{
    break;
}while(0);

#include<stdio.h>
#include<malloc.h>

int func(int n)
{
	int i=0;
	int ret =0;
	int *p = (int *)malloc(sizeof(int)* n);
	
	do{
		if(p ==NULL) break;
		//Direct return is wrong, because the requested memory is not released
		if( n<5) break;   
		//return memory leak
		if( n>100) break;  
		//return directly ends the function
		
		for(i=1; i<n; i++)
		{
			p[i] = i;
			printf("%d\n", p[i]);
		}
		
		ret = 1;	
	}
	while(0);
	
	free(p);
	print("Free(p)\n");
	return ret;
}

In the loop body of the above code, as long as the conditions are met, the loop is exited, and the memory space can be released without causing memory leaks.

If return is used directly, the function ends directly and memory leaks.

		if(p ==NULL) return ret;
		                            //Direct return is wrong, because the requested memory is not released
		if( n<5) return ret;   
		                            //return memory leak
		if( n>100) return ret;  
		                            //return directly ends the function
		
		for(i=1; i<n; i++)
		{
			p[i] = i;
			printf("%d\n", p[i]);
		}
		
		ret = 1;

Use the do...break statement to avoid certain statements without causing a memory leak.

summary:

The advanced judgment of the for loop enters the loop body, which is suitable for occasions where the number of loops is fixed;

The while loop also judges first and then enters the loop body, which is suitable for occasions where the number of loops is not fixed;

The do...while loop executes the loop body first and then the conditional loop, executing the loop body at least once, and

do
{
    break;
}while(0);
It has a good effect on preventing memory leaks.


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325568087&siteId=291194637