The goto statement in C language - when there are multiple layers of statements but the result is wrong, jump directly out of the layers of statements and re-select the answer to judge.

The goto statement is not commonly used, but when we continue to nest it, it will become a very useful tool.

Let’s talk about the conclusion first : The function of goto and flag is just to skip the code between them, but regardless of whether there is goto or not, the statements following the flag will be executed normally.

1. Usage scenarios (examples)

Here is an example:

Five athletes participated in the 10-meter platform diving competition. Someone asked them to predict the result of the competition:
Player A said: B second, I was third;
Player B said: I was second, E fourth;
Player C said: I was first, D is second;
player D said: C is last and I am third;
player E said: I am fourth and A is first;
after the game, each player got half of their words right, please program to determine the ranking of the game.

My idea is to initialize the array arr[6] to all 0s, and assign values ​​1,2,3,4,5,6 to each player A, B, C, D, and E.

Let the system generate random numbers 1 and 2 to use the switch statement to select the first half of the sentence and the second half of the sentence. Six  switch statements are needed here.

But here comes the question, if you choose the first half of player A’s sentence and the first half of player B’s sentence, is it necessarily wrong? We can directly deny the answer this time without going any further.

But we need a while loop to determine that each element of the array has a value. There is a switch statement nested in the while. In the case of the switch statement, use the if statement to determine whether the element has been occupied, and then use break or continue . At this time, only one switch statement can be jumped out , and the following switch statement cannot be avoided. At this time, it is more obvious to use the goto statement.

2. Use of goto statement

I also conducted related searches, but the results basically gave us a flag to show us the role of the goto statement.

2.1 goto statement syntax

goto name;

As shown in the picture:

int main()
{
	goto flag;
	printf("a\n");
	printf("b\n");

flag:
	printf("c\n");
	printf("d\n");
	return 0;
}

From the running results, we can conclude that the goto statement will skip the statement before the flag. 

2.2 About flag

We can delete the goto statement in the above code, or we can create an example of our own that can use the goto statement without using it:

int main()
{
	int i = 0;
	scanf("%d", &i);
	if (i == 1)
	{
		goto flag;
	}

flag: 
	printf("第一行:%d\n", i);
	printf("第二行:%d\n", i);
	printf("第三行:%d\n", i);
	printf("第四行:%d\n", i);

	return 0;
}

When we enter 1, the program will jump to flag, but there is a colon after flag: How many lines does it have scope? If the flag only applies to one line, will it be executed if I add a brace?

In fact, the function of goto and flag is just to skip the code between them, but regardless of whether there is goto or not, the statements following the flag will be executed normally.

Guess you like

Origin blog.csdn.net/m0_75186846/article/details/132093960