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.
package main
import "fmt"
func main() {
day := "Tue"
switch {
case day == "Mon" :
fmt.Println( "Monday" )
fallthrough
case day == "Tue" :
fmt.Println( "Tuesday" )
fallthrough
case day == "Wed" :
fmt.Println( "Wednesday" )
}
}
|
Output :
Tuesday
Wednesday
Example 2:
package main
import "fmt"
func main() {
gfg := "Geek"
switch {
case gfg == "Geek" :
fmt.Println( "Geek" )
fallthrough
case gfg == "For" :
fmt.Println( "For" )
fallthrough
case gfg == "Geeks" :
fmt.Println( "Geeks" )
}
}
|
Output :
Geek
For
Geeks
Last Updated :
04 May, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...