简体   繁体   English

Java中是否可能有一个不使用关系运算符的if语句?

[英]Is it possible to have an if statement in Java that doesn't make use of relational operators?

This isn't for a specific code, I'm just curious. 这不是特定的代码,我很好奇。 Relational operators as in == , < , > , >= , <= , != . 关系运算符,例如==<>>=<=!=

Of course: 当然:

if (processing)
{
    // enter if the boolean processing is true
}

Lets say you have a function that returns a boolean value 假设您有一个返回布尔值的函数

  public boolean isTrue()
  {
    //Some code
    //return true or false

  }

Then the 'if' statement will look something like this: 然后,“ if”语句将如下所示:

  if(isTrue())
     //Do something
  else
    //Do something else

Well actually,, the if statement only know wether if it is a true or false value. 好吧,实际上,if语句只知道它是真还是假。
But in case of integer, it returnx true if what inside bracket is correct. 但是,如果是整数,则如果内括号正确,则返回true。
Like: 喜欢:

int n=10;  
if(n==10){}

The value inside bracket will return true since it is correct. 括号内的值将返回true,因为它是正确的。 Return false if you change the bracket into n>10 如果将括号更改为n>10则返回false

Different condition goes for String.. 字符串的条件不同。

Yes: 是:

if (x) {
    ...
}

or 要么

if (!x) {
    ...
}

where x is a boolean or anything that returns a boolean. 其中x是布尔值或任何返回布尔值的值。

Heck, go crazy: 哎呀,发疯:

boolean x = false, y = false;

if (x = y = x = !y) {

}

(notice that we're using = not == here - you would probably very rarely want to do something like this, if ever.) (请注意,我们使用=不是==在这里-你可能很少需要做这样的事情,如果有的话)。

Yes. 是。 Generally speaking, the syntax of an if statement is if (cond) { xxx(); } 一般来说, if语句的语法是if (cond) { xxx(); } if (cond) { xxx(); } where cond is any expression which evaluate to a boolean . if (cond) { xxx(); }其中cond任何计算结果为boolean表达式

Which means the following are all valid: 这意味着以下所有内容均有效:

if (true)           // Literal value true
if (m.find())       // m is a Matcher; .find() returns true if match
if (3 <= 3)         // operator
if (a && b)         // a and b are expressions evaluating to a boolean;
                    // && is the logical "and" operator

And so on. 等等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM