Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Logical Operators | Control Structures
Introduction to GoLang
course content

Contenido del Curso

Introduction to GoLang

Introduction to GoLang

1. Getting Started
2. Data Types
3. Control Structures
4. Functions
5. Arrays and Slices
6. Intro to Structs & Maps

bookLogical Operators

Another class of operators is known as logical operators. Unlike other operators that work with numerical values, logical operators are designed for handling boolean values. There are three logical operators:

OperatorLogic
!NOT
||OR
&&AND

The NOT (!) operator is used to negate (invert) a boolean expression. Here's a simple example that includes an if-statement:

go

index

copy
12345678
package main import "fmt" func main() { if (false) { fmt.Println("Print if True") } }

In the above code, the Print statement won't be executed because the condition is false by default. However, if we include a NOT operator in the expression, it will be negated and, therefore, equivalent to true:

go

index

copy
12345678
package main import "fmt" func main() { if (!false) { fmt.Println("Print if True") } }

We can also use the NOT (!) operator in more complex cases. However, we need to enclose the expressions in parentheses before negating them. For example, if we want to negate 3 > 4, we would write !(3 > 4):

go

index

copy
12345678
package main import "fmt" func main() { if (!(3 > 4)) { fmt.Println("Print if True") } }

The OR (||) operator evaluates two operands and returns true if at least one of the operands is true.

Note

An operand refers to a value or expression used as input for an operator in a statement or expression. For example, in the expression 1 + 2, the values 1 and 2 are operands. In the case of logical operators, an operand is always a boolean expression or value.

Here's an example that illustrates the basic usage of logical operators:

go

index

copy
1234567
package main import "fmt" func main() { fmt.Println(true || false) fmt.Println(false || false) }

The first statement outputs true because one of the operands in the expression true || false is true. In contrast, the second statement outputs false as both operands are false in that expression. Below is the truth table for the logical OR operation:

Note

A truth table is a logical representation that displays all possible combinations of inputs and their corresponding output values.

InputOutput
true || truetrue
true || falsetrue
false || truetrue
false || falsefalse

We can chain logical OR operators to combine multiple operands. In this case, the expression is evaluated from left to right:

go

index

copy
123456
package main import "fmt" func main() { fmt.Println(false || false || true || false) }

The order of evaluation for the above expression is as follows:

Note

By default, all logical expressions are evaluated from left to right.

The logical AND (&&) operator is similar to the OR (||) operator, but it returns true only if both operands have a value of true. The truth table for the logical AND (&&) operation is provided below:

InputOutput
true && truetrue
true && falsefalse
false && truefalse
false && falsefalse

Here's a more complex example that demonstrates a practical use of logical operators. Take a moment to examine the code and understand how it works:

go

index

copy
1234567891011
package main import "fmt" func main() { var x int = 99 if (1 <= x && x <= 10 || 90 <= x && x <= 100) { fmt.Printf("The value %d is in the range 1-10 or 90-100", x) } else { fmt.Printf("The value %d is not in the range 1-10 or 90-100", x) } }

Please note that in the above example, we use multiple operators and values to create a more complex condition. During execution, the expression is evaluated based on the operator's priority. High-priority operators are evaluated first, and it's important to understand the order in which they are processed. Comparison operators have higher priority than logical operators.

Below is a diagram illustrating the priority ranking of logical operators:

The following diagram illustrates how the expression 1 <= x && x <= 10 || 90 <= x && x <= 100 is evaluated:

What of the following expressions can we use in the if-statement to check if a shape is a square based on its `width` and `height` ?

What of the following expressions can we use in the if-statement to check if a shape is a square based on its width and height ?

Selecciona unas respuestas correctas

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 3. Capítulo 4
We're sorry to hear that something went wrong. What happened?
some-alt