Easy Tutorial
❮ Csharp Intro Csharp Variables ❯

C# Decision Making

Decision structures require programmers to specify one or more conditions to be evaluated or tested, along with statements to be executed if the condition is true (required) and statements to be executed if the condition is false (optional).

Below is the general form of a typical decision structure in most programming languages:

Decision Statements

C# provides the following types of decision statements. Click on the links to see details for each statement.

Statement Description
if Statement An if statement consists of a Boolean expression followed by one or more statements.
if...else Statement An if statement can be followed by an optional else statement, which executes when the Boolean expression is false.
Nested if Statements You can use one if or else if statement inside another if or else if statement.
switch Statement A switch statement allows testing a variable against a list of values.
Nested switch Statements You can use one switch statement inside another switch statement.

? : Operator

We have covered the conditional operator ? : in previous chapters, which can be used to replace if...else statements. Its general form is as follows:

Exp1 ? Exp2 : Exp3;

Where Exp1, Exp2, and Exp3 are expressions. Note the use and placement of the colon.

The value of a ? expression is determined by Exp1. If Exp1 is true, then the value of Exp2 is the value of the entire ? expression. If Exp1 is false, then the value of Exp3 is the value of the entire ? expression.

❮ Csharp Intro Csharp Variables ❯