当"if"出现时,会发生什么?语句以半冒号结尾 [英] What happens when "if" statement is ended with a semi colon

查看:220
本文介绍了当"if"出现时,会发生什么?语句以半冒号结尾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在程序中,我需要一个if语句,并且错​​误地将分号放在了该语句的末尾.但是,既没有编译时错误也没有运行时错误.我试图弄清楚这段代码的含义,但毫无希望.

In a program, I needed an if statement and by mistake, I put semicolon at the end of the statement. However, there were neither compile-time error nor run-time error. I tried to figure out what this code means but hopeless.

if (i == 10); 
{
    System.out.println("It is here");
    break;
} 

如果您能启发我这个话题,将不胜感激.

If you enlighten me on this topic, that will be appreciated.

推荐答案

if语句与以下代码块无关:

The if statement has nothing to do with the following block:

if (i == 10);

这是有效的语句,因为;表示一个空语句:if (i == 10)然后什么也不做.

This is a valid statement as ; denotes an empty statement: if (i == 10) then do nothing.

{
    System.out.println("It is here");
    break;
}

这是有效的代码块.从语法上讲,这是正确的,尽管在这种情况下并没有多大帮助.在任何情况下都将执行此块,并且不受上述if语句的影响.

This is a valid code block. It is syntactically correct although it does not help a lot in this case. This block will be executed in all cases and is not affected by the if statement above.

这篇关于当"if"出现时,会发生什么?语句以半冒号结尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