GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||
GeezerVison is showing www.tutorialspoint.com — C# - Decision Making
Selected Reading
C# - Decision MakingDecision-making StatementsDecision making structures requires the programmer to specify one or more conditions to be evaluated or tested by the program, along with a statement or statements to be executed if the condition is determined to be true, and optionally, other statements to be executed if the condition is determined to be false. Following is the general form of a typical decision making structure found in most of the programming languages −
Types of Decision-making StatementsC# provides the following types of decision-making statements. Click the following links to check their detail −
The if StatementAn if statement consists of a boolean expression followed by one or more statements. ExampleThe following example demonstrates the use of a simple if statement in C#: using System;
namespace DecisionMaking
{
class Program
{
static void Main(string[] args)
{
// Define a local variable
int temperature = 30;
// Check the condition using an if statement
if (temperature > 25)
{
Console.WriteLine("It's a hot day.");
}
Console.WriteLine($"Current temperature: {temperature} degree C");
}
}
}
When the above code is compiled and executed, it produces the following result − It's a hot day. Current temperature: 30 degree C The if else StatementAn if statement can be followed by an optional else statement, which executes when the boolean expression is false. ExampleThe following example demonstrates the use of an if else statement in C#: using System;
namespace DecisionMaking
{
class Program
{
static void Main(string[] args)
{
// Define a local variable
int temperature = 21;
// Check the condition using if-else statement
if (temperature > 25)
{
Console.WriteLine("It's a hot day.");
}
else
{
Console.WriteLine("The weather is pleasant.");
}
Console.WriteLine($"Current temperature: {temperature} degree C");
}
}
}
When the above code is compiled and executed, it produces the following result − The weather is pleasant. Current temperature: 21 degree C Nested if StatementsIt is always legal in C# to nest if-else statements, which means you can use one if or else if statement inside another if or else if statement(s). ExampleThe following example demonstrates the use of nested if statements in C#: using System;
namespace DecisionMaking
{
class Program
{
static void Main(string[] args)
{
// Define a local variable
int temperature = 30;
// Check the condition using nested if statements
if (temperature > 0)
{
if (temperature < 15)
{
Console.WriteLine("It's a cold day.");
}
else if (temperature <= 15 && temperature >= 25)
{
Console.WriteLine("The weather is pleasant.");
}
else
{
Console.WriteLine("It's a hot day.");
}
}
else
{
Console.WriteLine("It's freezing outside!");
}
Console.WriteLine($"Current temperature: {temperature} degree C");
}
}
}
When the above code is compiled and executed, it produces the following result − It's a hot day. Current temperature: 30 degree C The switch StatementA 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. ExampleThe following example demonstrates the use of nested if statements in C#: using System;
namespace DecisionMaking
{
class Program
{
static void Main(string[] args)
{
int day = 3;
switch (day)
{
case 1:
Console.WriteLine("Sunday");
break;
case 2:
Console.WriteLine("Monday");
break;
case 3:
Console.WriteLine("Tuesday");
break;
case 4:
Console.WriteLine("Wednesday");
break;
case 5:
Console.WriteLine("Thursday");
break;
case 6:
Console.WriteLine("Friday");
break;
case 7:
Console.WriteLine("Saturday");
break;
default:
Console.WriteLine("Invalid day number! Please enter a number between 1 and 7.");
break;
}
}
}
}
When the above code is compiled and executed, it produces the following result − Tuesday The ? : OperatorWe 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. The value of a Advertisements
| ||||||||||||
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |