Curriculum
Conditional Statements are one of the most important programming concepts used to make decisions within software applications. Conditional Statements allow programs to execute different blocks of code based on specific conditions. Every .NET developer must understand Conditional Statements because they are widely used in C#, ASP.NET Core, MVC applications, Web APIs, Entity Framework, and enterprise software development.
In real-world applications, Conditional Statements help determine what action should be performed depending on user input, business rules, or system conditions.
Conditional Statements are programming constructs that allow a program to make decisions based on whether a condition evaluates to True or False.
Conditional Statements help software behave intelligently by choosing different execution paths.
Example:
int age = 20;
if(age >= 18)
{
Console.WriteLine("Eligible for Voting");
}
In this example, the program checks a condition and executes code only when the condition is true.
Conditional Statements help developers:
Without Conditional Statements, applications would execute the same instructions every time without adapting to different situations.
Conditional Statements evaluate expressions that return:
Example:
int marks = 75;
if(marks >= 40)
{
Console.WriteLine("Pass");
}
Condition:
marks >= 40
Result:
True
Output:
Pass
Most programming languages support several types of Conditional Statements.
The if statement executes code only when a condition is true.
Syntax:
if(condition)
{
// code
}
Example:
int age = 20;
if(age >= 18)
{
Console.WriteLine("Adult");
}
Output:
Adult
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
This structure allows multiple conditions to be evaluated.
Example:
int marks = 85;
if(marks >= 90)
{
Console.WriteLine("Grade A+");
}
else if(marks >= 75)
{
Console.WriteLine("Grade A");
}
else if(marks >= 60)
{
Console.WriteLine("Grade B");
}
else
{
Console.WriteLine("Grade C");
}
Output:
Grade A
The program evaluates conditions from top to bottom and executes the first matching condition.
A Nested if Statement contains another if statement inside an 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 multiple fixed options need to be checked.
Syntax:
switch(variable)
{
case value1:
// code
break;
case value2:
// code
break;
default:
// code
break;
}
Example:
int day = 3;
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:
Wednesday
Switch Statements improve readability when handling multiple options.
The Ternary Operator provides a shorthand way of writing simple Conditional Statements.
Syntax:
condition ? value1 : value2;
Example:
int marks = 60;
string result = marks >= 40 ? "Pass" : "Fail";
Console.WriteLine(result);
Output:
Pass
The Ternary Operator is useful for simple decision-making.
Conditional Statements often use comparison operators.
| Operator | Description |
|---|---|
| == | Equal To |
| != | Not Equal To |
| > | Greater Than |
| < | Less Than |
| >= | Greater Than or Equal To |
| <= | Less Than or Equal To |
Example:
if(age >= 18)
{
Console.WriteLine("Eligible");
}
Logical Operators combine multiple conditions.
Both conditions must be true.
Example:
if(age >= 18 && marks >= 60)
{
Console.WriteLine("Eligible");
}
At least one condition must be true.
Example:
if(age >= 18 || marks >= 60)
{
Console.WriteLine("Eligible");
}
Reverses the condition.
Example:
if(!isBlocked)
{
Console.WriteLine("Access Granted");
}
Example:
if(balance >= withdrawalAmount)
{
Console.WriteLine("Transaction Approved");
}
else
{
Console.WriteLine("Insufficient Balance");
}
Example:
if(username == "admin" && password == "123")
{
Console.WriteLine("Login Successful");
}
else
{
Console.WriteLine("Invalid Credentials");
}
Example:
if(cartTotal >= 5000)
{
Console.WriteLine("Free Shipping");
}
Example:
if(marks >= 40)
{
Console.WriteLine("Pass");
}
else
{
Console.WriteLine("Fail");
}
Conditional Statements are heavily used in:
Understanding Conditional Statements is essential for building intelligent and responsive .NET applications.
Conditional Statements are used to make decisions and execute code based on conditions.
The if statement executes code only when a condition is true, while if-else provides an alternative action when the condition is false.
A Switch Statement is useful when checking multiple fixed values for the same variable.
A Nested if Statement contains another if statement inside an existing if block.
The Ternary Operator is a shorthand way of writing simple conditional expressions.
Conditional Statements are used in authentication, validation, business rules, workflows, APIs, and enterprise applications.
WhatsApp us