Program Design and Application of Cyclic Structure

Introduction to Loop Statements

  • In reality, there are many problems that require regular repetition of certain operations. These operations appear in the computer as certain statements are executed repeatedly when certain conditions are met. This is a cycle.

For example: if you want to print "hello world" 100 times, you need to use a loop statement, and the condition is to print 100 times, which is very convenient.

1. The while loop statement

  • general structure

while(expression)
{ statement body; }

  • expression:condition for the loop. It can be any legal expression in C language, generally a relational or logical expression.
  • Statement body:That is, the repeated operation, also known as the loop body. If there are multiple statements that need to be enclosed by "{ }", they appear in the form of compound statements.
  • its characteristics: It is to judge the condition first and then execute the statement body.
  • There must be a statement in the body of the while loop that changes the loop condition, so that the loop tends to end, otherwise an infinite loop will be formed.

1. The execution process of the while statement

First calculate the value of the expression, if the value of the expression is not 0 (true), then execute the statement of the loop body, then recalculate the value of the expression, and judge whether the value is true again, if it is true, then continue to execute Go down, know that the value of the expression is 0 (false), then jump out of the loop structure.

2. Code implementation

1+2+3+4+…+99+100=?

int main()
{
    
    
	int n=0;
	int k=0;	
	while(n<100)
	{
    
    
		k += (++n);
	}
	printf("%d ",k);
	return 0;
}//输出 5050

Two, do...while loop

  • general structure

do
{ statement body; }while(expression);

  • expression:condition for the loop. It can be any legal expression in C language, generally a relational or logical expression.
  • Statement body:That is, the repeated operation, also known as the loop body. If there are multiple statements that need to be enclosed by "{ }", they appear in the form of compound statements.
  • its characteristics: First execute the statement body once and then make a judgment, so the statement body will be executed at least once.
  • do…while the last semicolon ":" cannot be omitted;

1. The execution process of the do...while statement

First execute the statement body after do, and then calculate the value of the expression after while, if the value of the expression is not 0 (true), continue to execute the loop statement, and so on, until the value of the expression is 0 (false), Then exit the loop structure.

  • Note :
  • 1) The characteristic of the do...while loop statement is to execute the loop body first and then judge the condition, so no matter whether the condition is true or not, the program will be executed once, which isThe essential gap between it and the while loop statement
  • 2) Regardless of whether the loop body is one statement or multiple statements, it is best to enclose it with "{}".

2. Code implementation

1+2+3+4+…+99+100=?

int main()
{
    
    
	int i = 0;
	int k = 0;

	do
	{
    
    
		k += ++i;

	} while (i<100);

	printf("%d ", k);
	return 0;
}//结果输出 5050

Three, for loop statement

  • general structure

for (expression 1; expression 2; expression 3)
{ statement body; }

  • Expression 1:It is generally used to assign an initial value to a loop variable, so it is often an assignment expression, specifies the location of the loop; if the loop variable is assigned an initial value outside the for loop, this expression can be omitted.
  • Expression 2:Indicates the loop condition, which determines whether the loop continues or ends, generally a relation or a logical expression;
  • Expression 3: It is generally used to modify the value of the loop variable, and the control variable changes according to the way of life every cycle.Thus changing the truth or falsehood of expression 2 (that is, changing the condition of the loop), generally an assignment statement.
  • Note:Separate the three expressions with a semicolon ";", other symbols cannot be used;

1. The execution process of the for loop statement

1. Execute expression 1 first.
2. Then calculate expression 2. If it is not 0 (true), execute the loop body; if it is 0 (false), then end the loop.
3. Then calculate the expression 3, and then repeat the second step

2. Code implementation

1+2+3+4+…+99+100=?

int main()
{
    
    
	int i = 0;
	int j = 0;

	for (i = 0; i <= 100; i++)
	{
    
    
		j += i;
	}

	printf("%d", j);
	return 0;
}//输出 5050

3. Several forms of for loop statement

  • The three expressions in the for statement can be omitted according to requirements, but the semicolon cannot be omitted.
    Here is an example:

If all three expressions are omitted;

int main()
{
    
    
	int i = 0 ;
	int j = 0;
	for (;;)
	{
    
    
		j += i;
		i++;
	}
	printf("%d ", j);
	return 0;
}// 死循环

  • The three expressions are all omitted, there is no initial value, the condition will not be judged, the loop variable will not change, and there will be an infinite loop

3. Loop nesting

Introduction

  • Include another loop statement in the body of a loop,When loops are nested, the outer loop is executed once, and the inner loop is executed from beginning to end.

Code

  • 99 multiplication table
int main()
{
    
    
	int i = 0;
	int j = 0;

	for (i = 1; i <= 9; i++)
	{
    
    
		for (j = 1; j <= i; j++)
		{
    
    
			printf("%d*%d=%-2d ", i, j, i * j);
		}
		printf("\n");
	}
	return 0;
}

99 multiplication table

Four, break, continue and goto statements

1. General format of break:

break statement:break;, which is used to forcibly end the loop and turn to the next statement of the loop statement;

int main()
{
    
    	int i=0;
	for(i=0;i<=10;i++)
	{
    
    
	printf("haha");
	break;
	}
	printf("hehe");
	return 0
}//则这个循环只会打印一次haha;之后会一遇到break跳出循环,执行打印hehe;

2. The general format of the continue statement

  • continue statement:continue;End this cycle. For the while and do...while loops, skip the remaining statements in the loop body, and turn to the judgment of the loop termination condition; for the for loop, skip the rest of the loop body statements, and turn to the calculation of the loop variable change expression.

3. goto statement and label statement

  • goto statement format:goto<statement label>;

Note:

  • The goto statement is an unconditional transfer statement. When the program executes the goto statement, it unconditionally transfers to the statement specified by <statement label> and executes it.

  • <statement label> is an identifier and should be named according to the naming rules for identifiers.
    The format of a label statement:
    Statement label:

  • A label statement is placed in front of a certain line, and a colon ( : ) is added after the statement label.

  • The statement label plays the role of identifying the statement, used in conjunction with the goto statement, and is the transfer target of the goto statement.

int main()
{
    
    
	int i,j = 0;
	int a[3][3] = {
    
     1,2,3,4,5,6,7,8,9 };
	for (i = 0; i < 3; i++)
	{
    
    
		for (j = 0; j < 3; j++)
		{
    
    
			if (a[i][j] == 6)
			{
    
    
				goto find;
			}
		}
	}
find:
	printf("%d %d\n", i, j);
	getchar();

	return 0;
}

  • The goto statement can jump to a specified location to execute a new program, often used to jump out of multiple loops

summary

  • Pay attention to the distinction between break and continue, break is to end the cycle of this layer, and continue is to end the cycle of this layer.
  • The while and do...while loop statements are applied with attention to the fact that the latter executes the loop at least once.

Guess you like

Origin blog.csdn.net/m0_66780695/article/details/130329187
Recommended