From 3211abc82afc789b60efb2b559d4f7b7b8815daf Mon Sep 17 00:00:00 2001 From: Sheel Bedi Date: Wed, 1 Nov 2023 01:36:29 -0500 Subject: [PATCH 1/5] initial commit --- tutorials/learn-golang.org/en/Switches.md | 133 ++++++++++++++++++++++ tutorials/learn-golang.org/en/Welcome.md | 1 + 2 files changed, 134 insertions(+) create mode 100644 tutorials/learn-golang.org/en/Switches.md diff --git a/tutorials/learn-golang.org/en/Switches.md b/tutorials/learn-golang.org/en/Switches.md new file mode 100644 index 000000000..084d60697 --- /dev/null +++ b/tutorials/learn-golang.org/en/Switches.md @@ -0,0 +1,133 @@ +Tutorial +-------- + +We've already looked at if-else statements, and we've seen that we can cascade these statements, but is there a cleaner way to do that? + +## switch statement + +A switch statement is a shorter way to write a sequence of if - else statements. It runs the first case whose value is equal to the condition expression. + +The general syntax is: + + switch name { + case "Alice": + // Code to be executed if name is Alice + case "Bob": + // Code to be executed if name is Bob + } + +Optionally, we can add a default statement to handle situations where none of the conditions are met: + + switch name { + case "Alice": + fmt.Println("Hi Alice!") + case "Bob": + fmt.Println("Hi Bob!") + default: + fmt.Println("I don't know you!") + } + +Cases are evaluated from top to bottom, stopping at the first case that is met: + + x := 10 + switch x { + case x == 0: + // Since x is 10 in this example, this case will not execute + case x > 5: + // X is 10, which is greater than 5. This case will execute. + case x > 0: + // While this case is also true, because an earlier case is true, this case will not execute + } + +## Example +Let's write an example code to check if the user's name is `John` or not + + userName := "Alice" + + // prints Hi Alice! + switch userName { + case "Alice": + fmt.Println("Hi Alice!") + case "Bob": + fmt.Println("Hi Bob!") + default: + fmt.Println("I don't know you!") + } + + // let's change the username variable + userName := "Bob" + + // prints Hi Bob! + switch userName { + case "Alice": + fmt.Println("Hi Alice!") + case "Bob": + fmt.Println("Hi Bob!") + default: + fmt.Println("I don't know you!") + } + userName := "Bob" + + // let's change the username again + userName := "Matthew" + // prints I don't know you! + switch userName { + case "Alice": + fmt.Println("Hi Alice!") + case "Bob": + fmt.Println("Hi Bob!") + default: + fmt.Println("I don't know you!") + } + +Just like an if statement, the switch statement also provides an option to initialize a variable and test the condition within the if statement. The general syntax in this case is + + switch ; { + case: + // code to execute if condition is true + } + +An example code is below: + // Initializes x by setting it to 5, then checks if a is positive or negative + switch x := 5; x { + case a < 0: + fmt.Println("A is negative") + case a > 0: + fmt.Println("A is positive") + } + +Exercise +-------- +In a country, a person is allowed to vote if his/her age is above or equal to 18. Check the userAge variable and print `"Eligible"` if the person is eligible to vote or `"Not Eligible"` if the person is not eligible. + +Tutorial Code +------------- +package main + +import "fmt" + +func main () { + userAge := 22 + + // Add your code here. +} + +Expected Output +--------------- +Eligible + +Solution +-------- +package main + +import "fmt" + +func main () { + userAge := 22 + + if userAge >= 18 { + fmt.Println("Eligible") + } else { + fmt.Println("Not Eligible") + } +} diff --git a/tutorials/learn-golang.org/en/Welcome.md b/tutorials/learn-golang.org/en/Welcome.md index 329ba746d..ed6f0231f 100644 --- a/tutorials/learn-golang.org/en/Welcome.md +++ b/tutorials/learn-golang.org/en/Welcome.md @@ -15,6 +15,7 @@ learn-golang.org is still under construction - If you wish to contribute tutoria - [[Arrays]] - [[Slices]] - [[If-Else]] +- [[Switches]] - [[Loops]] - [[Functions]] - [[The fmt module]] From 96df7781c5848c2314dd698e3ed8fae54edfd933 Mon Sep 17 00:00:00 2001 From: Sheel Bedi Date: Wed, 1 Nov 2023 02:38:46 -0500 Subject: [PATCH 2/5] finalize exercise --- tutorials/learn-golang.org/en/Switches.md | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/tutorials/learn-golang.org/en/Switches.md b/tutorials/learn-golang.org/en/Switches.md index 084d60697..6f18227e1 100644 --- a/tutorials/learn-golang.org/en/Switches.md +++ b/tutorials/learn-golang.org/en/Switches.md @@ -98,7 +98,7 @@ An example code is below: Exercise -------- -In a country, a person is allowed to vote if his/her age is above or equal to 18. Check the userAge variable and print `"Eligible"` if the person is eligible to vote or `"Not Eligible"` if the person is not eligible. +In a college course, a score below 70 is considered failing, a score of 70 to 90 is considered acceptable, and a score greater than 90 is considered outstanding. Write a switch statement to check the score variable. Print `"Failing"` if the score is below 70, `"Acceptable"` if the score is between 70-90 inclusive, and `"Outstanding"` if the score is more than 90. Tutorial Code ------------- @@ -107,14 +107,14 @@ package main import "fmt" func main () { - userAge := 22 + score := 85 // Add your code here. } Expected Output --------------- -Eligible +Acceptable Solution -------- @@ -123,11 +123,13 @@ package main import "fmt" func main () { - userAge := 22 - - if userAge >= 18 { - fmt.Println("Eligible") - } else { - fmt.Println("Not Eligible") + score := 85 + switch score { + case score > 90: + fmt.Println("Outstanding") + case score >= 70: + fmt.Println("Acceptable") + case score < 70: + fmt.Println("Failing") } } From d2a777f1628b50b8a630b3c436e701c0dd0062bb Mon Sep 17 00:00:00 2001 From: Sheel Bedi Date: Wed, 1 Nov 2023 03:36:07 -0500 Subject: [PATCH 3/5] proofread and edit exercise --- app.env.example | 2 - tutorials/learn-golang.org/en/Switches.md | 45 +++++++++++++---------- 2 files changed, 25 insertions(+), 22 deletions(-) delete mode 100644 app.env.example diff --git a/app.env.example b/app.env.example deleted file mode 100644 index 00b852593..000000000 --- a/app.env.example +++ /dev/null @@ -1,2 +0,0 @@ -IDEONE_USERNAME= -IDEONE_PASSWORD= diff --git a/tutorials/learn-golang.org/en/Switches.md b/tutorials/learn-golang.org/en/Switches.md index 6f18227e1..86194b7a5 100644 --- a/tutorials/learn-golang.org/en/Switches.md +++ b/tutorials/learn-golang.org/en/Switches.md @@ -27,10 +27,28 @@ Optionally, we can add a default statement to handle situations where none of th fmt.Println("I don't know you!") } -Cases are evaluated from top to bottom, stopping at the first case that is met: +Just like an if statement, the switch statement also provides an option to initialize a variable and test the condition within the if statement. The general syntax in this case is + + switch ; { + case: + // code to execute if condition is true + } + +An example code is below; this will print "Hi Alice!": + // Initializes name by setting it to Alice + switch name := "Alice"; name { + case "Alice": + fmt.Println("Hi Alice!") + case "Bob": + fmt.Println("Hi Bob!") + default: + fmt.Println("I don't know you!") + } + +A switch statement can be run bare, without any variable or condition; this can be a clean way to write long if-then-else chains: x := 10 - switch x { + switch { case x == 0: // Since x is 10 in this example, this case will not execute case x > 5: @@ -39,6 +57,8 @@ Cases are evaluated from top to bottom, stopping at the first case that is met: // While this case is also true, because an earlier case is true, this case will not execute } +Remember that cases are evaluated from top to bottom, stopping at the first case that is met. In the above example, x > 5 is the first true case, and any later cases within the switch statement are not checked. + ## Example Let's write an example code to check if the user's name is `John` or not @@ -68,8 +88,9 @@ Let's write an example code to check if the user's name is `John` or not } userName := "Bob" - // let's change the username again + // let's change the username variable again userName := "Matthew" + // prints I don't know you! switch userName { case "Alice": @@ -79,22 +100,6 @@ Let's write an example code to check if the user's name is `John` or not default: fmt.Println("I don't know you!") } - -Just like an if statement, the switch statement also provides an option to initialize a variable and test the condition within the if statement. The general syntax in this case is - - switch ; { - case: - // code to execute if condition is true - } - -An example code is below: - // Initializes x by setting it to 5, then checks if a is positive or negative - switch x := 5; x { - case a < 0: - fmt.Println("A is negative") - case a > 0: - fmt.Println("A is positive") - } Exercise -------- @@ -124,7 +129,7 @@ import "fmt" func main () { score := 85 - switch score { + switch { case score > 90: fmt.Println("Outstanding") case score >= 70: From f5c418d61cbdddec9cc8b1760f80ef6cbaec623e Mon Sep 17 00:00:00 2001 From: Sheel Bedi Date: Wed, 1 Nov 2023 03:44:21 -0500 Subject: [PATCH 4/5] restore example --- app.env.example | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 app.env.example diff --git a/app.env.example b/app.env.example new file mode 100644 index 000000000..86590febb --- /dev/null +++ b/app.env.example @@ -0,0 +1,2 @@ +IDEONE_USERNAME= +IDEONE_PASSWORD= \ No newline at end of file From ecf7f8cdadf994111acffa5895061b9fbeedf644 Mon Sep 17 00:00:00 2001 From: Sheel Bedi Date: Mon, 16 Mar 2026 02:38:54 -0500 Subject: [PATCH 5/5] Fix userName variable redeclaration in Switches.md Updated the userName variable assignments to use the same variable instead of redeclaring it. --- tutorials/learn-golang.org/en/Switches.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tutorials/learn-golang.org/en/Switches.md b/tutorials/learn-golang.org/en/Switches.md index 86194b7a5..7744b3606 100644 --- a/tutorials/learn-golang.org/en/Switches.md +++ b/tutorials/learn-golang.org/en/Switches.md @@ -75,7 +75,7 @@ Let's write an example code to check if the user's name is `John` or not } // let's change the username variable - userName := "Bob" + userName = "Bob" // prints Hi Bob! switch userName { @@ -86,10 +86,9 @@ Let's write an example code to check if the user's name is `John` or not default: fmt.Println("I don't know you!") } - userName := "Bob" // let's change the username variable again - userName := "Matthew" + userName = "Matthew" // prints I don't know you! switch userName {