Decision-Making Statements in C# Programming
Introduction
Decision-making statements in C# allow the program to make choices and execute specific blocks of code based on certain conditions. The most commonly used decision-making statements are if
, else if
, else
, and switch-case
.
If, Else If, Else Statements
The if
, else if
, and else
statements are used to execute code based on one or more conditions.
Syntax
if (condition) { // Code to execute if the condition is true } else if (another_condition) { // Code to execute if the previous condition is false and this one is true } else { // Code to execute if all conditions are false }
Example: Basic If Statement
using System; class Program { static void Main() { int number = 10; if (number > 5) { Console.WriteLine("The number is greater than 5."); } } }
Output:
The number is greater than 5.
Example: If-Else Statement
using System; class Program { static void Main() { int number = 3; if (number > 5) { Console.WriteLine("The number is greater than 5."); } else { Console.WriteLine("The number is 5 or less."); } } }
Output:
The number is 5 or less.
Example: If-Else If-Else Statement
using System; class Program { static void Main() { int number = 15; if (number > 20) { Console.WriteLine("The number is greater than 20."); } else if (number > 10) { Console.WriteLine("The number is greater than 10 but 20 or less."); } else { Console.WriteLine("The number is 10 or less."); } } }
Output:
The number is greater than 10 but 20 or less.
Switch-Case Statement
The switch-case
statement is used for selecting one block of code from multiple options. It is more efficient than multiple if-else if
statements when dealing with multiple discrete values.
Syntax
switch (expression) { case value1: // Code to execute for value1 break; case value2: // Code to execute for value2 break; default: // Code to execute if no case matches break; }
Example: Switch-Case Statement
using System; class Program { static void Main() { int day = 3; switch (day) { case 1: Console.WriteLine("Monday"); break; case 2: Console.WriteLine("Tuesday"); break; case 3: Console.WriteLine("Wednesday"); break; case 4: Console.WriteLine("Thursday"); break; case 5: Console.WriteLine("Friday"); break; default: Console.WriteLine("Weekend"); break; } } }
Output:
Wednesday
Example: Switch-Case with String
using System; class Program { static void Main() { string fruit = "Apple"; switch (fruit) { case "Apple": Console.WriteLine("You chose Apple."); break; case "Banana": Console.WriteLine("You chose Banana."); break; default: Console.WriteLine("Unknown fruit."); break; } } }
Output:
You chose Apple.
Key Points
- The
if
,else if
, andelse
statements are useful for conditional execution of code based on ranges or complex conditions. - The
switch-case
statement is ideal for selecting among multiple discrete values. - Always include a
default
case inswitch-case
to handle unexpected values.
Conclusion
Decision-making statements like if
, else if
, else
, and switch-case
are essential for writing dynamic and flexible C# programs. Understanding their usage helps in implementing conditional logic effectively.