Selection Statements: If, Else Switch, Case
Selection Statements: If, Else Switch, Case
Selection Statements: If, Else Switch, Case
if, else
if, else, else if (C#.Net) If, Else, ElseIf, End If (VB.Net)
switch, case
The if statement selects a statement for execution based on the value of a Boolean expression.
C# VB.Net
DEMO
Selection Statements
if, else
switch, case (C#.Net) Select Case (VB.Net)
switch, case
The switch statement is a control statement that handles multiple selections by passing control to one of
the case statements within its body.
switch (expression) Control is transferred to the case statement whose constant-expression matches
{
case constant-
expression.
expression: The switch statement can include any number of case instances, but no two case
statement constants within the same switch statement can have the same value.
jump-statement Notice that the jump-statement is required after each block, including the last
[default:
statement
block whether it is a case statement or a default statement. Unlike the C++ switch
jump-statement] statement, C# does not support an explicit fall through from one case label to
} another.
C# VB.Net
DEMO
Iteration Statements
do
do (C#.Net) Do, Loop (VB.Net)
for
foreach, in
The do statement executes a statement or a block of statements repeatedly until a specified expression
evaluates to false.
while
do Unlike the while statement, the body loop of the do statement is executed at least
{
statement(s);
once regardless of the value of the expression.
}
while (expression);
C# VB.Net
DEMO
Iteration Statements
do
for (C#.Net) For, Next (VB.Net)
for
foreach, in
The for loop executes a statement or a block of statements repeatedly until a specified expression evaluates
to false.
while
for ([initializers]; First, the initializers are evaluated.
[expression];
[iterators])
Then, while the expression evaluates to true, the statement(s) are executed and the
statement(s); iterators are evaluated.
When the expression becomes false, control is transferred outside the loop.
Because the test of expression takes place before the execution of the loop, a for
statement executes zero or more times.
All of the expressions of the for statement are optional (in C# only); for example, the following statement is used to write an
infinite loop: for (;;) { …}
C# VB.Net
DEMO
Iteration Statements
do
foreach (C#.Net) For Each (VB.Net)
for
foreach, in
The foreach statement repeats a block of statements for each element in an array or an object collection.
while
The foreach statement is a convenient way to iterate over the elements of an array. It can also enumerate
the elements of a collection, provided that the collection class has implemented the
System.Collections.IEnumerator and System.Collections.IEnumerable interfaces.
foreach (type identifier in The foreach statement is used to iterate through the collection to get the desired
expression)
statement(s)
information, but should not be used to change the contents of the collection to
avoid unpredictable side effects.
Initialize
Assign DEMO
Read
Arrays
Arrays in C#.Net
C# arrays are zero indexed; that is, the array indexes start at zero.
C# supports single-dimensional arrays, multidimensional arrays (rectangular arrays), and array-of-arrays
(jagged arrays).
In C#, arrays are actually objects. System.Array is the abstract base type of all array types
Multi-Dimensional Array
Declare
Initialize
Assign
Read
DEMO
Arrays
Arrays in C#.Net
C# arrays are zero indexed; that is, the array indexes start at zero.
C# supports single-dimensional arrays, multidimensional arrays (rectangular arrays), and array-of-arrays
(jagged arrays).
In C#, arrays are actually objects. System.Array is the abstract base type of all array types
Jagged Array
Declare
Initialize
Assign
DEMO
Arrays
Initializing Arrays at Declaration
C# provides simple and straightforward ways to initialize arrays at declaration time by enclosing the initial values in curly
braces ({}). The following examples show different ways to initialize different kinds of arrays.
Single
Dimensional
DEMO
Multi
Dimensional
Jagged