Golang program that uses fallthrough keyword


Golang is a powerful programming language that has gained a lot of popularity in recent years. It offers many features and tools that make it easier for developers to create efficient and reliable applications. One of the features that sets Golang apart from other programming languages is the fallthrough keyword. In this article, we will take a look at how you can use the fallthrough keyword in your Golang programs.

What is the Fallthrough Keyword in Golang?

In Golang, the fallthrough keyword is used in switch statements to transfer control to the next case statement. When a case statement contains the fallthrough keyword, it causes the code execution to "fall through" to the next case statement, even if the current case has been matched. This allows you to execute multiple case statements with a single switch statement.

How to Use the Fallthrough Keyword in Golang?

Example

Let's take a look at an example of how you can use the fallthrough keyword in your Golang program. Consider the following code −

package main

import "fmt"

func main() {
   var dayOfWeek int = 3

   switch dayOfWeek {
      case 1:
         fmt.Println("Monday")
         fallthrough
      case 2:
         fmt.Println("Tuesday")
         fallthrough
      case 3:
         fmt.Println("Wednesday")
         fallthrough
      case 4:
         fmt.Println("Thursday")
         fallthrough
      case 5:
         fmt.Println("Friday")
         fallthrough
      case 6:
         fmt.Println("Saturday")
      case 7:
         fmt.Println("Sunday")
      default:
         fmt.Println("Invalid Day")
   }
}

Output

Wednesday
Thursday
Friday
Saturday

In this example, we have a switch statement that takes the value of dayOfWeek and matches it against several cases. The first case matches when the value of dayOfWeek is 1, and it prints "Monday" to the console. The next case statement contains the fallthrough keyword, which causes the code execution to fall through to the next case statement, even though the current case has been matched. This allows us to print "Tuesday" to the console as well.

The same process is repeated for the other cases, until we reach case 6. Since case 6 does not contain the fallthrough keyword, the code execution does not fall through to the next case statement, and "Saturday" is printed to the console. Finally, the last case statement matches when the value of dayOfWeek is 7, and "Sunday" is printed to the console.

The default case statement is used to handle any values that do not match the previous case statements.

Benefits of Using the Fallthrough Keyword

Using the fallthrough keyword in your Golang programs can have several benefits. For example, it can help you avoid duplicating code by allowing you to execute multiple case statements with a single switch statement. It can also make your code more readable and easier to maintain by reducing the number of if-else statements and allowing you to handle multiple cases in a single switch statement.

Conclusion

The fallthrough keyword is a powerful feature in Golang that allows you to transfer control to the next case statement in a switch statement. It can be used to execute multiple case statements with a single switch statement, and can help you avoid duplicating code and make your code more readable. By following the examples and guidelines outlined in this article, you can start using the fallthrough keyword in your Golang programs today.

Updated on: 18-Apr-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements