Programming/Easy Go

Operators(Arithmetic, Comparison, Logical)

EveryDayIsNewDay 2024. 2. 7. 15:41

Operators are symbols or keywords used in programming and mathematics to perform operations on variables and values.

 

1. Arithmetic Operators

 

Arithmetic operators are used to perform common mathematical operations

 

Operator Description
+ Adds two operands
- Subtracts second operand from the first
* Multiplies both operands
/ Divides the numerator by the denominator.
% Modulus operator; gives the remainder after an integer division.
++ Increment operator. It increases the integer value by one.
-- Decrement operator. It decreases the integer value by one.

 

package main

import "fmt"

func main() {

	var a int = 21
	var b int = 10
	var c int

	c = a + b
	fmt.Printf("%d + %d = %d\n", a, b, c)

	c = a - b
	fmt.Printf("%d - %d = %d\n", a, b, c)

	c = a * b
	fmt.Printf("%d * %d = %d\n", a, b, c)

	c = a / b
	fmt.Printf("%d / %d = %d\n", a, b, c)

	c = a % b
	fmt.Printf("%d %% %d = %d\n", a, b, c)

	a++
	fmt.Printf("%d\n", a)

	b--
	fmt.Printf("%d\n", b)

}
$ go run arithmetic_op.go
21 + 10 = 31
21 - 10 = 11
21 * 10 = 210
21 / 10 = 2
21 % 10 = 1
22
9

 

  • 26, 29: ++’ and ‘--’ are called unary operators and have only one operand. In ‘C’ language, unary operators can appear before or after variables, but in ‘Go’, they can only appear after them. 'a++' is equivalent to 'a = a + 1

 

1) Shortening of arithmetic operators (compound assignment operators)

 

Let's look at the following example:

 

package main

import "fmt"

func main() {

	var a int = 20

	fmt.Printf("%d\n", a)
	a = a + 10
	fmt.Printf("%d\n", a)

}
$ go run compound_op.go
20
30

 

If you look at the code above, you can see that the same variable a is used on the left and right sides of the assignment operator (=) (a = a + 10). It was mentioned earlier that the right side of the assignment operator (=) represents the value and the left side represents the storage location.

The meaning of the code can be represented in a diagram as follows:

 

l-value / r-value

 

The variable to the right of the assignment operator (=) is called an r-value. In other words, it is said to be the right value or real value. In other words, the variable on the right represents the actual value that the variable has. In the end, “a + 10” means adding the actual value of the ‘a’ variable, 20, and 10.

The assignment operator (=) is responsible for putting the value on the right into the variable on the left. In the end, the value of 30 calculated on the right is stored in the left value or location.

 

In the ‘Go’ language, there are many cases where some calculation is performed on a variable and then put back in the same place. To reduce the amount of coding, it can be abbreviated as follows.

 

compound assignment operators Description
a += 10 a = a + 10
a -= 10 a = a - 10
a *= 10 a = a * 10
a /= 10 a = a / 10
a %= 10 a = a % 10

 

One thing to note about compound operators is that they can only be used when the variables on the left and right sides are the same. Let's remember this

 

 

2. Comparison Operator

We compare a lot in real life. For example, “If your test score is 90 points or higher, dad buys you a toy, otherwise, have to solve more workbooks”, you need to compare the test scores with 90 points that serve as the standard for judgment.

Operators that make comparisons like this are called “comparison operators (or relational operators).

 

Comparison Operator Description
X > Y True if X is greater than Y
X >= Y True if X is greater than or equal to Y
X < Y True if X is less than Y
X<= Y True if X is less than or equal to Y
X == Y True if X is equal to Y
'=' is used as an assign operator (meaning putting the right value to left variable) so use two consecutive '='s
X != Y True if X is not equal to Y

 

Comparison operators return the result of the operation as a Boolean type: true or false.

 

package main

import "fmt"

