Search Placementyogi.com
 

Decision Control in C#

The common decision control in c# are:

1. If Else control

2. Nested If 

3. If Else If Ladder

Lets discuss each of these in detail with some real world Examples!

If Else Control Statement in C#

Syntax:

if(condition)
{
	----;
	statements set1; // Compound statements
	----;
}
else
{
	----;
	statements set2;
	----;
}

If the condition Evaluates to True the set1 statements are EXECUTED  and set2 are NOT executed.

And if the statement is False, the statements set1 are NOT executed and set2 inside the else block are EXECUTED.

Single Statement:

If there is only one statement inside the if or else block the braces "{ }" can be removed.

Syntax: 

if (condition) 
	statement1;
else
	statement2;

Note 1: The Condition has to be a boolean Expression.

Note 2: Multiple Statements are technically called Compound Statements.

Example of If in C#.NET:

class IfExample
{
	public static void main(String[] args) 
	{
		int var1=0;
		if (var1 < 10)
		{
			Console.WriteLine("Inside If Condition");
			Console.WriteLine("2nd statement inside If Condition");
         	}
		else
		{
			Console.WriteLine("Inside Else Condition");			
                        Console.WriteLine("2nd statement inside Else Condition");
                }
		
	}
}

For Single Statement the If block could be written as:

if (var1 < 10)
   	Console.WriteLine("Inside If Condition");
        else
	Console.WriteLine("Inside Else Condition");

Be careful with these single statements:

int var1=10;
if (var1 != 10)
	Console.WriteLine("Statement1");
        Console.WriteLine("Statement2 Not under if block");

Here only "statement1" is under if block,  the Statement2 is not under if block and is executed irrespective of the result of the condition var1!=10.

Nested If in C#

A nested if means placing one or more if statements inside another if statement.

Syntax of Nested If in C#

if(condition1)
{
	//statementset1
	if(condition2)
	{	
		//statementset2
	}	
	else // executed if condition3 is false
	{
		//statementset3
	}
}
else     // executed if condition1 is false
{
	// statementset4
}

If Condition1 is true execute statementset1 and then check if condition2 is true if yes execute statementset2 also, else execute statementset3.

If Condition1 is false execute statementset4.

Example of Nested If in C#

int i=10,j=20;
if(i==10)
{
	if(j==20)
	{
		Console.WriteLine("i=10 and y=20");
	}
}

i=10 and y=20 is printed only if both the if condition evaluates to true.

if-else-if Ladder in C#

This decision control structure is used when you have multiple conditions and you have to execute the corresponding statements based on which condition is true.

Syntax:

if(boolean_expression1)
{
	//... StatementSet1
}
else if (boolean_expression2)
{
	// StatementSet2
}
else if (boolean_expression3)
{
	// StatementSet3
}
// any number of else if
else
{
	//Last StatementSet
}

Example:

int a =10;
if( a>10)
	Console.WriteLine("a>10");
else if( a>20)
	Console.WriteLine("a>20");
else if(a<10)
	Console.WriteLine("a<10");
else 
	Console.WriteLine("No condition matched!");

Real world Example: 

Lets say you are alone at your home and you feel hungry, you go to the kitchen to see if there is anything available to eat.

Here is the thought process:

if( there is bread and butter)
{
	spread the butter on bread 
	eat the bread
}
else if ( there is oats and milk)
{
	eat oats and milk
}
else if ( there is maggie )
	make maggie and eat it

The conditions are written in the preference order and if any condition matches the rest of the ladder is skipped!

If you find bread and butter, your search just ends! you do not go and search for oats or maggie after that.

Banner to LogicGuns.com

Hide Page Information

If Else in C#.NET | Example and Syntax with Explanation

This tutorial will help freshers to understand the meaning of if-else in c#.net with real world example and explanation along with programs that will help beginners in c# to grasp the concept of if-else in c#.