Q: Do I have to have a break statement in the statement sequence after case in a switch statement?
Answer: 1) use the break statement in the switch statement to terminate the sequence of statements. In the case of a Break statement, the program executes a line of code after the entire switch statement, which has the effect of jumping out of the switch statement.
The break statement is optional. If the break statement program is omitted, the next case is executed. If you need to use multiple cases, you do not need to use the break statement.
The use of switch statements is more efficient than if else in Multi-choice.
2) Do you know what break means? If there is no break, the program will continue to judge after leaving
========================================================== ========================================================== ============
Generally, there are four types of Jump statements used in the C language:
Goto
Continue
Break
Return
First, we will give you a brief introduction to the four types:
Goto:
It is used for inter-module jump. The secondary jump function is powerful and can be used to jump anywhere within the program. The call form is:
A: Module 1; // here, A is A identifier, which is often expressed by uppercase letters, indicating the starting position of the program execution after the jump
Module 2
{
Goto A; // jump to A and execute
}
In C language, this type of call usually occurs in menu creation. It is used for switching between multiple modules and can be nested. However, this programming method is not recommended and may cause unpredictable bugs during the jump process. Avoid using it whenever possible.
Continue
Continue is generally used to accelerate the loop. It is generally called within the loop body. Its function is to end this loop and quickly enter the next loop. For example:
For (;;)
{
............;
If (ture)
Continue;
............;
}
When the if condition is true, the loop goes into the next time;
Break
It is generally used to jump out of this loop, that is, the loop in which the break exists. The loop in which the break is called will jump out;
For example:
While ()
{
For (;;)
{
............
Break;
......
}
}
In this template, for calls break. When it reaches break, it jumps out of for, but does not jump out of while. Instead, it continues to execute the while loop.
Return
In general usage, it is okay to return a value or other complex types to achieve the return,
Return is generally used in the called object (Object 1). After a call, it is returned to the called object (Object 2). That is, when object 2 calls an object for a while, if the return statement is called internally by object 1, the program returns the result of execution. When object 2 is called, the next statement is executed,
For example: (the object here is a function in C)
Object 1 ()
{
Return;
}
Object 2 ()
{
Statement 1;
Object 1 ();
Statement 2;
}
When the program executes to object 2, it will call object 1 and convert it to object 2 for execution. When the return is executed, it will return to object 2 and execute Statement 2.
Your question is explained as follows:
First program section
For the return call, after the call, the program will jump out of the main function, that is, return has the function to end the function call.
The second program section:
Do
While ();
Is a loop body. break is called within the loop and jumps out of the loop, but does not jump out of the if,
In general, the redirection in the language satisfies the above rules, but it is not ruled out. It is used in special algorithms.
Hope you can understand
======================================
Break; jump out of the loop;
Return; return the method directly to end the entire function.
======================================
Author: "YEYUANGEN's column"