Skip to main content

How to Use an if Statement in C#

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Programs make decisions: warn when it's hot, stop when an order isn't paid. In C#, if runs a block of code only when its condition is true, and else if and else cover the other cases.

Syntax​

Syntax
if (<condition>)
{
<runs when the condition is true>
}
else if (<otherCondition>)
{
<runs when the first is false and this one is true>
}
else
{
<runs when none were true>
}
PartWhat it does
if (<condition>)Tests a bool condition. When it's true, its block runs and the rest is skipped.
else if (<otherCondition>)Optional, and you can have several. Tested only when everything above was false.
elseOptional. Runs when no condition was true.

Microsoft's selection statements reference: "The if statement executes a statement only if a provided Boolean expression evaluates to true."

The Example​

The example describes the weather from a temperature: hot above 30, warm above 20, cool otherwise. The temperature is 24.

Prerequisites​

  • The .NET 10 SDK or newer, which runs a single .cs file with dotnet run. This guide uses .NET SDK 10.0.404.

Step 1: Write the Code​

Create a file named main.cs in an empty folder:

main.cs, which sets temperature to 24 and prints It is hot. when it is above 30, It is warm. when it is above 20, and It is cool. otherwise.

main.cs
var temperature = 24;

if (temperature > 30)
{
Console.WriteLine("It is hot.");
}
else if (temperature > 20)
{
Console.WriteLine("It is warm.");
}
else
{
Console.WriteLine("It is cool.");
}

Step 2: Run It​

Terminal
dotnet run main.cs
Output
It is warm.

A zsh terminal where dotnet run main.cs prints It is warm.

24 > 30 was false, so C# moved on; 24 > 20 was true, so it printed It is warm. and skipped the else. The command is the same in zsh, bash and PowerShell.

Comparing and Negating​

OperatorMeansExample that's true
==Equal5 == 5
!=Not equal5 != 6
> and <Greater than, less than24 > 20
>= and <=Greater or equal, less or equal20 >= 20
!Not: turns true into false!false

! reads naturally in conditions such as if (!allPaid), "if not all paid".

Common Mistakes​

MistakeWhat happensDo this instead
Writing = instead of ==, as in if (t = 30)error CS0029: Cannot implicitly convert type 'int' to 'bool'Use == to compare
Ordering conditions from small to large, such as > 20 before > 30> 20 catches 35 first, so hot is never printedPut the narrowest condition first
Leaving out the braces and adding a second line laterOnly the first line belongs to the ifAlways use braces

Troubleshooting​

SymptomLikely causeFix
CS0029 on an if lineAn assignment in the conditionChange = to ==
A branch you expected never runsA condition above it already matchedReorder the conditions

FAQ​

Does C# treat numbers or strings as true or false?​

No. The condition must be a bool. That's why if (t = 30) doesn't compile in C#, while some other languages accept it.

Is there a switch statement?​

Yes. switch compares one value against several cases, which can be tidier than a long else if chain.

Conclusion​

if runs a block when its bool condition is true, else if tries the next condition, and else catches the rest. Compare with ==, negate with ! and put the narrowest condition first.

Additional Resources​