5. Loop structure (while loop, for loop, do while loop, break and continue, goto statement)

1.while loop

Format:

while(条件表达式){
    
    
	代码段1;
}

Step 1: If the conditional expression is established (not 0), execute code segment 1 (excluding break and other factors to jump out of the loop directly) and then
judge the conditional expression. If it is established, repeat step 1
until the conditional expression is not established (is 0), exit the while loop


Example 1: Use a while loop to print 1-10

//打印1-10
int main() {
    
    
	int i = 1;
	while (i<=10)
	{
    
    
		printf("%d\n", i);
		i++;
	}
	return 0;
}



Example 2: Enter a positive integer and print the number in reverse order
. Idea:
Step 1:
Get the lowest digit n%10 of n.
For example: 2345%10=5.
Step 2:
Remove the lowest digit n/10 of n
. For example: 2345/10=234.
Repeat steps 1 and 2 until n=0 to obtain a string of integers in reverse order.

int main() {
    
    
	int i = 0;
	scanf("%d",&i);
	while (i)//等同于 i != 0 的效果
	{
    
    
		int a = i % 10;
		printf("%d", a);
		i /= 10;
	}
	return 0;
}



2.for loop

Format:

for(表达式1;表达式2;表达式3){
    
    
	代码段;
}

Expression 1 Initialization of loop variables
Expression 2 Conditional judgment of loop end
Expression 3 Adjustment of loop variables


Example 1: Use for loop to print 1-10

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



Example 2: Enter a positive integer and print the number in reverse order
. Idea:
Step 1:
Get the lowest digit n%10 of n.
For example: 2345%10=5.
Step 2:
Remove the lowest digit n/10 of n
. For example: 2345/10=234.
Repeat steps 1 and 2 until n=0 to obtain a string of integers in reverse order.

//输入一个正整数,逆序打印正整数
int main() {
    
    
	int i = 0;
	for (scanf("%d",&i); i!=0; i/=10) {
    
    
		int a = i % 10;
		printf("%d", a);
	}
	return 0;
}

Example 3: Calculate the sum of numbers that are multiples of 3 between 1-100

int main() {
    
    
	int sum = 0;
	for (int i = 1; i <= 100; i++){
    
    
		if (i % 3 == 0) {
    
    
			sum += i;
		}
	}
	printf("%d", sum);
	return 0;
}

Output result: 1683

3. do while loop

The do while statement is the least used among loop statements.

Format:

do{
    
    
	语句1;
}while(条件表达式);

do while is to execute statement 1 inside the loop first (Therefore, the statements in the loop body will be executed at least once in the do while loop.)
and then start to judge the conditional expression of while. If it is true, execute statement 1; if it is not true, exit the loop.


Example 1: Use do while loop to print 1-10

int main() {
    
    
	int i = 1;
	do{
    
    
		printf("%d",i);
		i++;
	} while (i<=10);
}

Example 2: Enter a positive integer and calculate the number of digits this integer has.

int main() {
    
    
	int i = 0;
	int count = 0;
	scanf("%d", &i);
	do{
    
    
		i /= 10;
		count++;
	} while (i!=0);
	printf("%d", count);
}

4.break和continue

The function of break directly terminates the loop (more accurately, it is the first layer of loop structure outside break). The function
of the jump-out loop statement continue is to skip this loop (that is, directly ignore all codes below continue in the loop body. Directly re-enter the conditional judgment part of the loop)

4.1 break and continue in while

Break
example: Use a while loop to print 1-10. If 5 is encountered, stop printing immediately.

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

continue
example: use while loop to print all numbers from 1-10 except 5

//continue
int main() {
    
    
	int i = 1;
	while (i<=10)
	{
    
    
		if (i == 5) {
    
    
			i++;
			continue;
		}
		printf("%d ", i);
		i++;
	}
	return 0;
}

4.2 break and continue in for

Break
example: Use a for loop to print 1-10. If it encounters 5, it will stop printing immediately.

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

continue
example: Use a for loop to print all numbers from 1-10 except 5

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

4.2 break and continue in do while

It is basically the same as the usage in while.
In fact, there is basically no difference in the usage of break and continue among the three loop statements of while, for and do while. If I want to say what is different, I
can only say that the loop structure of for and while is slightly different, which does not affect break and continue. How to use continue

5.goto statement

Format:

goto 跳转标识;
.....
.....
跳转标识:

The goto statement and jump label are a special syntax in C language.
Function: The goto statement can jump to the set jump label within the same function.

Suitable scenarios:
Multi-level nested loop statements. If you want to use break to terminate the loop, but break can only jump out of the outer loop . If the nesting is deep, it will be very troublesome
. Goto is actually rarely used in programs. But in a multi-layer nested loop statement, if you want to break out of the loop, using goto is a good choice.

	int count = 0;
	for (int i = 1; i < 10; i++)
	{
    
    
		for (int j = 1; j < 10; j++)
		{
    
    
			goto outside;
		}
	}

outside:
	printf("goto跳出循环");

If you want to use break to jump out of a loop, you must write a break in each loop body.
Here, only two layers are used, so it does not look obvious. If the number of nested layers is huge, you will need to write many breaks to completely jump out of the loop.
Goto only needs to be in Mark the outermost layer and write the goto jump mark in the innermost loop body to jump out of the loop.

Guess you like

Origin blog.csdn.net/qq_45657848/article/details/131890755