Skip to content
Tutorials Center
My learning

Topics

C# Reference ยท Keywords and Statements

The if and else statements

Reference for C# if, else if and else: conditions must be bool, scoping of pattern variables, nesting, dangling else and the conditional operator.

if runs a statement only when a condition is true. An optional else runs when the condition is false, and you can chain else if to test several conditions in order. Only the first branch whose condition is true runs; the rest are skipped.

Unlike C or C++, the condition must be a bool expression. Numbers, strings and references are never converted to true or false, so if (count) does not compile. Conditions can also contain patterns such as is not null or is > 5, which makes many tests shorter and safer.

C#
if (score >= 90)
{
    grade = "A";
}
else if (score >= 80)
{
    grade = "B";
}
else
{
    grade = "C";
}

Try it

C# Chained conditions

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

C# Patterns and scope in conditions

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Syntax

C#
if (condition) statement
if (condition) statement else statement
if (condition) statement else if (condition) statement else statement
Name Description
condition An expression of type bool (or a type with an implicit conversion to bool). It may declare pattern variables with is or out var.
statement A single statement or a block in braces.

Errors: if (x = 5) with an int fails with CS0029 (cannot convert int to bool) rather than silently assigning. Using a variable the compiler cannot prove is assigned in the condition gives CS0165.

Usage notes

  • Always use braces. A body without braces covers exactly one statement; the second example above prints "always printed" even though the condition is false. Misleading indentation is a classic source of bugs.
  • Dangling else. An else belongs to the nearest preceding if that has no else. Braces make your intent explicit.
  • Pattern variables declared in the condition (item is int n) are definitely assigned only where the pattern matched. With if (x is not string s) return; the variable s is in scope and assigned after the if.
  • Short-circuit evaluation. && and || stop as soon as the result is known, so if (list != null && list.Count > 0) is safe. Use & and | only when you need both sides evaluated.
  • Guard clauses. Returning early with if (bad) return; keeps the main path unindented and is usually clearer than deep nesting.
  • Choosing a form. Use the conditional operator a ? b : c when you need a value, a switch expression for many cases on one value, and if when branches do different work.
  • Nullable bool. A bool? cannot be used directly as a condition; compare it with == true or use is true.