codescracker


c#

C# Decision Making



« Previous Tutorial Next Tutorial »


Using decision making statements, you can control your C# program accordingly. There are the following decision making statements available in C# programming:

if statement in C#

An if statement in C#, consists of a boolean expression, followed by one/more statement(s). Here is the general form to use if statement in C#:

if(boolean-expression)
{
	statement(s);
}

Here, if the boolean-expression evaluates to be true, then to control go to the inner part of the if statement to execute all the statement of the if statement. And if the boolean-expression evaluates to be false, then control doesn't goes to the inner part of the if statement.

Example

Here is an example program, illustrates the use of if statement in C# programming:

/* C# Decision Making - The if Statement - Example Program */
		
using System;
namespace IfStatement
{
	class IfClass
	{
		static void Main(string[] args)
		{
			int num=20;
			if(num<50)
			{
				Console.WriteLine("num is less than 50");
			}
			Console.WriteLine("Value of num = {0}", num);
			Console.ReadLine();
		}
	}
}

Here is the output produced by the above C# program:

num is less than 50
Value of num = 20

If you give a value to the variable num, which is greater than 50, then the program control doesn't go to the inner part of the if statement. Let's look at the following C# program, for the complete understanding on if statement in C#:

/* C# Decision Making - The if Statement - Example Program */
		
using System;
namespace IfStatement
{
	class IfClass
	{
		static void Main(string[] args)
		{
			int num=100;
			if(num<50)
			{
				Console.WriteLine("num is less than 50");
			}
			Console.WriteLine("Value of num = {0}", num);
			Console.ReadLine();
		}
	}
}

Here is the output produced by the above C# program:

Value of num = 100

if-else statement in C#

An if-else statement in C#, is similar to the if statement except, here, there is an else statement, used to execute statement(s) if the boolean-expression evaluates to false. Here is the general form to use if-else statement in C#:

if(boolean-expression)
{
	statement(s);
}
else
{
	statement(s);
}

Here, if the boolean-expression evaluates to true, then the if's statement(s) will be executed. And when the boolean-expression evaluates to false, then else's statement(s) will be executed.

Example

Here is an example program, demonstrating the if-else statement in C#:

/* C# Decision Making - The if-else Statement - Example Program */
		
using System;
namespace IfElseStatement
{
	class IfElseClass
	{
		static void Main(string[] args)
		{
			int num=20;
			if(num<50)
			{
				Console.WriteLine("num is less than 50");
			}
			else
			{
				Console.WriteLine("num is greater than 50");
			}
			Console.WriteLine("Value of num = {0}", num);
			Console.ReadLine();
		}
	}
}

Here is the output produced by the above C# program:

num is less than 50
Value of num = 20

Now, let's give a value to the variable num, which is greater than 50. Then the boolean-expression would evaluate as false, and the else's statement will be executed. Here is the program demonstrating this concept:

/* C# Decision Making - The if-else Statement - Example Program */
		
using System;
namespace IfElseStatement
{
	class IfElseClass
	{
		static void Main(string[] args)
		{
			int num=100;
			if(num<50)
			{
				Console.WriteLine("num is less than 50");
			}
			else
			{
				Console.WriteLine("num is greater than 50");
			}
			Console.WriteLine("Value of num = {0}", num);
			Console.ReadLine();
		}
	}
}

Here is the output produced by the above C# program:

num is greater than 50
Value of num = 20

nested if statement in C#

You are free to use one if/if-else statement inside another if/if-else statement in C# programming. Here is the general form to nest one if or if-else statement inside another if or if-else statement:

if(boolean-expression-1)
{
	if(boolean-expression-2)
	{
		if(boolean-expression-3)
		{
			statement(s);
		}
		else
		{
			statement(s);
		}
	}
	else
	{
		if(boolean-expression-4)
		{
			statement(s);
		}
		else
		{
			statement(s);
		}
	}
}
else
{
	statement(s);
}

switch statement in C#

A switch statement in C#, simply allows a variable to be tested for the equality against a list of values. Here each values are called as a case. Here is the general form to use switch statement in C# programming:

switch(expression)
{
	case constant1:
		statement(s);
		break;
	case constant2:
		statement(s);
		break;
	case constant3:
		statement(s);
		break;
	.
	.
	.
	case constantN:
		statement(s);
		break;
	default:
		statement(s);
}

Example

Here is an example program, illustrates the use of switch statement in C#:

/* C# Decision Making - The switch Statement - Example Program */

using System;
namespace SwitchStatement
{
	class SwitchClass
	{
		static void Main(String[] args)
		{
			char grade='A';
			switch(grade)
			{
				case 'A':
					Console.WriteLine("You got maximum");
					break;
				case 'B':
					Console.WriteLine("You got second");
					break;
				case 'C':
					Console.WriteLine("You got third grade");
					break;
				case 'D':
					Console.WriteLine("Just passed");
					break;
				case 'F':
					Console.WriteLine("Failed");
					break;
				default:
					Console.WriteLine("Wrong grade..!!");
					break;
			}
		}
	}
}

Here is the sample output of the above C# program:

You got maximum

« Previous Tutorial Next Tutorial »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test