4.8 KiB
What does "control flow" mean?
- Changing the top-to-bottom execution of a program
- Changing the left-to-right execution of a program
- Deciding which statements are executed CORRECT
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.
How can you simplify the condition expression of this if statement?
if (mood == "perfect") {
// this code is not important
}
if {mood == "perfect"}
if [mood == "perfect"]
if mood = "perfect"
if mood == "perfect"
CORRECT
1, 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.
The following code doesn't work. How you can fix it?
package main
import "fmt"
func main() {
// this program prints "cool"
// when the mood is "happy"
mood := "happy"
if "happy" {
fmt.Println("cool")
}
}
- Just wrap the "happy" inside parentheses.
- You need to compare mood with "happy". Like this:
if mood == "happy" { ... }
CORRECT - Just directly use
mood
instead ofhappy
. Like this:if mood { ... }
- This should work! This is a tricky question.
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 :)
How can you simplify the following code? You only need to change the condition expression, but how?
package main
import "fmt"
func main() {
happy := true
if happy == true {
fmt.Println("cool!")
}
}
happy != false
!happy == false
happy
CORRECT!happy == true
1, 2: Right! But you can do it better.
3: Perfect! You don't need to compare the value to
true
.happy
is already true, so it'll print "cool!".4: That won't print anything.
!happy
yields false.
How can you simplify the following code? You only need to change the condition expression, but how?
package main
import "fmt"
func main() {
happy := false
if happy == !true {
fmt.Println("why not?")
}
}
- Easy! Like this:
happy != true
!happy
CORRECThappy == false
!happy == false
1, 3: Right! But you can do it better.
2: Perfect! You don't need to compare the value to
false
or to!true
(which is false).!happy
already returns true, because it's false at the beginning.4: That won't print anything.
happy
will be true.
This code contains an error. How to fix it?
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?")
}
}
- Remove one of the else branches. CORRECT
- Move the else if as the last branch.
- It repeats "why not?" several times.
- Remove the
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.
What's the problem with the following code?
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?")
}
}
- It declares the energic variable unnecessarily.
- You can't use more than one else if branch.
- It will never run the last else if branch. CORRECT
- There's no else branch.
2: Well, actually you can.
3: Right.
happy
can 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.
How can you simplify the following code?
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?")
}
}
- Change
else if
's condition to:!happy
. - Move the else branch before else if.
- Remove the else branch.
- Remove the else if branch. CORRECT
1, 3: Close! But, you can do it even better.
2: You can't:
else
branch should be the last branch.4: Cool. That's not necessary because
else
branch already handless "unhappy" situation. It's simpler because it doesn't have a condition.