Chapter 03 Control Flow
5. Unconditional Jump
5.1 Goto Statement
goto <statement-label>;
......
<statement-label>: <statements>; //here statements is called labeled-statement
goto
means jumping to the labeled-statement corresponding to its statement-lebel.Remember that we’re not allowed to use
goto
to jump through function definition or over variable definition.And we seldom use
goto
to jump from outside a compound statement to inside a compound statement.
5.2 Break Statement
break;
Break statement has two main functions:
- End a branch of the switch statement;
- End the iteration body containing it.
- Break statement only jumps out of the inner layer of iteration. If you want to jump out of multiple layers of iteration, please use goto statement or use a flag, which is a variable placed along with loop condition using
&&
.
5.3 Continue Statement
continue;
Continue statement can only be used in the iteration body, which means end the current loop and jump to the next loop.
6. Programming Style
Usually, when we judge a program, in addition to correctness and efficiency, readability is also a quite important criteria, which makes the program easy to maintain and indirectly influenced its correctness.
6.1 Structured Programming, SP
Structured Programming refers to a series of programming method which improves the readability and maintenance.
It demands the program itself is well-structured as well as the process of designing the program.
To a program, well-structured refers to:
- Each program should feature only one entrance and only one exit.
- There’s no statements in the program which will never stop implementing, which means the program must exit in a limited time.
- There’s no useless statements in the program, which means each statement in the program should be implemented or at least has a chance to be implemented.
6.2 About goto
goto
is the most primitive and classic control-statement, which is widely used in the early programs. But it will destroyed the one-entrance-one-exit feature of the program if used improperly, which betrays the principle of structured programming.And it has been proved that goto-statement is not necessary and irreplaceable, so we should try our best to avoid usage of goto-statement to move forward or backward.