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

Perfect answer! That was exactly the answers to my question.

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?

Thanks both of you. Both answers were helpful, by the way

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? :slight_smile: 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.

Your information about “the yoga class” (IF expression THEN statement(s))
is fun, delightful, and helpful.

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 :tulip:

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. :+1:

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