Conditional Statement in C#
Condition Statement:
A statement that can be executed based
on a condition is known as a “Conditional Statement”.
The statement is often a block of code.
The following are the 2 types:
Conditional Branching.
Conditional Looping.
Conditional Branching:
This statement allows you to branch
your code depending on whether or not a certain condition is met.
In C# are the following conditional
branching statements:
1.
if statement.
2.
if-else statement.
3.
if-else-if ladder statement.
4.
Nested if statement.
5.
switch case statement.
Here if, else and switch are
pre-defined keywords.
1. if Statement:
If statement checks the given Boolean condition.
If the Boolean condition evaluates to
be true then the block of code/statements will execute otherwise not.
Syntax:
if (condition)
{
//code to be executed if condition is true.
Note:
If the curly brackets { } are not used
with if statements than the statement just next to it is only considered
associated with the if statement.
Example:
if (condition)
Statement 1;
Statement 2;
2. if-else Statement:
The C# if-else statement also tests the
condition. It executes the if block if condition is true otherwise else block
is executed.
Syntax:
If (condition)
{
//code if condition is true
}
else
{
//code if condition is false
}
3. if – else – if ladder Statement:
The if-else-if ladder statement
executes one condition from multiple statements.
The execution starts from top and
checked for each if condition.
The statement of if block will be
executed which evaluates to be true.
If none of the if condition evaluates
to be true then the last else block is evaluated.
Syntax:
if(condition1)
{
// code to be executed if
condition1 is true
}
else if(condition2)
{
// code to be executed if
condition2 is true
}
else if(condition3)
{
// code to be executed if
condition3 is true
}
…
else
{
// code to be executed if all the
conditions are false
}
4. Nested – If Statement
if statement inside an if statement is known as
nested if.
if statement in this case is the target of another
if or else statement.
When more than one condition needs to be true and
one of the condition is the sub-condition of parent condition, nested if can be
used.
Syntax:
if (condition1)
{
// code to be executed
// if condition2 is true
if (condition2)
{
// code to be executed
// if condition2 is true
}
else
{
//code to be executed
}
}
5.
switch case
Statement:
vC# switch case statement is
a selection statement.
vThe C# switch statement is
an alternate to using the C# if else statement when there are more than a few
options.
vThe switch case statement
is checked for different cases and the one match is
executed.
vThere is default case
(optional) at the end of switch, if none of the case matches then default case
is executed.
vC# switch statement pairs
with one or more case blocks and a default block.
vThe break in
switch statement is used to terminate or exit the current sequence.
vThe switch expression is of
integer type such as int, char, byte, or short, or of an enumeration type, or
of string type. The expression is checked for different cases and the one match
is executed.
vIn C#, duplicate case
values are not allowed.
vThe data type of
the variable in the switch and value of a case must be of the
same type.
vThe value of a case must be
a constant or a literal.
vVariables are not allowed.
vThe default statement is
optional and it can be used anywhere inside the switch statement.
vMultiple default statements
are not allowed.
vNested Switch case are
allowed in C#. In this case, switch is present inside other switch case. Inner
switch is present in one of the cases in parent switch.
Why do we use Switch Statements instead of if-else
statements?
We use a switch statement
instead of if-else statements because if-else statement only works for a small
number of logical evaluations of a value.
If you use if-else
statement for a larger number of possible conditions then, it takes more time
to write and process and also become difficult to read.
vThe advantage of using
switch over if...else if statement is the codes will look much cleaner and
readable with switch.
The syntax of switch statement is:
switch
(variable/expression)
{
case value1:
// Statements executed if
expression/variable = value1
break;
case value2:
// Statements executed if expression = value1
break;
... ... ...
... ... ...
default:
// Statements executed if
no case matches
}
Comments
Post a Comment