1How Do Conditional Statements Work?
Hi friends, in this article we will learn about conditional statements in C#. A conditional statement allows a program to make decisions based on conditions. A condition is an expression that evaluates to true or false. Based on the result, C# executes the matching block of code. We use conditions in login validation, payment processing, order tracking and many other applications.
2Think About an Online Payment
Imagine you are developing a shopping application. When a customer completes a payment, the application should display a success message. If the payment fails, it should display a failure message.
What if the payment has three possible statuses: success, pending and failed? We need different actions for each status.
Conditional statements help the application choose the correct action instead of executing every statement one after another.
3Understand the Three Decision Structures
1. The if statement
Executes a block only when its condition is true.
if (age >= 18)
{
Console.WriteLine("Eligible");
}
2. The if-else statement
Chooses between two blocks. One executes when the condition is true, and the other executes when it is false.
if (isPaid)
Console.WriteLine("Success");
else
Console.WriteLine("Payment failed");
3. The else-if ladder
Checks multiple conditions in sequence. The first matching block executes.
if (marks >= 90)
Console.WriteLine("A");
else if (marks >= 60)
Console.WriteLine("B");
else
Console.WriteLine("C");
4. The switch statement
Checks one expression against multiple possible values. It is useful when you have several fixed choices.
switch (status)
{
case "Success":
Console.WriteLine("Paid");
break;
default:
Console.WriteLine("Check payment");
break;
}
break statement exits the current switch section.4Build a Payment Status Checker
Let's use if-else to check payment status and switch to select the correct message for the order.
using System;
class Program
{
static void Main()
{
bool isPaid = true;
string status = "Success";
if (isPaid)
{
Console.WriteLine("Payment completed");
}
else
{
Console.WriteLine("Payment pending");
}
switch (status)
{
case "Success":
Console.WriteLine("Order confirmed");
break;
case "Pending":
Console.WriteLine("Order processing");
break;
default:
Console.WriteLine("Check order status");
break;
}
}
}
First, if-else checks whether payment is completed. Then switch checks the status and selects the matching order message.
5See the Program Result
Payment completed Order confirmed
Because isPaid is true, the first if block runs. The status is Success, so the first switch case runs.
6Follow Each Decision in VS Code
Watch the program evaluate the payment condition and then select a matching switch case.
7Keep These Decisions Clear
Conditional statements control the flow of your application. Use if for a single condition, if-else for two alternatives and switch for multiple fixed choices.
- if: Executes code when a condition is true.
- if-else: Chooses between two paths.
- switch: Selects a matching case.
8Questions Interviewers May Ask
What is a conditional statement?
AnswerIt controls program execution based on a condition. The condition evaluates to true or false, and the program executes the appropriate block.
What is the difference between if and if-else?
Answerif executes code only when its condition is true. if-else provides an alternative block when the condition is false.
When should you use switch instead of if-else?
AnswerUse switch when comparing one expression against multiple fixed values. Use if-else for ranges or more complex Boolean conditions.
What is the purpose of break in switch?
Answerbreak exits the switch section after a matching case executes. It prevents execution from continuing into another section.
Can we use multiple conditions in an if statement?
AnswerYes. Use logical operators such as && for AND and || for OR to combine conditions.