Showing posts with label switch. Show all posts
Showing posts with label switch. Show all posts

C# - Control Statements


Control flow and program logic are of the most important parts of a programming language’s dynamic behavior. In this section, I’ll cover control flow in C#. Most of the condition and looping statements in C# comes from c and C++. Those who are familiar with java will recognize most of them, as well.

The if . . .else Statement 
The if . . .else statement is inherited from C and C++. The if . . .else statement is also known as a conditional statement. For example:
if (condition)
statement
else
statement

The if. . .section of the statement or statement block is executed when the condition is true; if it’s false, control goes to the else statement or statement block. You can have a nested if . . .else statement with one of more else blocks. 

The Selective Structure : switch

The syntax of the switch instruction is a bit peculiar. Its objective is to check several possible constant values for an expression, something similar to what we did at the beginning of this section with the linking of several if and else if sentences. Its form is the following:
switch (expression)
{
case constant1:
block of instructions 1
break;
case constant2:
block of instructions 2
break; . . .
default: default block of instructions