What is the switch statement?

February 7th, 2008 by uCertify Leave a reply »

The switch statement is a multiway branch statement. The general form of the switch statement is given below:

switch(expression) {
case value1://statements
break;
case value2://statements
break;
.
.
default:
//default statements
}

The expression must be of int, char, byte, or short data type and the data type of the values specified must be compatible with the expression.

Each case value must be a unique literal value. Variables are not allowed as case values.

The execution process of a switch statement is as follows:
The value of the expression is matched with each of the literal values in the case statements. If a match is found then the code sequence following that case statement is executed. If none of the values match, then the default statement is executed.

If the default statement is not given and no matching value is found, then no further action is taken.

Note: The break statement is used inside the switch statement to terminate a statement sequence. When a break statement is encountered, the execution branches to the first line of code that follows the entire switch statement block.

  • Share/Bookmark
Advertisement

Leave a Reply

uCertify.com | Our Company | Articles | Contact Us | News and Press Release | uCertify India | Entries (RSS)
MCSE: MCSA, MCTS, MCITP    JAVA Certification: SCJP, SCWCD    Cisco Certification: CCNA, CCENT    A+, Network+, Security+ Project+
Oracle Certification: OCP 11g, OCP 10g, OCA 11g, OCA 10g    CIW foundation    EC-212-32,    CISSP    Photoshop ACE CS4    Adobe Flash ACE, PMP, CAPM
© 2008 uCertify.com. All rights reserved. All trademarks are the property of their respective owners.