A boolean data type represents logical entities. It can have two possible values: true or false.
We use the bool keyword to create boolean-type variables. For example,
Output
We use the relational operators to compare two values or variables. For example,
Here, > is a relational (comparison) operator. It compares whether number1 is greater than number2.
Relational Operators use boolean values (true and false) to return the validity of a relation. It returns:
true if the comparison between operators is correct.
false if the comparison between operators is incorrect.
Here's a list of various relational operators available in Go:
| Operator | Example | Descriptions |
|---|---|---|
== (equal to) | a == b | returns true if a and b are equal |
!= (not equal to) | a != b | returns true if a and b are not equal |
> (greater than) | a > b | returns true if a is greater than b |
< (less than) | a < b | returns true if a is less than b |
>= (greater than or equal to) | a >= b | returns true if a is either greater than or equal to b |
<= (less than or equal to) | a <= b | returns true is a is either less than or equal to b |
Output
Logical operators return either true or false depending upon the conditions.
| Operator | Description | Example |
|---|---|---|
&& (Logical AND) | exp1 && exp2 | Returns true if both expressions exp1 and exp2 are true. |
|| (Logical OR) | exp1 || exp2 | Returns true if at least one of the expressions exp1 or exp2 is true. |
! (Logical NOT) | !exp | Returns true if exp is false, and false if exp is true. |
Output
In programming, expressions that return boolean values: true or false are known as boolean expressions. For example,
Here, number1 > number2 is a boolean expression that returns false.
Boolean expressions are used to create decision-making programs. Suppose we want to create a program that determines whether a person can vote or not.
We can use a boolean expression to check if the age of that person is greater than 18. If true, the person can vote. If false, cannot vote.
We will learn more about these decision-making programs in Go if...else.