jump java_java跳转语句(Java jump statement).doc

java跳转语句(Java jump statement)

java跳转语句(Java jump statement)

2.3.5 jump statement

In addition to the if statements, switch statements, for statements, and while/do-while statements, Java also supports two other jump statements: the break statement and the continue statement. It is called a jump statement because Java makes the program move from the sequential execution to other parts through the two statements. Also note that there is no goto statement in Java, and the program's jump is implemented by means of break and continue.

1.break statement

In the switch statement, we have contacted the break statement, that is, it causes the program to jump out of the switch statement instead of sequentially executing the program in the following case.

In the loop statement, you use the break statement to jump out of the loop, ignoring any other statement of the loop body and the loop condition test. When a break statement is encountered in the loop, the loop terminates, and the program continues execution from the statement after the loop.

Unlike C and C++, there are no goto statements for arbitrary jumps in Java because the goto statement destroys the readability of the program and affects the compilation optimization. But Java can use break to implement some of the advantages of goto statements. Java defines an extended form of the break statement to handle this situation, that is, a tagged break statement. This form of break statement not only has the normal break statement jump function, but also can explicitly transfer program control to the designated place of the label. It should be emphasized that although this jump sometimes improves the efficiency of the program, it should be avoided. Tagged break statements are in the form of:

Break tags;

Look at the example below and take a closer look at the use of the break statement:

Int x = 0;

EnterLoop:// Tags

While (x < 10) {

X++;

System.out.println (enter loop, initial value of X: + X);

Switch (x) {

Case 0:

System.out.println (enter switch statement, x

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值