Login

Username:

Password: 

Join us Now |  Forgot Password? | Forgot UserName?

C#

Learn step by step



Decision Making

Any program that does any real work will have to make decisions. These decisions are based on meeting certain conditions or rather, running code only some of the time, when a particular condition or state is met. The central piece of decision making is a special C# statement called an if-statement. We'll start by looking at the if-statement, along with a few related statements, then we'll look at a variety of ways to compare two values. We'll then look at a few other special operators called logical operators (not nearly as scary as it sounds, I promise!) that help us make more sophisticated conditions.

The if-statement

An if statement consists of a boolean expression followed by one or more statements.


int mobileNumber=1234567890;
if(mobileNumber == 1234567890)
{
Console.WriteLine("Is This Ramesh");
}

output

Is This Ramesh
If-Else statement

An if-else statement consists of two statements if statement and else statement. When the expression in an if-statement is evaluated to true then if block is executed otherwise the else block would be executed.


int Number=123;
if(Number == 1234567890)
{
Console.WriteLine("Right number");
} else { Console.WriteLine("Wrong number");
}

output

Right number
If-Else-If statement or ladder

The If-Else-If ladder is a set of statements that is used to test a series of conditions. If the first if statement meet the result then code within the if block executes. If not, control passes to the else statement, which contains a second "if" statement. If second one meet the result then code within the if block executes. This continues as a series of else if statements. A default else code block may execute when no condition has been evaluated to true.


public class Example
{
static void Main(string[] args)
{
int day = 1;

if (day== 1)
{
Console.WriteLine("Sunday");
}
else if (day == 2)
{
Console.WriteLine("Monday");
}
else if (day == 3)
{
Console.WriteLine("Tuesday");
}
else if (day == 4)
{
Console.WriteLine("Thursday");
}
else
{
Console.WriteLine("Friday");
}
}
}

output

Sunday
Switch statement

A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each switch case.


public class Example
{
static void Main(string[] args)
{
char grade = 'B';

switch (grade)
{
case 'A':
Console.WriteLine("Excellent!");
break;
case 'B':
case 'C':
Console.WriteLine("Very Good");
break;
case 'D':
Console.WriteLine("You passed");
break;
case 'F':
Console.WriteLine("Try again");
break;
default:
Console.WriteLine("You are Failed!");
break;
}
}
}

output

Very Good
The ? : Operator:

We have covered conditional operator ? : in previous chapter which can be used to replace if...else statements. It has the following general form:
Exp1 ? Exp2 : Exp3;
Where Exp1, Exp2, and Exp3 are expressions. Notice the use and placement of the colon.



Related Videos