15.04.2018 Views

programming-for-dummies

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Branching Statements 549<br />

if (condition1) {<br />

Command;<br />

}<br />

else if (condition2) {<br />

Command;<br />

}<br />

else if (condition3) {<br />

Command;<br />

}<br />

Book VI<br />

Chapter 2<br />

Java and C#<br />

As an alternative to the IF-ELSEIF statement, you can also use the SWITCH<br />

statement, such as<br />

switch (expression) {<br />

case value1:<br />

Command;<br />

break;<br />

case value2:<br />

Command;<br />

break;<br />

default:<br />

Command;<br />

}<br />

The SWITCH statement always needs to include the break command to tell<br />

the computer when to exit the SWITCH statement.<br />

The preceding SWITCH statement is equivalent to the following IF-ELSEIF<br />

statement:<br />

if (expression = value1) {<br />

Command;<br />

}<br />

else if (expression = value2) {<br />

Command;<br />

}<br />

else {<br />

Command;<br />

}<br />

To check if a variable matches multiple values, you can stack multiple CASE<br />

statements, such as

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!