布尔逻辑_了解Go中的布尔逻辑

布尔逻辑

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 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)? false

    5( x )等于8( y )吗?

  • Is 5 not equal to 8? true

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值