C# Examples

C# Example Code


C# Decision Making

In C#, various types of decision making statements are available such as if..else, if..else..if, switch etc. Each statement is used to evaluate the specific test. If tests are determined to be true, specific statement will be execued for example : if(a > b) statement1 else statement2. Sometimes when develpoing program that requires to take the decision to execute specific part of program, decision making statement helps to do so.

If Statement

Sometimes we reqiure to execute certain statement when specified condition true.

Syntax

if (condition)
{
   //code that will run if the condition is true
}

If Statement Example

using System;
class DataTypeDemo
{
	static void main()
	{
		int a, b;
		a = 10;
		b = 20;
		if (b > a)
		{
		   Console.WriteLine("b is greater than a");
		}
	}
}

If..Else Statement

When specified condition fail the else statement will get execute.

Syntax

if (condition)
{
   //code that will run if the condition is true
}
else
{
  //code that will run if the condition is false
}

If..Else Statement Example

using System;
class IfStatementDemo
{
	static void main()
	{
		int a, b;
		a = 10;
		b = 20;
		if (a > b)
		{
		   Console.WriteLine("a is greater than b");
		}
		else
		{
			Console.WriteLine("a is less than b");
		}
	}
}

If..Else..If Statement

When there are situations when we need to test several conditions then we will use else if condition.

Syntax

if (condition1)
{
   //code that will run if the condition1 is true
}
else if(condition2)
{
  //code that will run if the condition2 is true
}
else if(condition3)
{
  //code that will run if the condition3 is true
}
else
{
  //code that will run if the all contitions fails
}

If..Else..If Statement Example

using System;
class IfStatementDemo
{
	static void main()
	{
		int a, b;
		a = 10;
		b = 20;
		if (a > b)
		{
		   Console.WriteLine("a is greater than b");
		}
		else if (b > a)
		{
			Console.WriteLine("b is greater than a");
		}
		else
		{
			Console.WriteLine("a and b are equal");
		}
	}
}

Switch Case Statement

In C# switch acts like a multiple if / else if / else chain. Checks a value against a list of cases, and executes the first case that is true. If no matching case found, it executes the default case. The break(optional) statements with case indicate to the interpreter to end the particular case.

Syntax

switch (expression) {
  case case1:
    statements1
    [break;]
  case case2:
    statements2
    [break;]
  case case3:
    statements3
    [break;]
  default:
    default statements
    [break;]
}

Switch Case Statement Example 1

using System;
class SwitchDemo
{
	static void main()
	{
		string book = "Commerce";
		switch (book) {
		  case "English":
			Console.WriteLine("English book price is $12.");
			break;
		  case "Math":
			Console.WriteLine("Math book price is $22.");
			break;
		  case "Commerce":
			Console.WriteLine("Commerce book price is $12.");
			break;
		  case "History":
			Console.WriteLine("History book price is $125.");
			break;
		  case "Physics":
			Console.WriteLine("Physics book price is $12.99.");
			break;
		  default:
			Console.WriteLine("Sorry, book out of stock");
		}
	}
}

If forget a break which is optional than code will run from the case where condition is met, and will run the case after that regardless if condition was met.

Switch Case Statement With Break Example 2

using System;
class SwitchDemo
{
	static void main()
	{
		double salary = 34000;
		switch (salary)
		{
		  case 10000: 
			Console.WriteLine("Assistant");
			break;
		  case 20000: 
			Console.WriteLine("Assistant Manager");
			break;
		  case 30000: 
			Console.WriteLine("Relationship Manager");
			break;
		  case 40000: 
		  case 50000: 
			Console.WriteLine("Manager");
			break;
		  default:  
			Console.WriteLine("Unknown post");
		}
	}
}