Blocks and Conditional Statements
Blocks
A block
is any number of C# statements that are surrounded by a pair of curly braces.
Blocks define the scope of a variable and they can be nested inside other
blocks. The following is a block that is nested inside the Main method.
public void Main (String[] args )
{
int i, j, k;
. . .
{
int b;
. . .
} // b is only defined up to here
}// i, j, and k are
defined up to here
The if Statement
One of
the notable features any software is its ability to make certain decisions throughout its lifecycle. For
instance, a voting system has the ability to make a decision whether a person
is eligible to vote or not based his or her age value; similarly, a student
grade generating system has the potential to decide whether the student should
be given an ‘A’, ‘B’, ‘C’, ‘D’ or ‘F’ based on the student’s mark.
Having this idea, in C#, to do such sort of
decisions we have something called the if statement. The if statement allows a
programmer to implement a decision based on a certain condition; thus, the if
statement has two parts – a condition to test and a block of statement to be
executed or not based on the truth value of the condition. If the condition is
true the statements within the body of the block will be executed otherwise the
control continues without executing the block statement. Here is a flowchart
that represents a piece of code in a voting system that decides whether a
person is eligible to vote or not based on his or her age.
The above flowchart is represented
in C# as follows:
if (voterAge >= 18)
{
isEligible = true;
}
In the above code snippet, if the
condition is true, the block of code within the if clause is executed;
otherwise, the block is skipped and execution continues to the line of code
immediately after the if block.
We can branch an if statement using
an else statement and the statement as a whole is called if – else statement.
If the condition is true the true block is executed otherwise the else block or
the false block is executed.
A more advanced version of the above voting scenario is illustrated as follows using if-else statement.
The above flowchart is represented
in C# as follows:
if (voterAge >= 18)
{
isEligible = true;
}
else
{
isEligible = false;
}
The if statement has much
more advanced version that has an if statement followed by an associated
else-if statement. Here, if
any one of the conditions evaluates to true, the code blocks associated with
that statement is executed and then control leaves the code block related to
the entire if construct. The following flowchart demonstrates such scenario.
The following program demonstrates full version of the above flowchart using the if --- else if statement in C#.
/*
* Author: Temesghen Tekeste
* The if --- else if statement in C#.
* mail: mail.temesghen.tekeste@gmail.com
* */
namespace DesionsAndConditions
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Welcome to Grade Report Generating
System");
Console.Write("Enter your mark:");
//Converting/type casting the input to double
double studentMark = Convert.ToDouble(Console.ReadLine());
if (studentMark >= 90)
{
output += "Grade: A";
output += "\nRemark: Outstanding";
}
else if(studentMark >=80)
{
output += "Grade: B";
output += "\nRemark: Very Good";
}
else if (studentMark >= 70)
{
output += "Grade: C";
output += "\nRemark: Good";
}
else if (studentMark >= 60)
{
output += "Grade: B";
output += "\nRemark: Poor";
}
else
{
output += "Grade: F";
output += "\nRemark: Very Poor";
}
Console.WriteLine(output);
Console.Write("Press any key to exit...");
Console.ReadKey();
}
}
}
The switch Statement
In c#, the switch statement is used as an alternative to nested if else statement; in fact, it is best used when there are too many if else statements so that the code you write will not be cluttered.
A
switch
statement includes: switch section(s) and case label(s).
The case labels are followed
by one or more statements and they must be evaluated to bool, char, string,
integral, enum or corresponding nullable type. We will discuss enum and strings
in the coming lessons.
The following code snippet shows a general form of a switch statement.
int switchExpression = 1;
switch (switchExpression)
{
case 1 :
Console.WriteLine("You
selected option 1 ");
break;
case 2:
Console.WriteLine("You
selected option 2");
break;
case 3:
Console.WriteLine("You
selected option 3");
break;
case 4:
Console.WriteLine("You
selected option 4");
break;
default:
Console.WriteLine("You
selected unspecified option");
break;
}
The above code snippet that demonstrates the
switch statement is evaluated as follows:
(1) The switch-expression is evaluated to int value which is hold
inside a variable called switchExpression; (2) If the value of the switch-expression matches a case label, the execution starts from the matched
case label and executes all statements until it reaches the break statement and
then the
execution exits or breaks out of the switch block; and (3) If the value of the switch-expression does not match a case label,execution starts at the statement following the optional default label and
executes any statement inside the default case.
The flowchart of the above switch code snippet
is represented as follows:
The following program demonstrates the switch statement in C#.
/*
* Author: Temesghen Tekeste
* The switch statement in C#.
* mail: mail.temesghen.tekeste@gmail.com
* */
using System;
namespace SwitchDemo
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Welcome to Grade Report Generating
System");
String
output = "";
Console.Write("Enter your mark:");
//Converting/type casting the input to double
switch (studentMark / 10)
{
case 10:
case 9:
output = "Grade: A \nRemark:
Outstanding!";
break;
case 8:
output = "Grade: B \nRemark: Very Good!";
break;
case 7:
output = "Grade: C \nRemark: Good!";
break;
case 6:
case 5:
output = "Grade: D \nRemark: You
passed!";
break;
case 4:
case 3:
case 2:
case 1:
case 0:
output = "Grade: F \nRemark: Very
Failed!";
break;
default:
output = "Grade: <ERROR> \nRemark: Grade
could not be generated.";
break;
}
Console.WriteLine(output);
Console.Write("Press any key to exit...");
Console.ReadKey();
}
}
}
References:
if-else (C# Reference) - msdn.microsoft.com
switch (C# Reference) - msdn.microsoft.com
https://www.tutorialspoint.com/csharp/csharp_decision_making.htm
http://www.infocodify.com/csharp/csharp_switch_statement
http://www.infocodify.com/csharp/csharp_conditional_statements