[C Language] The difference between jump statements that change the loop structure (break, continue, goto statements)

1. break statement and continue statement

break statement function

The break statement is used in loop statements and switch statements

  • When used in a loop statement: it can cause the program to terminate the loop and execute the subsequent statements of the loop statement. A break statement can only jump out of one loop (not a loop, but a layer of loops); usually the break statement is always combined with the if statement Used together, that is, when the conditions are met, the loop of this layer will be jumped out.
  • When used in a switch statement: see below

continue statement function

The continue statement can only be used in loop statements

  • Skip the statement after continue in the loop body
  • Often used with if statements, that is, jumping out of this loop when the conditions are met

specific explanation

1. In a for loop

for statement execution flow

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

When executing the loop body statement:

  • When encountering break: jump out of the for loop and execute the next statement of the for loop.
  • Encountered continue: jump to expression 3

2. In a while loop

while statement execution flow
When executing the loop body statement:

  • Encounter break: jump out of the while loop and execute the next statement of the while loop
  • Encounter continue: jump to expression

3. In the do...while loop

do-while statement execution flow
When executing the loop body statement:

  • Encounter break: jump out of the do-while loop and execute the next statement of the while loop
  • Encounter continue: jump to expression

4. The function of break statement in switch statement

When executing the switch statement:

  • When encountering break: jump out of the switch statement and execute the next statement of the switch statement.

2. Comparison of the differences between break statement and continue statement

Taking the for loop as an example, first look at the code
break statement:

#include<stdio.h>
int main()
{
    
    
	int i, sum = 0;
	for (i = 1; i < 4; i++)
	{
    
    
		if (i < 2)
		{
    
    
			sum = sum + 1;
			printf("%d\n", sum);
		}
		if (i ==2)
		break;
		if (i == 3)
			printf("3");
	}
	return 0;
}

The result is:

1

continue statement:

#include<stdio.h>
int main()
{
    
    
	int i, sum = 0;
	for (i = 1; i < 4; i++)
	{
    
    
		if (i < 2)
		{
    
    
			sum = sum + 1;
			printf("%d\n", sum);
		}
		if (i ==2)
		continue;
		if (i == 3)
			printf("3");
	}
	return 0;
}

The result is:

1
3

Monitoring and debugging:

  • break:
    break
    As you can see from the picture above, when i=2, the break statement is executed, and then the loop is jumped out directly, the return 0; statement is executed, and the code ends. The 3 here is not printed.

  • continue:
    continue
    From the picture above, you can see that when i=2, the continue statement is executed, and then jumps to expression 3 of the for loop, which is i++ (you can see that i changes from 2 to 3 after the for loop is executed) , and then continue to execute the following code normally. The 3 here are printed.

3. goto statement

goto statement label;

  • The statement label is a valid identifier. When used, a colon ":" follows the statement label and appears in front of a statement in the function. When the program executes the goto statement, control will jump to the statement label to achieve the purpose of controlling the loop.
  • For example:
#include<stdio.h>
int main()
{
    
    
	int i=1;
	int sum=0;
	loop:if(i<=100)//loop 为跳转语句的语句标号
		{
    
    
		    sum=sum+i;
			i++;
			goto loop;//跳转到语句标号loop处继续执行
		}
		printf("sum=%d\n",sum);
	    return 0;
}

Guess you like

Origin blog.csdn.net/m0_74102736/article/details/129729199