"switch" Statements

This section describes 'switch' statements - controlling expressions with multiple embedded statement blocks. At run time, only one statement blocks will be executed depending on the value of the controlling expression.

A "switch" statement switches the flow of execution to one of the pre-defined sections of statement depending on the resulting value of the controlling expression. Here is the syntax of a "switch" statement:

switch statement:
   switch (expression) {
   case literal_1:
      embeded_statements
      break;
   case literal_2:
      embeded_statements
      break;
   ......
   default: 
      embeded_statements
      break;
   }

When a "switch" statement is encountered in the execution flow, the expression will be evaluated first. If resulting value equals to one of the literals listed in the "case" clauses, the execution flow will be switched to the first embedded statement of that "case" clause. The "break" statement will break the execution flow from continuing to the next statement, and transfer it to the end of the "switch" statement.

If the resulting value of the expression does no equal to any of the literals listed in the "case" clauses, the execution flow will be switched to the first embedded statement of the "default" clause.

Last update: 2010.

Table of Contents

 About This Book

 Introduction of C# (C Sharp)

 Data Type and Variables

Logical Expressions and Conditional Statements

 Logical Expressions

 "if" Statements

 "if" Statements - Example

"switch" Statements

 "switch" Statements - Example

 Arrays and Loop Statements

 Data Type Features

 Floating-Point Data Types

 Passing Parameters to Methods

 Execution Environment Class

 Visual C# 2010 Express Edition

 Class Features

 C# Compiler and Intermediate Language

 Compiling C# Source Code Files

 MSBuild - Microsoft Build Engine

 Memory Usages of Processes

 Multithreading in C#

 Async Feature from C# 5

 System.IO.FileInfo Class

 System.Diagnostics.FileVersionInfo Class

 WPF - Windows Presentation Foundation

 Partial Classes and Partial Methods

 Outdated Tutorials

 References

 PDF Printing Version