if...else

Using the if statement is simple and easy in a scenario where the user wants to execute a specific code block when a condition is met. C# provides us with widely used if statements that allow us to achieve the desired functionality.

If (true) then-statements Else (false) else-statements. The following is the general syntax of the If / Else statement:

If(Boolean expression)
{
Then statements; //these are executed when Boolean expression is true
}
Else
{
Else statements; //these are executed when Boolean expression is false
}

When the Boolean expression evaluates to true, then-statements are executed, and when the Boolean expression evaluates to false, else-statements are executed. When the Boolean expression evaluates to either true or false, the program allows you to execute single or multiple statements. However, multiple statements need to be enclosed in curly braces, {}. This will ensure that all the statements are executed in one context and in sequence. This is also called a code block. For single statements, these braces are optional, but they are recommended from a code readability point of view. Also, we need to understand that the scope of the variables is limited to the code block they were defined in.

The else statement is optional. If this is not provided, the program evaluates the Boolean expression and executes the then-statement. At any given time, either the then-statements or the else-statements of an if-else statement will be executed.

Let's look at a few examples. In the following code block, we have already set the condition variable to true, so when the Boolean expression in the if statement is evaluated, it returns true and the code block (then-statement) is executed. Else-statement is ignored:

bool condition = true;
if (condition)
{
Console.WriteLine("Then-Statement executed");
}
else
{
Console.WriteLine("Else-Statement executed");
}
//output: Then-Statement executed

In the following scenario, if the statement doesn't include the else part, when the Boolean expression is evaluated to true, then-statements is executed by default:

if (condition) 
{
Console.WriteLine("Then-Statement without an Else executed");
}
//output: Then-Statement without an Else executed

C# also allows nested if and nested else statements. In the following code, we will see how nested if statements can be used in a program.

When condition 1 is evaluated to true, by default, the then-statements of condition 1 are executed. Similarly, when condition 2 is evaluated to true, the then-statements of condition 2 are executed:

int variable1 = 15;
int variable2 = 10;


if (variable1 > 10)//Condition 1
{
Console.WriteLine("Then-Statement of condition 1 executed");
if (variable2 < 15) //Condition 2
{
Console.WriteLine("Then-Statement of condition 2 executed");
}
else
{
Console.WriteLine("Else-Statement of condition 2 executed");
}
}
else
{
Console.WriteLine("Then-Statement condition 1 executed");
}
//Output:
Then-Statement of condition 1 executed
Then-Statement of condition 2 executed

We can also define a nested if in an Else statement. For example, the user wants to find out whether the character that was entered was a vowel and, if so, to print it. The following code block illustrates how multiple if statements can be used. The program checks if the entered character is a vowel or not and prints the results:

Console.Write("Enter a character: ");
char ch = (char)Console.Read();
if (ch.Equals('a'))
{
Console.WriteLine("The character entered is a vowel and it is 'a'.");
}
else if (ch.Equals('e'))
{
Console.WriteLine("The character entered is a vowel and it is 'e'.");
}
else if (ch.Equals('i'))
{
Console.WriteLine("The character entered is a vowel and it is 'i'.");
}
else if (ch.Equals('o'))
{
Console.WriteLine("The character entered is a vowel and it is 'o'.");
}
else if (ch.Equals('u'))
{
Console.WriteLine("The character entered is a vowel and it is 'u'.");
}
else
{
Console.WriteLine("The character entered is not vowel. It is:" + ch );
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.2.225