# I'm confused by https://play.golang.org/p/25swYz6BEtu

https://play.golang.org/p/25swYz6BEtu

Here we have a number of if statements, correct?

Are all these statements related to 2 == 2? And if so, how is that indicated?

If statements execute if the expression evaluates to true

``````//true evaluates to true
if true {
fmt.Println("001")
}

//false is false
if false {
fmt.Println("002")
}

//not true is false
if !true {
fmt.Println("003")
}

//not false is true
if !false {
fmt.Println("004")
}

// 2 == 2 is true
if 2 == 2 {
fmt.Println("005")
}
``````
1 Like

Yes. 5 of them, but I do not think that counting is your problem

What do you mean by this? You have 5 distinct statements, each of them prints some different string in its body, ehich is only evaluated if the condition evaluates to `true`.

1 Like

But wait a minute.

For example, the first statement, if trueâ€¦
does this at all refer to 2==2?

And so for the other statements?

Or are all the statements totally separate?

`2==2` is true. But there is no reference from one of the statements to another. They are totally distinct. You can delete any of them and the remaining will do the same as before.

Curtisâ€™s and Norbertâ€™s answers are all perfectly good, and Iâ€™m adding this to make sure you have a good basic understanding.

Your question is about if/then constructs, which exist in most general-purpose programming languages. There is a page on Wikipedia about them if you really want to dig in and study: https://en.wikipedia.org/wiki/Conditional_(computer_programming)

The way it works is like this. There is a language construct (part of the syntax and grammar) that follows this pattern:

IF expression THEN statement(s)

The statements are executed only if the expression is true. This is â€śtrueâ€ť as in Boolean logic, which is (put simply) mathematics about 1s and 0s, or true and false, or whatever words you want to put on the two binary digital states that computers work with. (Itâ€™s actually high and low voltage levels in the computerâ€™s electronic circuits.)

Hereâ€™s an example that doesnâ€™t involve computers at all. Suppose you regularly go to yoga class on Tuesdays. You can say, â€śOn Tuesday, I go to yoga class.â€ť Or on a Tuesday, you can say, â€śToday is Tuesday, so Iâ€™m going to my yoga class.â€ť This is just a humanized form of conditional construct. Put into Go, it might look like this:

``````// Give integer values to the days of the week
// sunday will be zero, monday will be 1, tuesday will be 2, etc.

const (
sunday = iota
monday
tuesday
wednesday
thursday
friday
saturday
)

var day int

// ...

if day == tuesday { yoga_class() }
``````

The expression in this case is â€ś`day == tuesday`â€ť and the statement is â€ś`yoga_class()`â€ť, a function call. When `day` is equal to `2`, it will be equal to `tuesday`, which is always `2` (because itâ€™s defined that way as a `const`). `day == tuesday` is the same as `2 == 2`, which evaluates as `true` (because 2 equals 2), so `yoga_class()` will be called. When `day` is any other value, `yoga_class()` will not be called because `day` is not equal to 2, so the expression evaluates to `false`. Any number of statements can be put inside the braces.

About the expression, in Go the expression must evaluate to a bool type, so it can be only true or false. You can use the following operators to yield bool values:

``````==   equal to (as opposed to = and :=, which are for setting values, not comparing them)
!=    not equal to
<    less than
>    greater than
<=    less than or equal to
>=    greater than or equal to
``````

Using those, you can combine other values, which are not `bool`. For example,

`if i + 32 < x { p = 6 }`

sets `p` to `6` if `i + 32` is less than the value of `x`. `i` and `x` are both non-`bool` types (for example, integers), and doing the comparison with `<` yields a `bool` value, either `true` or `false` which satisfies the requirement that the expression is of type bool.

You can also use the `&&` (logical and) and `||` (logical or) operators to combine `bool` values. A quick example:

``````if day == tuesday && phase_of_moon != full { yoga_class() }
``````

This means, â€śGo to yoga class if itâ€™s Tuesday and itâ€™s not a full moon.â€ť

If you make a mistake and use an integer type instead of a `bool` in the expression, youâ€™ll get an error from the compiler that looks like this:

./boo.go:9:2: non-bool var_name (type int) used as if condition

Donâ€™t you just love reading Goâ€™s error messages? I hope this helps!

1 Like

So â€śif trueâ€ť is not evaluating 2 == 2 ? And if not, then what is it referring to? If what is true?

How should `if true {â€¦}` be able to evaluate `2 == 2`? I canâ€™t see `2 == 2` in thereâ€¦

`2 == 2` gets evaluated in `if 2 == 2 {â€¦}`.

Ah. So I donâ€™t get the statement â€śIf trueâ€¦â€ť. What is it referring to?

To `true`, the boolean literal.

I began reading the Wikipedia article you mentioned. Helping me to get oriented.

At this point, Goâ€™s error messages are a bit confusing, but I do understand some of them.

Yes! It helps. Again, I promise Iâ€™ll get there

1 Like

By the way, my last reply was to jayts

I was joking about the Go error message because Goâ€™s error messages are some of the weirdest Iâ€™ve ever seen from any programming language! I like to laugh at them. So donâ€™t worry if you find them enigmatic.

You do get the line number of the error, and then hopefully, rest of the words in the message associate somehow with what you did wrong. Itâ€™s not easy to write a compiler that gives really clear and helpful error messages!

1 Like

Thanks!

It would be related to 2 == 2 if there were else statements in between. Here is the example but of course it always prints 001 because only first one is true.

``````func main() {
if true {
fmt.Println("001")
} else

if false {
fmt.Println("002")
} else

if !true {
fmt.Println("003")
} else

if !false {
fmt.Println("004")
} else

if 2 == 2 {
fmt.Println("005")
}
}``````

Interesting. Thanks

When Lazer liked Curtisâ€™s answer, it inspired me to study it again, and I understand it better now. I think all the answers combined helped me with this. Thanks everybody.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.