ESS多版本 【TA】【ESS】02 条件

TAAAAAAA

天王
管理成员
2024/06/16
279
4
39
1,270
2: Conditions 条件

We now know how we can define variables and give them different values, and we can even do some basic math, but that’s it. In this tutorial, you’ll learn how to check variables to be equal to something, greater than, and so forth. We do this using another set of operators: the relational operators:

到目前为止,我们已经是知道了如何定义一个变量,并且可以利用变量进行一些简单的数学运算了。
现在,我们来学习比较。
比较就是比较一下两个东西之间的关系,和进行数学运算一样的,进行比较运算同样也需要用到对应的比较运算符,比如说有如下几种:

== Equal to 比较是否左边和右边相等
> Greater than 比较是否左边大于右边
< Less than 比较是否左边小于右边
>= Greater than OR Equal to 比较是否左边大于或者和右边相等
<= Less than OR Equal to 比较是否左边小于或者和右边相等
!= NOT Equal to 比较是否左边和右边不相等

We use this with a variable or number/string/int/etc on the left, and what you want to check for on the right.

比较的结果有且只有两种情况,要么是是,也就是true(真),要么是不是,也就是false(假,或者说伪)。
我们来看下面的几个例子:


We can also do this with variables, like so:

我们同样也可以直接用变量来进行比较,请看下面的这几个例子:
 

TAAAAAAA

天王
管理成员
2024/06/16
279
4
39
1,270
If-statements 如果条件语句

We can use these conditions inside if-statements too. These basically check if the given condition is true, and if so, runs the code inside it.

我们可以把比较的结果作为条件放到如果条件语句(if条件句)中使用。
如果条件语句就是,如果怎么怎么样,那么就怎么怎么样,的这样一个句子。
很明显,如果后面的部分就是条件。
我们来看下面的这个例子:


Notable here is the end. Certain keywords, including if, must always be “closed”, so to speak. We do this by writing down end after we write down the keyword. Where we do so and what happens when we do so, depends on the keyword. I’ll touch upon them all briefly when introducing a new keyword. If you forget the end, though, your code will raise the infamous SyntaxError because there are either too many or too few ends.

可以看到,在上面的这个例子中,最后结尾额外多写了一个end(结束)。
使用某些关键词时,结尾必须跟上end,不然就会报错。
虽然使用if不一定需要end,但是,我希望你牢记,有if,就有end,if和end是成对出现的,你可以直接把它称作if-end对。

Because we know 10 > 9 is always true (and can’t-not be true), it basically acts like so, internally:

我们知道,因为10不可能不大于9,所以10 > 9永远都是true,所以上面的例子其实就等价于下面这样:


if-statements test the condition for being true, as mentioned. This means it essentially does (condition) == true. If (condition) is true, it will essentially evaluate as true == true, and so it will always run the code inside.

所以,if条件句,其实就是检查if的条件部分是否是true。
或者,我们也可以说,if条件句其实就是要求if后面跟着的条件必须是true,只有条件是true,才会运行某某代码。

Similarly, if you were to write if false, that will never run, because false == true is false.

类似的,如果你写if false,因为false永远不是true,所以代码永远不会运行。
那你说,如果我就想要当条件是false时,才会运行某某代码的话,我该怎么办?
如果你想要检查的条件是false时才会运行什么什么的话,你就需要使用unless(除非),unless其实就相当于if not(如果不),我们来看下面这个例子:


This naturally also works perfectly fine with variables.

我们来看下面这个使用变量的例子:


One thing new in this example is c = a > b. a > b is, as I said, a condition, and a condition is either true or false. Since the condition a > b is actually false, c will be false.

Since you can do c = true and c = false, that means true and false are also data types. true’s data type is TrueClass, and false’s data type is FalseClass.

因为我们可以直接写c = true 或者c = false,所以我们可以发现,true和false本身也是一种数据类型,true的数据类型是真类(TrueClass),false的数据类型是假类(FalseClass)。
除此之外,还有一种nil(无),它表示“无”、“空”或者“没有”,就是什么也没有,同样的,nil 的数据类型就是空类(NilClass),当你看到 nil 时,它表示没有值,是一个空的状态,比如说,我们可以写c = nil。
但是需要注意,当你写了c = nil时,并不是说没有c这个变量,而是你定义了c这个变量,这个变量的值是nil,只不过nil是一种特殊的数据类型。

在Ruby中,一个变量的值如果不是false或者nil,那它就是真,或者说是“成立的/存在的”。
 

TAAAAAAA

天王
管理成员
2024/06/16
279
4
39
1,270
Keyword Else 其他