func main() {

	var a int = 10
	var b int = 20

	fmt.Println(a > b)
	fmt.Println(a < b)
	fmt.Println(a == b)
	fmt.Println(a != b)

	b = 10
	fmt.Println(a > b)
	fmt.Println(a >= b)
	fmt.Println(a == b)
	fmt.Println(a != b)

}
$ go run comparison_op.go
false
true
false
true
false
true
true
false

 

  • 10: 10 is less than 20 so it is false

  • 11: 10 is less than 20 so it is true

  • 12: False because 10 and 20 are not equal

  • 13: True because 10 and 20 are not equal

  • 15: Change the value of b from 20 to 10

  • 17: True because it is greater than or equal to 10

 

 

3. Logical Operator

 

The comparison operator we learned earlier is a binary operator, which compares the values on the left and right of the operator, right? However, in real life, it is common for us to compare not just two things, but many things.

 

For example, let’s say there is a condition that says, “If your math score is 90 or higher and your English score is 90 or higher, we will buy you a toy.” You can think of this sentence as “Math score >= 90 and English score >= 90.” In other words, since there are two objects that need to be compared, the word 'and' in the middle (more accurately, a 'connective adverb' is used when connecting words, phrases, clauses, sentences, etc. in parallel. In middle school, I went to Korean grammar class. Since this is what you will learn, let's just say it's a word) goes in and connects the two sentences under the same conditions. To compare two or more conditions like this, we use logical operators such as ‘and(&&)’, ‘or(||)’, and ‘not(!)’

 

1) Conjunction(&&) operator

 

For example, let’s say there is a comparison: “If your math score is 90 points or higher and your English score is 90 points or higher.” In this case, not only your math score but also your English score must be 90 or higher to satisfy the conditions. If your math score is 90 points or higher, but your English score is 89 points, you may not be meeting the requirements. In this way, the conjunction operator returns true only if both conditions are true, and returns false if even one condition is not satisfied.

 

2) Disjunction(||) operator

 

For example, let’s say there is a comparison: “If your math score is 90 points or higher or your English score is 90 points or higher.” In this case, if your math score is 90 points or higher or your English score is 90 points or higher (that is, if only one of the two is satisfied), the condition will be satisfied. If your math score is 90 points or higher, but your English score is 89 points, that would still be a case of satisfying the conditions. In this way, if either condition on both sides of the disjunction operator is true, the result is true, and if neither condition is satisfied, it returns false.

 

3) Negation(!) operator


The negation operator compares one condition, and the opposite (logical negation) of “Tom is good at studying” is expressed as “Tom is bad at studying.” In other words, it is an operator that returns a false result if the comparison object is true, and returns true if the comparison object is false.

 

To summarize, it is as follows.

 

Logical Operator Description
X && Y Ture if X is true and Y is true
X || Y True if at least one of X or Y is true
!X False if X is true, True if X is false

 

Look at the following examples:

 

package main

import "fmt"

func main() {

	var korean int = 90
	var math int = 85
	var tom bool = true

	fmt.Println(korean >= 90 && math >= 90)
	fmt.Println(korean >= 90 || math >= 90)
	fmt.Println(!tom)

}
$ go run logical_op.go
false
true
false

 

  • 11: The value of Korean is true, but the value of math is false, so the conjunction(&&) operation results in an overall false result.

  • 12: The value of Korean is true, but the value of mathematics is false. But disjunction(||) operation result is an overall true result.

  • 13: The opposite of tom's value is false.

 

4. Miscellaneous Operators

 

There are several other important operators, but they are difficult to explain at this time, so I will explain them in another chapter.

'Programming > Easy Go' 카테고리의 다른 글

Utilizing Loops: for  (0) 2025.01.10
Using Conditional Statements: if / switch  (0) 2025.01.09
Print to Screen  (1) 2024.02.07
Variables and Constants  (0) 2023.09.08
Setup Go development environments  (0) 2023.09.06