11-if/questions/3-if.md
1, 2: You can't change that.
3: That's right. Control flow allows us to decide which parts of a program is to be run depending on condition values such as true or false.
if (mood == "perfect") {
// this code is not important
}
if {mood == "perfect"}if [mood == "perfect"]if mood = "perfect"if mood == "perfect" CORRECT1, 2: That's a syntax error. Try again.
3:
=is the assignment operator. It cannot be used as a condition.4: That's right. In Go, you don't need to use the parentheses.
package main
import "fmt"
func main() {
// this program prints "cool"
// when the mood is "happy"
mood := "happy"
if "happy" {
fmt.Println("cool")
}
}
if mood == "happy" { ... } CORRECTmood instead of happy. Like this: if mood { ... }1: That won't change anything. Go adds the parentheses automatically behind the scenes for every if statement.
2: Yep. In Go, condition expressions always yield a bool value. Using a comparison operator will yield a bool value. So, it will work.
4: No, it's not :)
package main
import "fmt"
func main() {
happy := true
if happy == true {
fmt.Println("cool!")
}
}
happy != false!happy == falsehappy CORRECT!happy == true1, 2: Right! But you can do it better.
3: Perfect! You don't need to compare the value to
true.happyis already true, so it'll print "cool!".4: That won't print anything.
!happyyields false.
package main
import "fmt"
func main() {
happy := false
if happy == !true {
fmt.Println("why not?")
}
}
happy != true!happy CORRECThappy == false!happy == false1, 3: Right! But you can do it better.
2: Perfect! You don't need to compare the value to
falseor to!true(which is false).!happyalready returns true, because it's false at the beginning.4: That won't print anything.
happywill be true.
package main
import "fmt"
func main() {
happy := false
if happy {
fmt.Println("cool!")
} else if !happy {
fmt.Println("why not?")
} else {
fmt.Println("why not?")
} else {
fmt.Println("why not?")
}
}
else if branch.1: Right. There can be only one else branch.
2: If there's an else branch, you can't move else if branch as the last branch.
3, 4: So? :) That's not the cause of the problem.
package main
import "fmt"
func main() {
happy := true
energic := happy
if happy {
fmt.Println("cool!")
} else if !happy {
fmt.Println("why not?")
} else if energic {
fmt.Println("working out?")
}
}
2: Well, actually you can.
3: Right.
happycan only be either true or false. That means, it will always execute the first two branches, but it will never execute the else if branch.4: It doesn't have to be. Else branch is optional.
package main
import "fmt"
func main() {
happy := false
if happy {
fmt.Println("cool!")
} else if happy != true {
fmt.Println("why not?")
} else {
fmt.Println("why not?")
}
}
else if's condition to: !happy.1, 3: Close! But, you can do it even better.
2: You can't:
elsebranch should be the last branch.4: Cool. That's not necessary because
elsebranch already handless "unhappy" situation. It's simpler because it doesn't have a condition.