Golang program that uses fallthrough keyword

With the help of fallthrough statement, we can use to transfer the program control just after the statement is executed in the switch cases even if the expression does not match. Normally, control will come out of the statement of switch just after the execution of first line after match. Don’t put the fallthrough in the last statement of switch case.

Example 1: In this example we can see that by using switch cases with fallthrough and assuming variable as a string type we can make use of switch cases.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Golang program to show the
// uses of fallthrough keyword
package main
  
// Here "fmt" is formatted IO which
// is same as C’s printf and scanf.
import "fmt"
  
// Main function
func main() {
    day := "Tue"
  
    // Use switch on the day variable.
    switch {
    case day == "Mon":
        fmt.Println("Monday")
        fallthrough
    case day == "Tue":
        fmt.Println("Tuesday")
        fallthrough
    case day == "Wed":
        fmt.Println("Wednesday")
    }
}

chevron_right


Output :

Tuesday
Wednesday

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Golang program to show the
// uses of fallthrough keyword
package main
  
// Here "fmt" is formatted IO which
// is same as C’s printf and scanf.
import "fmt"
  
// Main function
func main() {
    gfg := "Geek"
  
    // Use switch on the day variable.
    switch {
    case gfg == "Geek":
        fmt.Println("Geek")
        fallthrough
    case gfg == "For":
        fmt.Println("For")
        fallthrough
    case gfg == "Geeks":
        fmt.Println("Geeks")
    }
}

chevron_right


Output :

Geek
For
Geeks



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.