Ads block

Banner 728x90px

c#step15


if Statement

C# provides many decision making statements that help the flow of the C# program based on certain logical conditions. C# includes the following decision making statements.
  1. if statement
  2. if-else statement
  3. switch statement
  4. Ternary operator :?
Here, you will learn about the if statements.
Syntax:
if(boolean expression)
{
    // execute this code block if expression evalutes to true
}
The if statement contains boolean expression inside brackets followed by a single or multi line code block. At runtime, if a boolean expression is evalutes to true then the code block will be executed.
Consider the following example where the if condition contains true as an expression.
Example: if condition
if(true)
{
    Console.WriteLine("This will be displayed.");
}

if(false)
{
    Console.WriteLine("This will not be displayed.");
}
As mentioned above, if statement can contain boolean expression. An expression which returns either true or false. Following example uses logical expression as a condition:
Example: if condition
int i = 10, j = 20;

if (i > j)
{
    Console.WriteLine("i is greater than j");
}

if (i < j)
{
    Console.WriteLine("i is less than j");
}        

if (i == j)
{
    Console.WriteLine("i is equal to j");
}   
Output:
i is less than j
In the above example, the boolen expression i < j in the second 'if' statement evalutes to be true, only the second 'if' statement's code block will be executed. The first and third 'if' condition evalutes to false, so their code blocks will not be executed.

if-else Statement

C# also provides for a second part to the if statement, that is else. The else statement must follow if or else if statement. Also, else statement can appear only one time in a if-else statement chain.
Syntax:
if(boolean expression)
{
// execute this code block if expression evalutes to true
}
else
{
// always execute this code block when above if expression is false
}
As you can see in the above syntax, the else stament cannot contain any expression. The code block that follows else statement will always be executed, when the 'if' condition evalutes to be false.
Example: if else
int i = 10, j = 20;

if (i > j)
{
    Console.WriteLine("i is greater than j");
}
else
{
    Console.WriteLine("i is either equal to or less than j");
}
Output:
i is either equal to or less than j

else if Statement

The 'if' statement can also follow an 'else' statement, if you want to check for another condition in the else part.
Example: else if
static void Main(string[] args)
{
    int i = 10, j = 20;

    if (i > j)
    {
        Console.WriteLine("i is greater than j");
    }
    else if (i < j)
    {
        Console.WriteLine("i is less than j");
    }
    else
    {
        Console.WriteLine("i is equal to j");
    }
}
Output:
i is less than j
You can use multiple else-if statements in a single 'if' statment chain. Also, you can remove the curly brackets, when the 'if' block has only one line to execute:
C#- if..else condition:
int i = 10, j = 20;

if (i > j)
    Console.WriteLine("i is greater than j");
else if (i < j)
    Console.WriteLine("i is less than j");
else if (i == j)
    Console.WriteLine("i is equal to j");
Output:
i is less than j

Nested if Statements

C# alows nested if else statements. The nested 'if' statement makes the code more readable.
Example: Nested if statements
int i = 10;

if (i > 0)
{
    if (i <= 100)
    {
        Console.WriteLine("i is positive number less than 100");
    }
    else 
    {
        Console.WriteLine("i is positive number greater than 100");
    }
}
Output:
i is positive number less than 100
The if-else statement can be replaced by ternary operator. Learn about ternary operator in the next section.
 Points to Remember :
  1. if-else statement controls the flow of program based on the evaluation of the boolean expression.
  2. It should start from the if statement followed by else or else-if statements.
  3. Only one else statement is allowed in the if-else chain.
  4. Multiple else-if statements are allowed in a single if-else chain.
  5. Nested if-else statement is allowed.

No comments:

Post a Comment