aboutsummaryrefslogtreecommitdiffstats
path: root/source/arduino/switchcase.rst
diff options
context:
space:
mode:
authorMarti Bolivar <mbolivar@mit.edu>2010-10-25 21:15:28 -0400
committerMarti Bolivar <mbolivar@mit.edu>2010-11-17 12:44:28 -0500
commit2d429e75ce69e77f8c95490ac03881ec9aa0354a (patch)
treea3b810a6c75625b07a4b976e5d1e319c60e19a6b /source/arduino/switchcase.rst
parent30ac55d80c18e93f9c39a6dd850c10f9e7fd92ac (diff)
downloadlibrambutan-2d429e75ce69e77f8c95490ac03881ec9aa0354a.tar.gz
librambutan-2d429e75ce69e77f8c95490ac03881ec9aa0354a.zip
arduino language reference nearing completion, properly CC-BY-SA 3.0 attributed
Diffstat (limited to 'source/arduino/switchcase.rst')
-rw-r--r--source/arduino/switchcase.rst89
1 files changed, 45 insertions, 44 deletions
diff --git a/source/arduino/switchcase.rst b/source/arduino/switchcase.rst
index 28791eb..1634de1 100644
--- a/source/arduino/switchcase.rst
+++ b/source/arduino/switchcase.rst
@@ -3,71 +3,72 @@
switch / case statements
========================
-Like **if** statements, **switch...case** controls the flow of
-programs by allowing programmers to specify different code that
-should be executed in various conditions. In particular, a switch
-statement compares the value of a variable to the values specified
-in case statements. When a case statement is found whose value
-matches that of the variable, the code in that case statement is
-run.
-
-
-
-The **break** keyword exits the switch statement, and is typically
-used at the end of each case. Without a break statement, the switch
-statement will continue executing the following expressions
-("falling-through") until a break, or the end of the switch
-statement is reached.
-
-
-
-Example
-~~~~~~~
-
-::
-
- 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
- }
-
-
+Like :ref:`if/else <arduino-else>` blocks, A ``switch`` statement
+controls program flow by allowing you to specify different code that
+should be executed under various cases.
Syntax
-~~~~~~
+------
::
switch (var) {
- case label:
+ case val1:
// statements
break;
- case label:
+ case val2:
// statements
break;
- default:
+ ...
+ default:
// statements
}
+Where ``var`` is a variable whose value to investigate, and the
+``val1``, ``val2`` after each ``case`` are constant values that
+``var`` might be.
+Description
+-----------
-Parameters
-~~~~~~~~~~
+A ``switch`` statement compares the value of a variable to the values
+specified in ``case`` statements. When a ``case`` statement is found
+whose value matches that of the variable, the code in that case
+statement is run.
-var: the variable whose value to compare to the various cases
+The ``break`` keyword exits the switch statement, and is typically
+used at the end of each ``case``. Without a ``break``, the ``switch``
+statement will continue executing the following ``case`` expressions
+("falling-through") until a ``break`` (or the end of the switch
+statement) is reached.
+Writing ``default:`` instead of a ``case`` statement allows you to
+specify what to do if none of the ``case`` statements matches. Having
+a ``default:`` is optional (you can leave it out), but if you have
+one, it must appear after all of the ``case`` statements, as shown
+above.
+``switch`` statements are often used with an ``enum`` value as the
+variable to compare. In this case, you can write down all of the
+values the ``enum`` takes as ``case`` statements, and be sure you've
+covered all the possibilities.
-label: a value to compare the variable to
+Example
+-------
+::
+ 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
+ }
See also:
---------