Skip to content

Latest commit

 

History

History
220 lines (156 loc) · 5.47 KB

06-if-else-and-elseif-statements.md

File metadata and controls

220 lines (156 loc) · 5.47 KB

Lab: if-else and else if statements

Take me to the lab!

Help for the VSCode editor.

See also Assignment in the Go manual.

All the code fragments in this lab are complete mini-programs, so you can paste them into the editor and run them to see the results:

All the code fragments in this lab are complete mini-programs, so you can paste them into the editor and run them to see the results:

Running the code fragments
  1. Right click in Explorer pane to create a new file, e.g. test.go
  2. Paste the question code snippet into the editor pane
  3. Open the terminal window and execute go run test.go
  4. Re-use your test.go file by replacing the content with that of the next question.

Questions

  1. What would be the output for the following program:
    package main
    
    import "fmt"
    
    func main() {
            var a, b string = "kolkata", "Kolkata"
            if a == b {
                    fmt.Println("strings are equal")
            } else {
                    fmt.Println("strings are not equal")
            }
            fmt.Println("thank you!")
    }
    • strings are equal
    • strings are equal
      thank you!
    • Error
    • strings are not equal
      thank you!
    Reveal

    strings are not equal
    thank you!

    Strings are not equal because there is a difference in capitalisation, so the if test is false, and the else block is executed.

    There's no error as the syntax is correct and it will compile.

    thank you! is always printed, as it's not inside of an if-else construct.

  2. What would be the output for the following program:
    package main
    
    import "fmt"
    
    func main() {
            var a, b string = "kolkata", "kolkata"
            if a == b {
                    fmt.Println("strings are equal")
            }
            else {
                    fmt.Println("strings are not equal")
            }
            fmt.Println("thank you!")
    }
    • strings are equal
    • strings are equal
      thank you!
    • Error
    • strings are not equal
      thank you!
    Reveal

    Error

    There is a syntax error in this code, so it will not compile. Go is strict about the placement of braces.

            }
            else {

    should be

            } else {
  3. What would be the output for the following program:
    package main
    
    import "fmt"
    
    func main() {
            var a, b string = "foo", "bar"
            if a+b == "foo" {
                    fmt.Println("foo")
            } else if a+b == "bar" {
                    fmt.Println("bar")
            } else if a+b == "foobar" {
                    fmt.Println("foobar")
            } else {
                    fmt.Println("None matched")
            }
            fmt.Println("thank you!")
    }
    • foo
      thank you!
    • bar
      thank you!
    • None matched
      thank you!
    • foobar
      thank you!
    Reveal

    foobar
    thank you!

    1. First, evaluate what is a+b. Addtion of strings concatenates them, so we get foobar
    2. Now look at the if-else if statements. Which one matches foobar? This is the branch the program will follow.

    thank you! is always printed, as it's not inside of an if-else construct.

  4. What would be the output for the following program:
    package main
    
    import "fmt"
    
    func main() {
            var a, b string = "foo", "bar"
            if a+b == "foo" {
                    fmt.Println("foo")
            } else if a+b == "foobar" {
                    fmt.Println("bar")
            } else if a+b == "foobar" {
                    fmt.Println("foobar")
            } else {
                    fmt.Println("None matched")
            }
            fmt.Println("thank you!")
    
    }
    • None matched
      thank you!
    • bar
      thank you!
    • foobar
      thank you!
    • foo
      thank you!
    Reveal

    bar
    thank you!

    This one has a gotcha! Note this bit

        } else if a+b == "foobar" {
                fmt.Println("bar")
        } else if a+b == "foobar" {
                fmt.Println("foobar")
        } ...

    a+b is indeeed foobar and it matches both of the above. Go will choose the first match and ignore any others that match, therefore the output is bar and not foobar

  5. Can we use if block independently, without else-if and else blocks?
    1. No, we need at least one else block.
    2. else-if is optional, but if block would raise error without else block.
    3. Yes, we can write if block without any of the other blocks.
    4. We need at least one if, one else-if and one else block for program to compile successfully.
    • false
      true
    Reveal

    C

    Just like speech. You don't always need to say "else" or "else if" after if.