From 27be37638eb2374fef3e5a4e5185181e8732c36a Mon Sep 17 00:00:00 2001 From: Mikkel-danielsen <53428543+Mikkel-danielsen@users.noreply.github.com> Date: Thu, 1 Aug 2019 15:01:38 +0200 Subject: [PATCH 1/5] Update switchCase.adoc A good thing to know, A had a lot of trubleshooting befor I found out. ;) --- Language/Structure/Control Structure/switchCase.adoc | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/Language/Structure/Control Structure/switchCase.adoc b/Language/Structure/Control Structure/switchCase.adoc index faa4a60a7..857fee2e5 100644 --- a/Language/Structure/Control Structure/switchCase.adoc +++ b/Language/Structure/Control Structure/switchCase.adoc @@ -24,6 +24,7 @@ The link:../break[break] keyword exits the switch statement, and is typically us [%hardbreaks] + [float] === Syntax [source,arduino] @@ -52,6 +53,10 @@ switch (var) { === Returns Nothing +[float] +=== Note + +If you need to declare one, or more, variables in a case you need to use curly brackets {} to define the scope specific to that case statement and define your variable within it. -- // OVERVIEW SECTION ENDS @@ -71,9 +76,11 @@ switch (var) { case 1: //do something when var equals 1 break; - case 2: + case 2:{ //do something when var equals 2 + int var2 = 1337; //to declare a variable you need the curly brackets break; + } default: // if nothing else matches, do the default // default is optional From a85306e1252a13164f5e81b4629c1898ca4a318f Mon Sep 17 00:00:00 2001 From: Mikkel-danielsen <53428543+Mikkel-danielsen@users.noreply.github.com> Date: Mon, 2 Sep 2019 15:09:07 +0200 Subject: [PATCH 2/5] Update Language/Structure/Control Structure/switchCase.adoc Co-Authored-By: per1234 --- Language/Structure/Control Structure/switchCase.adoc | 1 - 1 file changed, 1 deletion(-) diff --git a/Language/Structure/Control Structure/switchCase.adoc b/Language/Structure/Control Structure/switchCase.adoc index 857fee2e5..578dbcb08 100644 --- a/Language/Structure/Control Structure/switchCase.adoc +++ b/Language/Structure/Control Structure/switchCase.adoc @@ -24,7 +24,6 @@ The link:../break[break] keyword exits the switch statement, and is typically us [%hardbreaks] - [float] === Syntax [source,arduino] From a3c74ae087326b5feaf39fe3f3c7e3049f8c7307 Mon Sep 17 00:00:00 2001 From: Mikkel-danielsen <53428543+Mikkel-danielsen@users.noreply.github.com> Date: Mon, 2 Sep 2019 15:09:14 +0200 Subject: [PATCH 3/5] Update Language/Structure/Control Structure/switchCase.adoc Co-Authored-By: per1234 --- Language/Structure/Control Structure/switchCase.adoc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Language/Structure/Control Structure/switchCase.adoc b/Language/Structure/Control Structure/switchCase.adoc index 578dbcb08..4dda87397 100644 --- a/Language/Structure/Control Structure/switchCase.adoc +++ b/Language/Structure/Control Structure/switchCase.adoc @@ -55,7 +55,7 @@ Nothing [float] === Note -If you need to declare one, or more, variables in a case you need to use curly brackets {} to define the scope specific to that case statement and define your variable within it. +If you need to declare a variable in a case, use curly brackets {} to define a scope within the case statement to avoid a "crosses initialization of" compiler error. -- // OVERVIEW SECTION ENDS From d05c66cf5ac1a536be6f709bc61ce6dbecf3cace Mon Sep 17 00:00:00 2001 From: Mikkel-danielsen <53428543+Mikkel-danielsen@users.noreply.github.com> Date: Mon, 2 Sep 2019 15:09:20 +0200 Subject: [PATCH 4/5] Update Language/Structure/Control Structure/switchCase.adoc Co-Authored-By: per1234 --- Language/Structure/Control Structure/switchCase.adoc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Language/Structure/Control Structure/switchCase.adoc b/Language/Structure/Control Structure/switchCase.adoc index 4dda87397..573da2807 100644 --- a/Language/Structure/Control Structure/switchCase.adoc +++ b/Language/Structure/Control Structure/switchCase.adoc @@ -75,7 +75,7 @@ switch (var) { case 1: //do something when var equals 1 break; - case 2:{ + case 2: { //do something when var equals 2 int var2 = 1337; //to declare a variable you need the curly brackets break; From a885353adad80f6b09e74766715be9a18ed33d98 Mon Sep 17 00:00:00 2001 From: Mikkel-danielsen <53428543+Mikkel-danielsen@users.noreply.github.com> Date: Tue, 5 Oct 2021 10:48:06 +0200 Subject: [PATCH 5/5] Update switchCase.adoc Moved the new example code to the "Notes and Warnings". --- .../Control Structure/switchCase.adoc | 30 ++++++++++++++----- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/Language/Structure/Control Structure/switchCase.adoc b/Language/Structure/Control Structure/switchCase.adoc index 573da2807..327ab1ce6 100644 --- a/Language/Structure/Control Structure/switchCase.adoc +++ b/Language/Structure/Control Structure/switchCase.adoc @@ -52,11 +52,6 @@ switch (var) { === Returns Nothing -[float] -=== Note - -If you need to declare a variable in a case, use curly brackets {} to define a scope within the case statement to avoid a "crosses initialization of" compiler error. --- // OVERVIEW SECTION ENDS @@ -69,6 +64,29 @@ If you need to declare a variable in a case, use curly brackets {} to define a s [float] === Example Code +[source,arduino] +---- +switch (var) { + case 1: + //do something when var equals 1 + break; + case 2: + //do something when var equals 2 + break; + default: + // if nothing else matches, do the default + // default is optional + break; +} + +---- +[%hardbreaks] + + +[float] +=== Notes and Warnings +// Add useful notes, tips, caveat, known issues, and warnings about this Reference term +If you need to declare a variable in a case, use curly brackets {} to define a scope within the case statement to avoid a "crosses initialization of" compiler error. [source,arduino] ---- switch (var) { @@ -85,9 +103,7 @@ switch (var) { // default is optional break; } - ---- -[%hardbreaks] -- // HOW TO USE SECTION ENDS