C# Decision Making is used to requires the programmer to specify any one or more conditions to be evaluated by the program. In decision making, a certain block of code needs to be executed when given condition is fulfilled. It execute a statement or set of statements. If the given condition is determined to be true, other statements will be executed if the given condition is determined to be false.
Given below is the structure of decision making in C# programing languages:
Types of Decision Making in C#:
Statement | Description |
if statement | It consists of a boolean expression followed by one or more statements. |
nested if statements | It is used in one if or else if statement inside another if or else if statement(s). |
if…else statement | It can be followed by an optional else statement, which executes when the boolean expression is false. |
switch statement | It allows a variable to be tested for equality against a list of values. |
nested-switch statements | It can use one switch statement inside another switch statement(s). |
These types are explained in the next sections.
?operator
In the previous chapter, we have covered the conditional operator, ?:
which can be used to replace if...else
statements. It has the following general form:
Exp1 ? Exp2 : Exp3;
Where Exp1
, Exp2
and Exp3
are expressions. Please pay attention to the use and position of the colon.
1. the Exp1
expression is evaluated and evaluated. If the evaluation result is true, it Exp2
is evaluated and returned as the entire value. If the Exp1
evaluation result is false ( false
), the Exp3
expression is evaluated, and its value is returned as the value of the expression.
int a = 1; int b = 2; int c = 0; c = (a>b)? a: b;