Control Flow

Question Click to View Answer

What does the following code print to the console?

if 2 == 2 {
    fmt.Println("ice cream is tasty!")
}

"ice cream is tasty!" is printed.

The code block in an if statement is executed if the boolean condition associated with the if statement evaluates to true. In this example, the boolean condition (2 == 2) evaluates to true and the code block is executed.

What does the following code print to the console?

if "cat" == "dog" {
    fmt.Println("prrrr")
} else {
    fmt.Println("ruff")
}

"ruff" is printed.

The boolean condition ("cat" == "dog") evaluates to false, so the code block associated with the if is skipped and the code block associated with the else is executed.

What does the following code print to the console?

if 5 > 10 {
    fmt.Println("fan")
} else if 8 != 9 {
    fmt.Println("glass")
} else {
    fmt.Println("cream")
}

"glass" is printed.

If the boolean condition associated with the if is false and the boolean condition associated with the else if is true, then the code associated with the else if will be executed.

What does the following code print to the console?

name := "maria"
if name == "melissa" {
    fmt.Println("usa")
} else if name == "mary" {
    fmt.Println("ireland")
} else {
    fmt.Println("colombia")
}

"colombia" is printed.

If the boolean conditions associated with if and else if are false, then the code associated with else will be executed.

What does the following code print to the console?

if 88 > 100 {
    fmt.Println("cardio")
}

Nothing is printed.

The boolean condition (88 > 100) associated with the if statement evaluates to false, so the code block associated with the if statement is not executed. There are not else if or else statements, so nothing gets printed.

What does the following code print to the console?

hairColor := "blue"
if 3 > 2 {
    if hairColor == "black" {
        fmt.Println("You rock!")
    } else {
        fmt.Println("Boring")
    }
}

"Boring" is printed.

This is an example of a nested if statement. The boolean condition for the exterior if statment (3 > 2) evaluates to true so we enter the inner if statement.

The boolean condition for the inner if statement (hairColor == "black") evaluates to false, so the code block associated with the inner else statement is executed.

What does the following code print to the console?

if true {
    fmt.Println(101)
} else {
    fmt.Println(202)
}

101 is printed to the console.

In previous examples, we've used boolean expressions that evaluate to true or false, but we can also use true or false directly.

What does the following code print to the console?

if false {
    fmt.Println("Nissan")
} else if true {
    fmt.Println("Ford")
} else if true {
    fmt.Println("BMW")
} else {
    fmt.Println("Audi")
}

"Ford" is printed.

The first else if that is true will be executed when multiple else if statements are true.

What does the following code print to the console?

var likesHugs bool
if likesHugs {
    fmt.Println("Hug me!")
} else {
    fmt.Println("High 5 is better")
}

High 5 is better is printed to the console.

The likesHugs variable is declared to be a boolean, but is not assigned to a value. Variables declared without an explicit initial value are given their zero value. The zero value is false for the boolean type.

What does the following code print to the console?

i := 2
switch i {
case 0:
    fmt.Println("Zero")
case 1:
    fmt.Println("One")
case 2:
    fmt.Println("Two")
case 3:
    fmt.Println("Three")
default:
    fmt.Println("Whatever")
}

Two is printed to the console.

switch statements are a more elegant way to write code with multiple else if conditions. We could also use the following code, but it requires more typing.

i := 2
if i == 0 {
    fmt.Println("Zero")
} else if i == 1 {
    fmt.Println("One")
} else if i == 2 {
    fmt.Println("Two")
} else if i == 3 {
    fmt.Println("Three")
} else {
    fmt.Println("Whatever")
}