CSharp Decision Making

In CSharp, 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
}

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
}

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
}

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

In CSharp 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;]
}

Example1

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.

Example2

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");
		}
	}
}


 
SHARE THIS IF YOU LIKE!
submit to reddit

SUBSCRIBE FOR LATEST UPDATES
HAVE WE CONNECTED ON FACEBOOK?
TOP TUTORIALSTOP CODE EXAMPLESTOP LIVE DEMOLINKSSTAY CONNECTED
HTML Tutorial
HTML5 Tutorial
Bootstrap3 Tutorial
Javascript Tutorial
TypeScript Tutorial
AngularJS Tutorial
CSharp Tutorial
PHP Tutorial
Developer Tools
Javascript
AngularJS
Google MAP API V3
ASP.NET
Javascript
AngularJS
Google MAP API V3
Contact Us
Advertise with Us
Privacy Policy
Disclaimer
Stay connected. We actually like you. If you want to get up to the minute updates, then connect with us on your fav below.
 
Twitter   Linkedin   Facebook   Google+    RSS   Pinterest
 
Protected by Copyscape Original Content Checker
 
 
Copyright ©2017 www.techstrikers.com Unauthorized reproduction/replication of any part of this site is prohibited.