Curriculum
Conditional Statements in C# are programming constructs that allow applications to make decisions based on specific conditions. Conditional Statements in C# help developers control the flow of execution by determining which code should run depending on whether a condition evaluates to true or false. Every .NET developer uses Conditional Statements in C# extensively while building Console Applications, ASP.NET Core Applications, MVC Projects, Web APIs, Desktop Applications, and Enterprise Software Solutions.
Understanding Conditional Statements in C# is essential because most real-world applications rely on decision-making logic to process user input, validate data, enforce business rules, and control workflows.
Conditional Statements in C# are used to execute different blocks of code based on specified conditions.
Example:
int age = 20;
if(age >= 18)
{
Console.WriteLine("Eligible to Vote");
}
Output:
Eligible to Vote
In this example, the program checks a condition and executes code only if the condition is true.
Conditional Statements in C# help developers:
Without Conditional Statements, applications would execute the same instructions regardless of circumstances.
Conditional Statements evaluate expressions that return:
Example:
int marks = 75;
if(marks >= 40)
{
Console.WriteLine("Pass");
}
Condition:
marks >= 40
Result:
True
Output:
Pass
C# provides several types of Conditional Statements.
The if Statement executes code only when a condition is true.
Syntax:
if(condition)
{
// code
}
Example:
int age = 21;
if(age >= 18)
{
Console.WriteLine("Adult");
}
Output:
Adult
The if Statement is the simplest Conditional Statement in C#.
The if-else Statement provides an alternative action when a condition is false.
Syntax:
if(condition)
{
// code if true
}
else
{
// code if false
}
Example:
int age = 15;
if(age >= 18)
{
Console.WriteLine("Eligible");
}
else
{
Console.WriteLine("Not Eligible");
}
Output:
Not Eligible
The if-else Statement is commonly used in validation and decision-making.
This structure allows multiple conditions to be evaluated.
Example:
int marks = 82;
if(marks >= 90)
{
Console.WriteLine("Grade A+");
}
else if(marks >= 75)
{
Console.WriteLine("Grade A");
}
else if(marks >= 60)
{
Console.WriteLine("Grade B");
}
else if(marks >= 40)
{
Console.WriteLine("Grade C");
}
else
{
Console.WriteLine("Fail");
}
Output:
Grade A
The program executes the first matching condition.
A Nested if Statement contains another if Statement inside an existing if block.
Example:
int age = 25;
bool hasLicense = true;
if(age >= 18)
{
if(hasLicense)
{
Console.WriteLine("Can Drive");
}
}
Output:
Can Drive
Nested if Statements are useful when multiple conditions must be satisfied.
The Switch Statement is used when checking multiple fixed values.
Syntax:
switch(variable)
{
case value1:
// code
break;
case value2:
// code
break;
default:
// code
break;
}
Example:
int day = 2;
switch(day)
{
case 1:
Console.WriteLine("Monday");
break;
case 2:
Console.WriteLine("Tuesday");
break;
case 3:
Console.WriteLine("Wednesday");
break;
default:
Console.WriteLine("Invalid Day");
break;
}
Output:
Tuesday
Switch Statements improve readability when multiple options are available.
Modern C# provides Switch Expressions for concise code.
Example:
int day = 1;
string dayName = day switch
{
1 => "Monday",
2 => "Tuesday",
3 => "Wednesday",
_ => "Invalid Day"
};
Console.WriteLine(dayName);
Output:
Monday
Switch Expressions are widely used in modern .NET applications.
The Ternary Operator provides a short form of the if-else Statement.
Syntax:
condition ? value1 : value2;
Example:
int marks = 65;
string result = marks >= 40 ? "Pass" : "Fail";
Console.WriteLine(result);
Output:
Pass
The Ternary Operator improves code readability for simple conditions.
Conditional Statements often use Logical Operators.
Both conditions must be true.
Example:
int age = 20;
int marks = 75;
if(age >= 18 && marks >= 60)
{
Console.WriteLine("Eligible");
}
Output:
Eligible
At least one condition must be true.
Example:
if(age >= 18 || marks >= 90)
{
Console.WriteLine("Eligible");
}
Output:
Eligible
Reverses the condition.
Example:
bool isBlocked = false;
if(!isBlocked)
{
Console.WriteLine("Access Granted");
}
Output:
Access Granted
if(balance >= withdrawalAmount)
{
Console.WriteLine("Transaction Approved");
}
else
{
Console.WriteLine("Insufficient Balance");
}
if(username == "admin" && password == "123")
{
Console.WriteLine("Login Successful");
}
if(orderAmount >= 5000)
{
Console.WriteLine("Free Shipping");
}
if(marks >= 40)
{
Console.WriteLine("Pass");
}
else
{
Console.WriteLine("Fail");
}
Conditional Statements in C# are used extensively in business applications.
Incorrect:
if(age = 18)
Correct:
if(age == 18)
Avoid deeply nested conditions because they reduce readability.
In Switch Statements, forgetting break can lead to unintended behavior.
Conditional Statements in C# are heavily used in:
A strong understanding of Conditional Statements in C# helps developers build intelligent and responsive applications.
Conditional Statements in C# are used to make decisions based on conditions.
The if Statement executes code only when a condition is true, while if-else provides an alternative path when the condition is false.
A Switch Statement is useful when evaluating multiple fixed values for the same variable.
A Nested if Statement is an if Statement placed inside another if Statement.
The Ternary Operator is a shorthand method for writing simple if-else conditions.
They help implement business rules, validation, workflows, and decision-making logic in software applications.
WhatsApp us