如果条件为简写,则多个语句if [英] Multiple statements if condition is true in shorthand if

查看:150
本文介绍了如果条件为简写,则多个语句if的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近发现了if语句的简写,并且在网上搜索后找不到明确的答案。

I recently discovered the shorthand if statement and after searching online I couldn't find a definite answer.

如果条件为真/假,是否可以执行2条语句?

Is it possible to execute 2 statements if the condition is true/false?

int x = (expression) ? 1 : 2;

例如

int x = (expression) ? 1 AND 2 : 3;

看到我还没有碰到他们使用它的示例,我想这是不可能的,但是我

Seeing as i haven't comne across a example where they used it I guess it's not possible but I wouldn't want to miss out.

推荐答案

您正在谈论条件赋值。您应该看一下所定义的内容:

You're talking about conditional assignment. You should look at what is defined by what you've written:

int x = (expression) ? 1 AND 2 : 3;

这是在评估表达式,如果为true,则执行 1 AND 2,然后将值赋给x 。如果将表达式评估为false,则将评估 3并将其分配给x。因此,您绝对可以执行以下操作:

That is evaluating 'expression' and if true executing '1 AND 2' then assigning the value to x. If 'expression' evaluated to false, '3' is evaluated and assigned to x. Hence you could definitely do something like this:

int x = (expression) ? GetInt1() + GetInt2() : 345;

重要的是,如果发现的话,您所发现的不仅仅是速记。这是有条件的分配

What is important is that what you have found is not just a shorthand if. It is conditional assignment.

这篇关于如果条件为简写,则多个语句if的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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