Learn Create Your Own Split Screen

Next Page

Let's look at that example again and understand the structure of it.
switch (MyEvent.keyCode) { case 37: x_pos = x_pos - 10; // left break; case 38: y_pos = y_pos - 10; // up break; case 39: x_pos = x_pos + 10; // right break; case 40: y_pos = y_pos + 10; // down break; }

We start with the word "switch", then the thing we're switching upon enclosed in round parentheses, and then open a curly-bracket to help delimit the extent of the switch-statement.

For each possible outcome, we use the word "case", the value we want to test against, then a colon and the statements we want executed.

Switch can only work on the basis of equality -- there's an implied "==" operator for each case. You can't use greater-than or less-than.

At the end of the whole switch, we close the curly-bracket.


But what about those "break" statements?