Skip to content
Related Articles

Related Articles

Golang program that uses fallthrough keyword
  • Last Updated : 04 May, 2020

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
Recommended Articles
Page :