if Statements

if statements allow evaluation of an expression and, depending on the truth of the evaluation, the capability to branch to a specified sequence of logic. C# provides three forms of if statements: simple if, if-then-else, and if-else if-else.

Simple if

A simple if statement takes the following form:

if (Boolean expression)
[{]
    true condition statement(s)
[}]

As expected, the Boolean expression must evaluate to either true or false. When the Boolean expression is true, the program performs the following true condition statements:

if (args.Length == 1)
{
  Console.WriteLine("What is your pleasure, {0}?", args[0] );
}

Warning

The curly braces are optional if there's only one action. It's usually a good practice to add them anyway. ...

Get C# Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.