Conditional statements in C# are used to perform different actions based on different conditions. They allow your program to make decisions and execute code based on whether certain conditions are true or false. This tutorial covers various types of conditional statements in C#, including `if`, `else`, `else if`, `switch`, and the ternary operator.
Conditional Statement | Description |
---|---|
if |
Evaluates a boolean expression and executes the code block inside it if the expression is true. |
else |
Provides an alternative code block that executes if the boolean expression in the if statement is false. |
else if |
Specifies a new condition to test if the previous if or else if condition is false. |
switch |
Selects one of many code blocks to be executed based on the value of a variable or expression. |
Ternary Operator (? : ) |
Shorthand for an if-else statement that returns one of two values based on a boolean expression. |
Relational operators are used to compare two values. They return a boolean result indicating whether the comparison is true or false.
Operator | Description | Example |
---|---|---|
== |
Equal to | a == b |
!= |
Not equal to | a != b |
> |
Greater than | a > b |
< |
Less than | a < b |
>= |
Greater than or equal to | a >= b |
<= |
Less than or equal to | a <= b |
The `if` statement evaluates a boolean expression and executes the code block inside it if the expression is true.
Basic Example:
namespace ConditionalStatements;
class Program
{
static void Main(string[] args)
{
int number = 10;
if (number > 5)
Console.WriteLine("The number is greater than 5.");
}
}
namespace ConditionalStatements;
class Program
{
static void Main(string[] args)
{
int number = 10;
if (number > 5)
Console.WriteLine("The number is greater than 5.");
if (number > 8)
Console.WriteLine("The number is also greater than 8.");
if (number > 15)
Console.WriteLine("This will not print because the number is not greater than 15.");
}
}
The `if-else` statement provides an alternative code block that executes if the boolean expression is false.
Basic Example:
namespace ConditionalStatements;
class Program
{
static void Main(string[] args)
{
int number = 10;
if (number > 15)
Console.WriteLine("The number is greater than 15.");
else
Console.WriteLine("The number is not greater than 15.");
}
}
namespace ConditionalStatements;
class Program
{
static void Main(string[] args)
{
int number = 10;
if (number > 15)
Console.WriteLine("The number is greater than 15.");
else
Console.WriteLine("The number is not greater than 15.");
string result = number % 2 == 0 ? "even" : "odd";
Console.WriteLine($"The number is {result}.");
}
}
The `else-if` statement is used to specify a new condition to test if the first condition is false.
Basic Example:
namespace ConditionalStatements;
class Program
{
static void Main(string[] args)
{
int number = 10;
if (number > 15)
Console.WriteLine("The number is greater than 15.");
else if (number > 5)
Console.WriteLine("The number is greater than 5 but less than or equal to 15.");
else
Console.WriteLine("The number is 5 or less.");
}
}
namespace ConditionalStatements;
class Program
{
static void Main(string[] args)
{
int number = 10;
if (number > 15)
Console.WriteLine("The number is greater than 15.");
else if (number > 10)
Console.WriteLine("The number is greater than 10 but less than or equal to 15.");
else if (number > 5)
Console.WriteLine("The number is greater than 5 but less than or equal to 10.");
else
Console.WriteLine("The number is 5 or less.");
}
}
The `switch` statement selects one of many code blocks to be executed based on the value of a variable or expression.
Basic Example:
namespace ConditionalStatements;
class Program
{
static void Main(string[] args)
{
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;
case 6:
Console.WriteLine("Saturday");
break;
case 7:
Console.WriteLine("Sunday");
break;
default:
Console.WriteLine("Invalid day");
break;
}
}
}
namespace ConditionalStatements;
class Program
{
static void Main(string[] args)
{
int month = 7;
switch (month)
{
case 1:
case 2:
case 12:
Console.WriteLine("Winter");
break;
case 3:
case 4:
case 5:
Console.WriteLine("Spring");
break;
case 6:
case 7:
case 8:
Console.WriteLine("Summer");
break;
case 9:
case 10:
case 11:
Console.WriteLine("Autumn");
break;
default:
Console.WriteLine("Invalid month");
break;
}
}
}
The ternary operator `? :` is a shorthand for an `if-else` statement that returns one of two values based on a boolean expression.
Basic Example:
namespace ConditionalStatements;
class Program
{
static void Main(string[] args)
{
int a = 10;
int b = 20;
// Basic usage of ternary operator
string result = (a > b) ? "a is greater than b" : "a is less than or equal to b";
Console.WriteLine(result);
}
}
namespace ConditionalStatements;
class Program
{
static void Main(string[] args)
{
int a = 10;
int b = 20;
int c = 5;
// Nested conditional operators
string result = (a > b) ? "a is greater than b" :
(b > c) ? "b is greater than c" :
"c is the greatest";
Console.WriteLine(result);
}
}
Understanding and using conditional statements effectively is crucial for controlling the flow of your C# programs. This tutorial covered various types of conditional statements, including `if`, `else`, `else if`, `switch`, and the ternary operator, along with best practices for using them.