If you want to check if a condition is NOT true, you could either do the opposite (for instance, the opposite of a > b is a <= b if you think logically, and the opposite of a == b is a != b) or use the else keyword. See the example below:

通过前面的学习,我们发现如果要运行if条件句中的代码,那么if的条件部分就必须是true。
实际上,为了更加灵活,我们可以使用else(其他),其实就是如果怎么怎么样,就怎么怎么样,如果不怎么怎么样,那么就那么那么样。

我们来看下面的例子:


In this example, we’re checking if age is greater than or equal to 18. If that’s true, it runs the code where # 18+ is. If it’s false, though, it will see if there is an else-condition - which there is, in this case, and run the code inside there, which is # 17 or younger here.

在这个例子中,我们检查age(年龄)是否大于等于18,当这个条件是true时,就运行某一段代码,当这个条件是false时,就运行另一段代码。
简单的说就是根据年龄分成了两种情况,针对不同的情况运行不同的代码,而如果没有else的话,那么就只有if后面的条件是true时会怎么怎么样的这一种情况。
 

TAAAAAAA

天王
管理成员
2024/06/16
279
4
39
1,270
Logical operator NOT 不

You can have multiple conditions inside one conditions too. Let’s say we have condition A, which is true, and condition B, which is false. If we wanted to run a certain piece of code only if A is true and B is false, we could write something like this:

你可以在if条件句中嵌套另一个if条件句。
我们假设我们有a和b这两个变量,并且我们还有一段代码,我想要只有当a是true,并且b是false时,才会运行这段代码,那么我们该如何实现呢?
我们来看下面这段代码实现:


New here is if !b. what ! basically does it invert the variable. If b is false, !b is true, !!b is false, !!!b is true, etc. We use this to check for a variable being false - we hardly ever use b == false, but instead do !b.

这里出现了一个新的东西,那就是!b,就是在b这个变量之前加了一个叹号,注意是英文的叹号。
这个的意思其实就是not(不是),其实就是if !xxx(if not xxx)和unless xxx是等价的(xxx就是某一个条件)。
没有!的时候,我们需要条件是真才会运行代码,而加了!之后,就变成了只有条件是假时,才会运行代码。

实际上,!是真假互相转换的意思,就是说本来是true的,加个!,就变成了false,本来是false的,加个!,就变成了true。
在上面的例子中,因为b是false,所以!b变成了true,所以if后面的整个条件部分是true,所以看上去就好像是加了一个!之后就变成了需要只有条件是假时,才会运行代码。

This also works for false and true themselves; !false #=> true, !true #=> false
 

TAAAAAAA

天王
管理成员
2024/06/16
279
4
39
1,270
Logical operator AND 和

Rather than two separate if-statements, though, we can also combine them with logical operators:

在上面的例子中,我们是在一个if里面又嵌套了一个if。
其实,一个if的条件是可以由多个部分组成的,我们可以通过逻辑运算符来组合不同的部分。
下面是一些逻辑运算符:

&& And 和
|| Or 或
! Not 不

I’ve just showed off all ! Not does, but there’s still && and ||.

之前我们已经解释过!了,下面我们来看一下&&和||。

Since we only want our code to run if A is true and B is false, we can look at the two conditions we have, a and !b, and combine those two. Since we want BOTH to be true, we use AND. This would get us a && !b. What we’ve done now is combine two conditions into one condition which we can use in just one if-statement, like so:

在上面的例子中,我们希望在a是true,并且b是false时,才会运行代码。
这两个条件必须同时满足,所以,我们可以使用&&来连接这两个条件。
用&&连接的条件必须全部是true,if的条件部分才是true。
所以,我们需要这样写a && !b,前面说过了,a是true,b是false,所以!b就是true,因为a和!b同时是true,所以if的条件部分是true,所以才会运行代码。
请看下面的代码示例:


This works exactly the same with numbers, floats, and strings. You’ll just need some other operators:

请继续看下面更多的例子:


In this example, we want to run Code A if age is 10, Code B if age is 11, and Code C otherwise. One part of the condition for Code C is age != 10, which checks if age is not equal to 10. It could still be 11 though, so we check for that too using age != 11. If that’s the case, we can run Code C and we’d have our intended behaviour.

在上面的这个例子当中,我们希望当age是10时,运行Code A,当age是11时,运行Code B,当age既不是10,也不是11时,运行Code C。
 

TAAAAAAA

天王
管理成员
2024/06/16
279
4
39
1,270
Keyword Elsif 其他如果

The code above isn’t great though. We can compress it into fewer lines using the elsif keyword. What we wrote above will check every if-statement, no matter if it has run any previous code or conditions.

上面的代码其实不够简洁,我们可以使用elsif来减少一些代码的行数以及增加代码运行的效率。

If age == 10 is true, for instance, we’re sure age == 11 and age != 10 are going to be false, so we wouldn’t really need to even test those conditions anymore. What this means is that we could skip those conditions, which we can achieve with else. This would get us something like this:

我们使用else来简化代码,请看下面的例子:


The same applies to age == 11 and age != 11, so we can do the same there:

到这里,我想你应该发现了,这里实际上还可以使用else来分情况讨论,我我们来看下面的例子:


We can compress this even further with elsif though:

我们使用elsif再次简化这个例子:


It first tries age == 10. If that’s true, it runs Code A. If it’s false, it tries age == 11. If that’s true, it runs Code B. If it’s false, it’ll run the else-condition if present - which it is here.

elsif(其他如果)其实就是else if(其他如果)的简写。
其实就是,if如果怎么怎么样,接着else在其他情况下if如果怎么怎么样。
其实就是其他情况有很多情况,else就是所有其他的情况,elsif就是其他情况下的某一种特定的情况。
 

TAAAAAAA

天王
管理成员
2024/06/16
279
4
39
1,270
Logical operator OR 或

Aside from NOT and AND, there’s also OR. This takes two conditions and as a whole, will be true if either one of them is true.

说完了“不”和“和”,我们现在来说“或”。
“或”和“和”类似,同样是把多个条件结合变成一个整体,但是和“和”不同的是,“和”需要所有条件都是true,才是true,而“或”只需要里面的某一个条件是true,那么就是true了。

Let’s say we want to know if age == 10 OR age == 11. We’d combine those two conditions into one with ||, which gets us age == 10 || age == 11.

我们来看下面的这个例子:


另外还需要说一点,那就是代码运行的效率的事情。
因为当你用&&或者||组合条件时,那么在程序运行的时候,是从左往右运行,依次检查每一个条件的。
对于&&来说,只要检查到有一个条件是false,那么程序就会退出if的检查了,因为此时已经确定if是false了;
而对于||来说,只要检查到某一个条件是true,那么程序就会退出if的检查了,因为此时已经确定if是true了。
所以在你自己写代码的时候,你可能需要考虑一下什么条件写在前面可能会更好一点。
 

TAAAAAAA

天王
管理成员
2024/06/16
279
4
39
1,270
Operator precedence 运算符的优先级

You should be careful if you use the OR condition in your code anywhere, as it may give you undesired results. Just like in math, certain symbols/operators have precedence over others.
Let’s say we want to run Code A if gender is 0 and age is 10 or 11. We could write something like this:

运算符是有优先级的,就跟数学里的运算符号类似,在数学中,乘除法的优先级是比加减法大的。
我们来看下面的这个例子:


假如说我们想要的是当gender(性别)是0,age为10或者11时,运行代码(0,10或者0,11)。
那么,上面的例子的写法能实现我们的要求吗?

...But this isn’t the behaviour we want. gender == 0 && age == 10 is one part of the condition, and sure, that’s what we want. But the other part is age == 11, and if that’s true, the condition as a whole is also true, regardless of the gender. And we don’t want that.

实际上,上面的代码并不能实现我们的要求,因为&&的优先级比||高,所以程序会把gender == 0 && age == 10当作一个整体,所以上面的代码实际上要求的是当gender是0并且age是10时,或者age是11时(0,10或者11)。

One way we can do resolve this is by adding another if-statement:

我们可以通过下面这样的写法解决我们遇到的问题:


And that’d be that. While valid, we can also use parentheses, just like in math. It changes the precedence to give priority to whatever is in those parentheses and executes that first.

当然,我们也可以跟数学中的做法一样,那就是使用括号,括号中的部分会被认为是一个整体,会有更高的优先级,所以会优先进行计算,请看下面的写法:


This first executes age == 10 || age == 11, and that would be true in this case. It takes the result, true, and combines that with the condition as a whole: gender == 0 && true. So now, the only extra condition it has to check is gender == 0, and if that’s true, the condition as a whole is true, and therefore Code A would be run.

You can add parentheses wherever and whenever you want. (gender == 0 && ((age == 10) || (age == 11))) is the exact same, for instance. Just make sure the amount of opening and closing parentheses match up, or you’ll be greeted with a SyntaxError.

需要注意,你只允许使用小括号来改变优先级,你不能使用中括号和大括号,中括号和大括号是用来做其他的事情的。

本章完。
 
  • Like
反馈: 言十土木

在线成员

论坛统计

主题
543
消息
2,480
成员
3,147
最新成员
执余-