Summary: in this tutorial, you’ll learn how to use the C# if statement to execute a code block based on a condition.
Introduction to the C# if statement
The if statement evaluates a condition and executes one or more statements if the result is true. Otherwise, the if statement passes the control to the statement after it.
The following illustrates the syntax of the if statement:
if (condition)
statement;Code language: C# (cs)In this syntax, if the condition evaluates to true, then if statement executes the statement.
If you want to execute multiple statements, you need to use a block like this:
if (expression) {
// statements
}Code language: C# (cs)However, it’s a good practice to always use a block with the if statement even though it has a simple statement.
The following flowchart illustrates how the C# if statement works:
C# if statement examples
Let’s take some examples of using the if statement.
1) A simple C# if statement example
The following example uses the if statement to show a message when the condition is sunny:
string condition = "sunny";
if (condition == "sunny")
{
Console.WriteLine("Let's go outside.");
}Code language: C# (cs)Output:
Let's go outside.Code language: C# (cs)How it works.
- First, declare a string variable
conditionwith the initial value"sunny". - Second, check if the
conditionvariable equals"sunny"and display the message"Let's go outside".
2) C# if statement with condition evaluates to false
The following example doesn’t output anything because the condition in the if statement evaluates to false:
string condition = "sunny";
if (condition == "rainy")
{
Console.WriteLine("Stay home");
}Code language: C# (cs)3) C# if statement example with a complex condition
In practice, the condition is more complex, which consists of multiple expressions with operators like this:
string condition = "sunny";
bool free = true;
if (condition == "sunny" && free)
{
Console.WriteLine("Let's go outside.");
}Code language: C# (cs)Output:
Let's go outside.Code language: C# (cs)Nested C# if statement
C# allows you to nest if statements inside an if statement. The following example illustrates how to nest if statements inside another if statement:
string condition = "rainy";
bool free = true;
if (free)
{
if (condition== "sunny")
{
Console.WriteLine("Let's go outside.");
}
if (condition == "rainy")
{
Console.WriteLine("Just stay home.");
}
}Code language: C# (cs)Output:
Just stay home.Code language: C# (cs)How it works.
- First, declare the
conditionandfreevariables and initialize their values to"sunny"andtruerespectively. - Second, check if the
freeistruein theifstatement. Since thefreeistrue, theifstatement executes the statement inside its block. - Third, check if the
conditionis"sunny"in the first nestedifstatement. Because theconditionis"rainy", the first nestedifstatement does nothing. - Finally, check if the
conditionis"rainy"in the second nestedifstatment. Since theconditionis"rainy", theifstatement outputs the message"Just stay home."to the console.
In practice, you should avoid the nesting of the if statements as much as possible. Otherwise, the code will become difficult to read.
For example, you can flatten the example above by using two if statements as follows:
string condition = "rainy";
bool free = true;
if (free && condition == "sunny")
{
Console.WriteLine("Let's go outside.");
}
if (free && condition == "rainy")
{
Console.WriteLine("Just stay home.");
}Code language: C# (cs)Summary
- Use the C#
ifstatement to execute one or more statements when a condition istrue. - Avoid nesting
ifstatements as much as possible to make the code more readable.