Algorithm
Creating a flowchart for a switch statement involves representing the decision-making process that occurs when a certain variable or expression is evaluated against multiple possible values. Below is a basic flowchart template for a switch statement. Please note that the exact structure and symbols used may vary based on the conventions you're following:
-
Start:
- This is the starting point of your flowchart.
-
Initialize Variable:
- Initialize the variable that will be evaluated in the switch statement.
-
Decision Node (Switch):
- Create a diamond-shaped decision node labeled "Switch."
- The incoming arrow should be labeled with the variable being evaluated.
-
Cases:
- For each possible value of the variable, create a rectangular process node.
- Label each node with the corresponding case value.
- Connect each case node to the decision node with arrows.
-
Case Logic:
- Inside each case node, depict the logic or actions to be taken for that specific case.
-
Default Case:
- If there is a default case, create a rectangular process node labeled "Default."
- Connect it to the decision node.
-
End:
- Create an oval-shaped end node to represent the end of the flowchart.
-
Arrows:
- Use arrows to connect the nodes and indicate the flow of the process.
- Label the arrows with any relevant conditions or values.
Code Examples
#1 switch case in Golang Programming
Code -
Golang Programming
// Program to print the day of the week using switch case
package main
import "fmt"
func main() {
dayOfWeek := 3
switch dayOfWeek {
case 1:
fmt.Println("Sunday")
case 2:
fmt.Println("Monday")
case 3:
fmt.Println("Tuesday")
case 4:
fmt.Println("Wednesday")
case 5:
fmt.Println("Thursday")
case 6:
fmt.Println("Friday")
case 7:
fmt.Println("Saturday")
default:
fmt.Println("Invalid day")
}
}
Copy The Code &
Try With Live Editor
Output
#2 Go switch case with fallthrough
Code -
Golang Programming
// Program to print the day of the week using fallthrough in switch
package main
import "fmt"
func main() {
dayOfWeek := 3
switch dayOfWeek {
case 1:
fmt.Println("Sunday")
case 2:
fmt.Println("Monday")
case 3:
fmt.Println("Tuesday")
fallthrough
case 4:
fmt.Println("Wednesday")
case 5:
fmt.Println("Thursday")
case 6:
fmt.Println("Friday")
case 7:
fmt.Println("Saturday")
default:
fmt.Println("Invalid day")
}
}
Copy The Code &
Try With Live Editor
#3 Go switch with multiple cases
Code -
Golang Programming
// Program to check if the day is a weekend or a weekday
package main
import "fmt"
func main() {
dayOfWeek := "Sunday"
switch dayOfWeek {
case "Saturday", "Sunday":
fmt.Println("Weekend")
case "Monday","Tuesday","Wednesday","Thursday","Friday":
fmt.Println("Weekday")
default:
fmt.Println("Invalid day")
}
}
Copy The Code &
Try With Live Editor
Output
#4 Golang switch without expression
Code -
Golang Programming
// Program to check if it's February or not using switch without expression
package main
import "fmt"
func main() {
numberOfDays := 28
// switch without any expression
switch {
case 28 == numberOfDays:
fmt.Println("It's February")
default:
fmt.Println("Not February")
}
}
Copy The Code &
Try With Live Editor
Output
#5 Go switch optional statement
Code -
Golang Programming
// Program to check the day of a week using optional statement
package main
import "fmt"
func main() {
// switch with statement
switch day := 4; day {
case 1:
fmt.Println("Sunday")
case 2:
fmt.Println("Monday")
case 3:
fmt.Println("Tuesday")
case 4:
fmt.Println("Wednesday")
case 5:
fmt.Println("Thursday")
case 6:
fmt.Println("Friday")
case 7:
fmt.Println("Saturday")
default:
fmt.Println("Invalid Day!")
}
}
Copy The Code &
Try With Live Editor
Output
Demonstration
Go Lang Program switch