布尔逻辑
The Boolean data type (bool
) can be one of two values, either true or false. Booleans are used in programming to make comparisons and to control the flow of the program.
布尔数据类型( bool
)可以是true或false两个值之一。 在编程中使用布尔值进行比较并控制程序的流程。
Booleans represent the truth values that are associated with the logic branch of mathematics, which informs algorithms in computer science. Named for the mathematician George Boole, the word Boolean always begins with a capitalized B
.
布尔值表示与数学逻辑分支相关联的真值,该真值用于通知计算机科学中的算法。 以数学家George Boole的名字命名的Boolean总是以大写的B
开头。
The data type in Go for Boolean is bool
, all lowercase. The values true
and false
will always be with a lowercase t
and f
respectively, as they are special values in Go.
Go for Boolean中的数据类型为bool
,均为小写。 值true
和false
始终分别使用小写的t
和f
,因为它们是Go中的特殊值。
This tutorial will cover the basics you’ll need to understand how the bool
data type works, including Boolean comparison, logical operators, and truth tables.
本教程将介绍了解bool
数据类型如何工作所需的基础知识,包括布尔比较,逻辑运算符和真值表。
比较运算符 (Comparison Operators)
In programming, comparison operators are used to compare values and evaluate down to a single Boolean value of either true or false.
在编程中, 比较运算符用于比较值并评估为true或false的单个布尔值。
The table below shows Boolean comparison operators.
下表显示了布尔比较运算符。
Operator | What it means |
---|---|
== | Equal to |
!= | Not equal to |
< | Less than |
> | Greater than |
<= | Less than or equal to |
>= | Greater than or equal to |
操作员 | 这是什么意思 |
---|---|
== | 等于 |
!= | 不等于 |
< | 少于 |
> | 比...更棒 |
<= | 小于或等于 |
> = | 大于或等于 |
To understand how these operators work, let’s assign two integers to two variables in a Go program:
要了解这些运算符的工作方式,让我们在Go程序中为两个变量分配两个整数:
x := 5
y := 8
In this example, since x
has the value of 5
, it is less than y
which has the value of 8
.
在此示例中,由于x
的值为5
,因此它小于y
的值为8
。
Using those two variables and their associated values, let’s go through the operators from the preceding table. In this program, you’ll ask Go to print out whether each comparison operator evaluates to either true or false. To help better understand this output, you’ll have Go also print a string to show you what it’s evaluating:
使用这两个变量及其关联的值,让我们遍历上表中的运算符。 在此程序中,您将要求Go打印出每个比较运算符的评估结果是否为true或false。 为了更好地理解此输出,您将让Go还打印一个字符串以向您显示其评估内容:
package main
import "fmt"
func main() {
x := 5
y := 8
fmt.Println("x == y:", x == y)
fmt.Println("x != y:", x != y)
fmt.Println("x < y:", x < y)
fmt.Println("x > y:", x > y)
fmt.Println("x <= y:", x <= y)
fmt.Println("x >= y:", x >= y)
}
Output
x == y: false
x != y: true
x < y: true
x > y: false
x <= y: true
x >= y: false
Following mathematical logic, Go has evaluated the following from the expressions:
按照数学逻辑,Go从表达式中求出了以下值:
Is 5 (
x
) equal to 8 (y
)? false5(
x
)等于8(y
)吗? 假Is 5 not equal to 8? true
5不等于8吗? 真正
Is 5 less than 8? true
5比8小吗? 真正
Is 5 greater than 8? false
5比8大吗? 假
Is 5 less than or equal to 8? true
5是否小于或等于8? 真正
Is 5 not less than or equal to 8? false
5不小于或等于8吗? 假
Although integers were used here, you could substitute them with float values.
尽管此处使用了整数,但您可以将它们替换为浮点值。
Strings can also be used with Boolean operators. They are case-sensitive unless you use an additional string method.
字符串也可以与布尔运算符一起使用。 除非您使用其他字符串方法,否则它们区分大小写。
You can look at how strings are compared in practice:
您可以查看实际中如何比较字符串:
Sammy := "Sammy"
sammy := "sammy"
fmt.Println("Sammy == sammy: ", Sammy == sammy)
Output
Sammy == sammy: false
The string Sammy
is not equal to the string sammy
, because they are not exactly the same; one starts with an uppercase S
and the other with a lowercase s
. But, if you add another variable that is assigned the value of Sammy
, then they will evaluate to equal:
字符串Sammy
不等于字符串sammy
,因为它们并不完全相同。 一个以大写S
开头,另一个以小写s
开头。 但是,如果添加分配了Sammy
值的另一个变量,则它们的值将等于:
Sammy := "Sammy"
sammy := "sammy"
alsoSammy := "Sammy"
fmt.Println("Sammy == sammy: ", Sammy == sammy)
fmt.Println("Sammy == alsoSammy", Sammy == alsoSammy)
Output
Sammy == sammy: false
Sammy == alsoSammy true
You can also use the other comparison operators including >
and <
to compare two strings. Go will compare these strings lexicographically using the ASCII values of the characters.
您还可以使用其他比较运算符,包括>
和<
来比较两个字符串。 Go将使用字符的ASCII值按字典顺序比较这些字符串。
You can also evaluate Boolean values with comparison operators:
您还可以使用比较运算符评估布尔值:
t := true
f := false
fmt.Println("t != f: ", t != f)
Output
t != f: true
The preceding code block evaluated that true
is not equal to false
.
前面的代码块评估为true
不等于false
。
Note the difference between the two operators =
and ==
.
注意两个运算符=
和==
之间的区别。
x = y // Sets x equal to y
x == y // Evaluates whether x is equal to y
The first =
is the assignment operator, which will set one value equal to another. The second, ==
, is a comparison operator and will evaluate whether two values are equal.
第一个=
是赋值运算符,它将设置一个值等于另一个值。 第二个==
是比较运算符,它将评估两个值是否相等。
逻辑运算符 (Logical Operators)
There are two logical operators that are used to compare values. They evaluate expressions down to Boolean values, returning either true
or false
. These operators are &&
, ||
, and !
, and are defined in the list below:
有两个用于比较值的逻辑运算符。 他们将表达式计算为布尔值,返回true
或false
。 这些运算符是&&
, ||
和!
,并在以下列表中定义:
&& (
x && y
) is theand
operator. It is true if both statements are true.&& (
x && y
)是and
运算符。 如果两个语句都为真,则为真。|| (
x || y
) is theor
operator. It is true if at least one statement is true.|| (
x || y
)是or
运算符。 如果至少一个陈述为真,则为真。! (
!x
) is thenot
operator. It is true only if the statement is false.! (
!x
)是not
运算符。 仅当语句为假时才为真。
Logical operators are typically used to evaluate whether two or more expressions are true or not true. For example, they can be used to determine if the grade is passing and that the student is registered in the course, and if both cases are true, then the student will be assigned a grade in the system. Another example would be to determine whether a user is a valid active customer of an online shop based on whether they have store credit or have made a purchase in the past 6 months.
逻辑运算符通常用于评估两个或多个表达式是否为真。 例如,可以使用它们来确定成绩是否及格,以及该课程是否已注册该学生,如果两种情况都成立,则将在系统中为该学生分配一个成绩。 另一个示例将是根据用户在过去6个月中是否拥有商店信用额或进行过购买来确定用户是否是在线商店的有效活跃客户。
To understand how logical operators work, let’s evaluate three expressions:
为了了解逻辑运算符的工作方式,让我们评估三个表达式:
fmt.Println((9 > 7) && (2 < 4)) // Both original expressions are true
fmt.Println((8 == 8) || (6 != 6)) // One original expression is true
fmt.Println(!(3 <= 1)) // The original expression is false
Output
true
true
true
In the first case, fmt.Println((9 > 7) && (2 < 4))
, both 9 > 7
and 2 < 4
needed to evaluate to true since the and
operator was used.
在第一种情况下, fmt.Println((9 > 7) && (2 < 4))
,因为使用了and
运算符,所以9 > 7
和2 < 4
需要评估为true。
In the second case, fmt.Println((8 == 8) || (6 != 6))
, since 8 == 8
evaluated to true, it did not make a difference that 6 != 6
evaluates to false because the or
operator was used. If you had used the and
operator, this would evaluate to false.
在第二种情况下, fmt.Println((8 == 8) || (6 != 6))
,因为8 == 8
计算为true,所以6 != 6
计算为false没什么区别,因为or
运算符。 如果使用了and
运算符,则该运算结果将为false。
In the third case, fmt.Println(!(3 <= 1))
, the not
operator negates the false value that 3 <=1
returns.
在第三种情况下, fmt.Println(!(3 <= 1))
, not
运算符取反3 <=1
返回的假值。
Let’s substitute floats for integers and aim for false evaluations:
让我们用浮点数代替整数,并进行错误的评估:
fmt.Println((-0.2 > 1.4) && (0.8 < 3.1)) // One original expression is false
fmt.Println((7.5 == 8.9) || (9.2 != 9.2)) // Both original expressions are false
fmt.Println(!(-5.7 <= 0.3)) // The original expression is true
In this example:
在此示例中:
and
must have at least one false expression evaluate to false.and
必须至少有一个假表达式评估为假。or
must have both expressions evaluate to false.or
必须使两个表达式都为假。!
must have its inner expression be true for the new expression to evaluate to false.!
必须使内部表达式为true,新表达式的值为false。
If these results seem unclear to you, go through some truth tables for further clarification.
如果您不清楚这些结果,请通过一些真值表进行进一步说明。
You can also write compound statements using &&
, ||
, and !
:
您也可以使用&&
, ||
编写复合语句。 和!
:
!((-0.2 > 1.4) && ((0.8 < 3.1) || (0.1 == 0.1)))
Take a look at the inner-most expression first: (0.8 < 3.1) || (0.1 == 0.1)
. This expression evaluates to true
because both mathematical statements are true
.
首先看一下最里面的表达式: (0.8 < 3.1) || (0.1 == 0.1)
(0.8 < 3.1) || (0.1 == 0.1)
。 该表达式的计算结果为true
因为两个数学陈述均为true
。
Next, Go takes the returned value true
and combines it with the next inner expression: (-0.2 > 1.4) && (true)
. This example returns false
because the mathematical statement -0.2 > 1.4
is false, and (false
) and (true
) returns false
.
接下来,Go将返回值true
并将其与下一个内部表达式组合: (-0.2 > 1.4) && (true)
。 此示例返回false
因为数学表达式-0.2 > 1.4
为false,并且( false
)和( true
)返回false
。
Finally, we have the outer expression: !(false)
, which evaluates to true
, so the final returned value if we print this statement out is:
最后,我们有外部表达式: !(false)
,其计算结果为true
,因此,如果我们打印出此语句,最终返回的值是:
Output
true
The logical operators &&
, ||
, and !
evaluate expressions and return Boolean values.
逻辑运算符&&
, ||
和!
计算表达式并返回布尔值。
真相表 (Truth Tables)
There is a lot to learn about the logic branch of mathematics, but you can selectively learn some of it to improve your algorithmic thinking when programming.
关于数学的逻辑分支,有很多知识要学习,但是您可以有选择地学习其中的一些知识,以提高编程时的算法思维能力。
The following are truth tables for the comparison operator ==
, and each of the logic operators &&
, ||
and !
. While you may be able to reason them out, it can also be helpful to memorize them as that can make your programming decision-making process quicker.
以下是比较运算符==
以及每个逻辑运算符&&
, ||
的真值表。 和!
。 尽管您可以推理出它们,但记住它们也可能会有所帮助,因为这可以使您的编程决策过程更快。
==
(等于)真值表 (==
(equal) Truth Table)
x | == | y | Returns |
---|---|---|---|
true | == | true | true |
true | == | false | false |
false | == | true | false |
false | == | false | true |
X | == | ÿ | 退货 |
---|---|---|---|
真正 | == | 真正 | 真正 |
真正 | == | 假 | 假 |
假 | == | 真正 | 假 |
假 | == | 假 | 真正 |
&&
(和)真值表 (&&
(and) Truth Table)
x | and | y | Returns |
---|---|---|---|
true | and | true | true |
true | and | false | false |
false | and | true | false |
false | and | false | false |
X | 和 | ÿ | 退货 |
---|---|---|---|
真正 | 和 | 真正 | 真正 |
真正 | 和 | 假 | 假 |
假 | 和 | 真正 | 假 |
假 | 和 | 假 | 假 |
||
(或)真值表 (||
(or) Truth Table)
x | or | y | Returns |
---|---|---|---|
true | or | true | true |
true | or | false | true |
false | or | true | true |
false | or | false | false |
X | 要么 | ÿ | 退货 |
---|---|---|---|
真正 | 要么 | 真正 | 真正 |
真正 | 要么 | 假 | 真正 |
假 | 要么 | 真正 | 真正 |
假 | 要么 | 假 | 假 |
!
(不是)真值表 (!
(not) Truth Table)
not | x | Returns |
---|---|---|
not | true | false |
not | false | true |
不 | X | 退货 |
---|---|---|
不 | 真正 | 假 |
不 | 假 | 真正 |
Truth tables are common mathematical tables used in logic, and are useful to keep in mind when constructing algorithms (instructions) in computer programming.
真值表是逻辑中常用的数学表,在构造计算机程序中的算法(指令)时要牢记这些信息。
使用布尔运算符进行流量控制 (Using Boolean Operators for Flow Control)
To control the stream and outcomes of a program in the form of flow control statements, you can use a condition followed by a clause.
要以流控制语句的形式控制程序的流和结果,可以使用条件,后跟一个子句 。
A condition evaluates down to a Boolean value of true or false, presenting a point where a decision is made in the program. That is, a condition would tell you if something evaluates to true or false.
条件的求值结果为布尔值true或false,表示在程序中做出决定的点。 也就是说,条件会告诉您某物的值为真还是假。
The clause is the block of code that follows the condition and dictates the outcome of the program. That is, it is the “do this” part of the construction “If x
is true
, then do this.”
该子句是遵循条件并指示程序结果的代码块。 也就是说,这是构造的“执行此操作”部分“如果x
为true
,则执行此操作。”
The code block below shows an example of comparison operators working in tandem with conditional statements to control the flow of a Go program:
下面的代码块显示了一个比较运算符与条件语句协同工作以控制Go程序流程的示例:
if grade >= 65 { // Condition
fmt.Println("Passing grade") // Clause
} else {
fmt.Println("Failing grade")
}
This program will evaluate whether each student’s grade is passing or failing. In the case of a student with a grade of 83
, the first statement will evaluate to true
, and the print statement of Passing grade
will be triggered. In the case of a student with a grade of 59
, the first statement will evaluate to false
, so the program will move on to execute the print statement tied to the else expression: Failing grade
.
该计划将评估每个学生的成绩是及格还是不及格。 如果学生的分数为83
,则第一个陈述将评估为true
,并且将触发Passing grade
的打印陈述。 对于年级为59
的学生,第一个语句的评估结果为false
,因此程序将继续执行与else表达式相关联的print语句: Failing grade
。
Boolean operators present conditions that can be used to decide the eventual outcome of a program through flow control statements.
布尔运算符提供了可用于通过流控制语句确定程序最终结果的条件。
结论 (Conclusion)
This tutorial went through comparison and logical operators belonging to the Boolean type, as well as truth tables and using Booleans for program flow control.
本教程介绍了属于布尔类型的比较运算符和逻辑运算符,以及真值表,并使用布尔值进行程序流控制。
翻译自: https://www.digitalocean.com/community/tutorials/understanding-boolean-logic-in-go
布尔逻辑