BP-3-4 Unconditional Jump and Programming Style

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:

  1. End a branch of the switch statement;
  2. 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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值