布尔逻辑

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 )可以是truefalse两个值之一。 在编程中使用布尔值进行比较并控制程序的流程。

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 ,均为小写。 值truefalse始终分别使用小写的tf ,因为它们是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 mainimport "fmt"func main() {x := 5y := 8fmt.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)? false

    5( 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 := falsefmt.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:

有两个用于比较值的逻辑运算符。 他们将表达式计算为布尔值,返回truefalse 。 这些运算符是&&||! ,并在以下列表中定义:

  • && (x && y) is the and operator. It is true if both statements are true.

    && ( x && y )是and运算符。 如果两个语句都为真,则为真。

  • || (x || y) is the or operator. It is true if at least one statement is true.

    || ( x || y )是or运算符。 如果至少一个陈述为真,则为真。

  • ! (!x) is the not 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 > 72 < 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.”

子句是遵循条件并指示程序结果的代码块。 也就是说,这是构造的“执行此操作”部分“如果xtrue ,则执行此操作。”

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 {                 // Conditionfmt.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

布尔逻辑

布尔逻辑_了解Go中的布尔逻辑相关推荐

  1. 布尔教育_燕十八 php,布尔教育_燕十八_HTML视频资源课件

    <布尔教育_燕十八_HTML教程>从最基本的概念开始讲起,步步深入,带领大家学习HTML.CSS样式基础知识,了解各种常用标签的意义以及基本用法,后半部分讲解CSS样式代码添加,为后面的案 ...

  2. javascript 布尔_JavaScript布尔说明-如何在JavaScript中使用布尔

    javascript 布尔 布尔型 (Boolean) Booleans are a primitive datatype commonly used in computer programming ...

  3. js布尔类型+数字判断_C ++中的布尔数据类型

    js布尔类型+数字判断 In this article, we'll take a look at the Bool datatype in C++. 在本文中,我们将介绍C ++中的Bool数据类型 ...

  4. java返回一个布尔值_关于java:返回布尔值的方法

    好的,所以我的问题是关于布尔值的回报. 对于我的Comp Sci作业,我必须使用方法制作课程注册程序,其中之一是添加课程方法. 基本上,您在目录中搜索该班级,如果匹配,则将其添加到学生时间表中,并返回 ...

  5. python画椭圆的逻辑_在Python中绘制椭圆轨道(使用numpy,matplotlib)

    作为x的函数,y可以解决该问题 问题是每个有效x有2个y值,而椭圆跨度x范围外没有(或想象)y解 下面是3.5代码,sympy 1.0应该可以打印,list comps可能不会向后兼容到2.x. fr ...

  6. python怎么返回布尔值_尝试从函数返回布尔值

    您需要将"return incomp1==True"替换为"return True".然后像这样调用door1函数"incomp1=door1(inc ...

  7. JAVA的思维逻辑_[Java教程]计算机程序的思维逻辑

    [Java教程]计算机程序的思维逻辑 0 2016-04-15 20:00:04 程序大概是怎么回事 计算机就是个机器,这个机器主要由CPU.内存.硬盘和输入输出设备组成.计算机上跑着操作系统,如Wi ...

  8. php 位 逻辑,php – 在Laravel中放置菜单逻辑的位置?

    Note: this answer was written for Laravel 3 and might or might not work with the most recent Laravel ...

  9. python输出布尔值true_关于python中bool类型的重要细节

    原标题:关于python中bool类型的重要细节 0. 导读 大家好,欢迎来到 Crossin的编程教室 ! 布尔类型是Python中必不可少的一种数据类型.它看起来很简单,仅包括True和False ...

最新文章

  1. 腾讯裁撤中层干部,拥抱年轻人
  2. CTO:再写 if-else,逮着罚款 1000!
  3. 使用LCC网络补偿设计无线功率系统
  4. MATLAB教程目录
  5. python 从字符串中随机选取4个字符_我需要一个Python函数,当给定字符的期望概率时,它将输出4个不同字符的随机字符串...
  6. 每天一道LeetCode-----复制无向图
  7. HttpClient常用的一些常识
  8. 程序员应该如何规划自己的人生与书写履历?
  9. (组合数求模=乘法逆元+快速幂) Problem Makes Problem
  10. 贪婪的送礼者(洛谷P1201题题解,Java语言描述)
  11. 数据库设置为php5.3,php5.3不能连接mssql数据库的解决方法,php5.3mssql_PHP教程
  12. 如何设计一个大数据实时分析平台
  13. Python语言编写有趣练习题!
  14. PIL 图片压缩处理
  15. MySQL药品管理系统设计_药店药品管理系统的设计与实现(SSH,MySQL)(含录像)
  16. 【开发工具】【Java开发工具 IDEAIU-2017.3】【安装和激活】
  17. 如何建立数据指标体系
  18. Mac使用技巧:M1芯片的电脑恢复模式如何开启
  19. 用计算机弹奏体面6,抖音计算器乐谱汇总 抖音计算器按出的音乐乐谱有哪些
  20. 冯诺依曼与哈佛结构的区别

热门文章

  1. 全球太阳能、风能资源空间分布数据集
  2. 新闻学应该掌握哪些计算机技能,哪些人适合学新闻学专业 要掌握哪些技能
  3. 学术小白:如何区分SCI、EI
  4. 【图像分类】实战——使用ResNet实现猫狗分类(pytorch)
  5. HashMap方法tableSizeFor解析
  6. 图像篡改检测C语言,图像篡改检测和定位(二)
  7. 如何知道计算机显卡内存,如何查看显卡显存_如何查看显卡显存占用
  8. 【vscode】MarkDown 插入视频标签
  9. 干得好也要说得好| 这样向领导汇报,让你在职场上如鱼得水
  10. matlab 图像处理 histogram shifting 基于直方图平移的信息隐藏