Wednesday 27 May 2009

Chunk 11 - Switch Statement (Draft)

M257 Unit2

Switch Statement

The switch statement, like an if-then-else statement, is used to control the program execution based on the value of a control variable. The value of the control variable is compared to the value of a case selector which is a constant value and needs to be of the same type as the control variable.

The switch statement is executed from top to bottom comparing the value of the control variable to each case selector. If the value of the control variable matches the value of a case selector, the program will jump to that block of statements and the code after the case selector will be executed until a break command causes the execution to terminate.

The possible types of the control variable and the case selectors are the primitive types int, char, short or byte; unlike in other programming languages, a control variable of type String is not supported in Java and Processing. In Java 1.5, a new type called the enum or enumerated type was introduced, which is not supported by Processing at the time of writing.

The common format of a switch statement is:

switch (control variable)

{

case selector:

statements;

break;

case selector:

statements;

break;

case selector:

statements;

break;

default:

statements;

break;

}

There can be as many cases as required and each case can contain as many statements as needed.

It is important to stress out that the break command is optional, but if you fail to give it, the execution flows through into the next case (even if the selector and the control variable do not match) executing the code block for that case. In addition, the program will continue checking all the remaining cases until it reaches the end of the switch statement which is a waste of computing time.

The default case at the end of the switch case is optional and the statements this block are executed if no case selector matches the value of the control variable. The last break in the default case is unnecessary but it is considered good programming practice to write it.

The switch statement is frequently used where a large number of conditions need to be tested; they are easier to debug, easier to read and easier to maintain than an equivalent series of if-then-else statements. Let us consider the following program using if-then-else statements:

/**

* Set up the scene

*/

void setup()

{

size(200, 200);

stroke(255);

background(0);

displayMessage();

}

/**

* Begin the drawing

*/

void draw()

{

//if the user presses a key

if(keyPressed)

{

//if the user presses l draw a line

if (key=='l')

{

background(0);

line(50, 100, 150, 100);

}

//if the user presses c draw a circle

else if (key=='c')

{

background(0);

ellipse(100, 100, 50, 50);

}

//if the user presses s draw a square

else if (key=='s')

{

background(0);

rect(75, 75, 50, 50);

}

//if the user presses r draw a rectangle

else if (key=='r')

{

background(0);

rect(75, 50, 50, 100);

}

//if the user presses r draw a rectangle

else if (key=='t')

{

background(0);

triangle(75, 125, 75, 75, 150, 125);

}

//if the user presses any other key display a message

else

{

background(0);

displayMessage();

}

}

}

/**

* Display a message

*/

void displayMessage()

{

PFont font = createFont("Arial", 10);

textFont(font);

fill(204, 102, 0);

text("This example demonstrates the usage \nof a switch statement.", 10, 30);

text("Press the following keys to draw a shape:", 10, 60);

text("l --> line", 10, 90);

text("c --> circle", 10, 110);

text("s --> squarre", 10, 130);

text("r --> rectangle", 10, 150);

text("t --> triangle", 10, 170);

}

This program can be rewritten by using a switch statement:

/**

* Set up the scene

*/

void setup()

{

size(200, 200);

stroke(255);

background(0);

displayMessage();

}

/**

* Begin the drawing

*/

void draw()

{

if(keyPressed)

{

//if the user presses a key

switch(key)

{

//if the user presses l draw a line

case 'l':

background(0);

line(50, 100, 150, 100);

break;

//if the user presses c draw a circle

case 'c':

background(0);

ellipse(100, 100, 50, 50);

break;

//if the user presses s draw a square

case 's':

background(0);

rect(75, 75, 50, 50);

break;

//if the user presses r draw a rectangle

case 'r':

background(0);

rect(75, 50, 50, 100);

break;

//if the user presses r draw a triangle

case 't':

background(0);

triangle(75, 125, 75, 75, 150, 125);

break;

//if the user presses any other key display a message

default:

background(0);

displayMessage();

break;

}

}

}

/**

* Display a message

*/

void displayMessage()

{

PFont font = createFont("Arial", 10);

textFont(font);

fill(204, 102, 0);

text("This example demonstrates the usage \nof a switch statement.", 10, 30);

text("Press the following keys to draw a shape:", 10, 60);

text("l --> line", 10, 90);

text("c --> circle", 10, 110);

text("s --> squarre", 10, 130);

text("r --> rectangle", 10, 150);

text("t --> triangle", 10, 170);

}

The same program (case insensitive input):

/**

* Set up the scene

*/

void setup()

{

size(200, 200);

stroke(255);

background(0);

displayMessage();

}

/**

* Begin the drawing

*/

void draw()

{

if(keyPressed)

{

//if the user presses a key

switch(key)

{

//if the user presses l draw a line

case 'L':

case 'l':

background(0);

line(50, 100, 150, 100);

break;

//if the user presses c draw a circle

case 'C':

case 'c':

background(0);

ellipse(100, 100, 50, 50);

break;

//if the user presses s draw a square

case 'S':

case 's':

background(0);

rect(75, 75, 50, 50);

break;

//if the user presses r draw a rectangle

case 'R':

case 'r':

background(0);

rect(75, 50, 50, 100);

break;

//if the user presses r draw a triangle

case 'T':

case 't':

background(0);

triangle(75, 125, 75, 75, 150, 125);

break;

//if the user presses any other key display a message

default:

background(0);

displayMessage();

break;

}

}

}

/**

* Display a message

*/

void displayMessage()

{

PFont font = createFont("Arial", 10);

textFont(font);

fill(204, 102, 0);

text("This example demonstrates the usage \nof a switch statement.", 10, 30);

text("Press the following keys to draw a shape:", 10, 60);

text("l --> line", 10, 90);

text("c --> circle", 10, 110);

text("s --> squarre", 10, 130);

text("r --> rectangle", 10, 150);

text("t --> triangle", 10, 170);

}

No comments: