Help for the VSCode editor.
See also Logical Operators in the Go manual.
All the code fragments in this lab are complete mini-programs, so you can paste them into the editor and run them to see the results:
Running the code fragments
- Right click in Explorer pane to create a new file, e.g.
test.go
- Paste the question code snippet into the editor pane
- Open the terminal window and execute
go run test.go
- Re-use your
test.go
file by replacing the content with that of the next question.
-
What would be the output for the following program:
package main import "fmt" func main() { var a, b bool = true, false fmt.Println(a && b) fmt.Println(a || b) }
- false
true - false
false - true
true - true
false
Reveal
false
true&&
is the AND operator.||
is the OR operator. As will be seen later,!
is the NOT operator.Refer logic truth table
a b a AND b a OR b a XOR b NOT a NOT b false false false false false true true true false false true true false true false true false true true true false true true true true false false false It should be noted that Go does not provide a logical XOR (exclusive-OR) operator where many other languages do, however XOR for two
bool
variables can be expressed like this:(a != b)
. - false
-
What would be the output for the following program:
package main import "fmt" func main() { var a, b bool = false, false fmt.Println(a && b) fmt.Println(a || b) }
- false
true - false
false - true
true - true
false
Reveal
false
falseRefer boolean truth table in the answer to Q1 above
- false
-
What would be the output for the following program:
package main import "fmt" func main() { var a, b bool = false, true fmt.Println(!a) fmt.Println(b) }
- false
true - false
false - true
true - true
false
Reveal
true
trueRefer boolean truth table in the answer to Q1 above
In the first
Println
we have applied the logical NOT (!
) operator to the value ofa
. NOT false = true. - false
-
What would be the output for the following program:
package main import "fmt" func main() { var a bool = false result := 10 > 50 fmt.Println(!(a && result)) }
- false
- error
- 1
- true
Reveal
true
Refer boolean truth table in the answer to Q1 above
Let's break it down:
result := 10 > 50
. This createsresult
as abool
variable, holding the value of10 > 50
which isfalse
.- Next, the bracketed expression is evaluated
(a && result)
.a
isfalse
,result
isfalse
therefore this evaluates tofalse
. - Finally, NOT is applied to the result of the previous evaluation, therefore answer is
true
.
-
What would be the output for the following program:
package main import "fmt" func main() { var a bool = true result := 10 > 50 fmt.Println(!(a || result)) }
- false
- error
- 1
- true
Reveal
true
Refer boolean truth table in the answer to Q1 above
Let's break it down:
result := 10 > 50
. This createsresult
as abool
variable, holding the value of10 > 50
which isfalse
.- Next, the bracketed expression is evaluated
(a || result)
.a
istrue
,result
isfalse
therefore this evaluates totrue
. - Finally, NOT is applied to the result of the previous evaluation, therefore answer is
false
.