Decision Making in C#

Decision Making in C#

The programmer must define one or more conditions to be evaluated or tested by the programme, a statement or statements to be performed if the condition is found to be true, and optionally, further statements to be run if the condition is found to be false.

The general shape of a common decision-making framework present in most programming languages is shown below.

In the C programming language, any non-zero and non-null value is assumed to be true, whereas zero and null values are considered to be false.

The C programming language includes the decision-making statements listed below.

Sr.No Statement & Description
1 if condition
A boolean expression is followed by one or more statements in an if statement.
2 if...else condition
An if statement may be followed by an optional else statement if the Boolean expression is false.
3 If statements that are nested
One if or else if statement can be used inside another if or else if statement (s).
4 a switch clause
A switch statement compares a variable against a list of values to see whether they are equivalent.
5 switch statements stacked within each other
You can nest switch statements (s)

The ? : Operator

  • In the last chapter, we looked at the conditional operator? :, which can be used to replace if...else expressions. It takes the broad pattern Exp1? Exp2: Exp3;
  • Expressions Exp1, Exp2, and Exp3 are used. Take note of the colon's use and location.
  • The value of a? expression is calculated in the same way as Exp1 is calculated. If this is the case, Exp2 is evaluated and the value of the entire? expression is determined.
  • If Exp1 is false, Exp3 is evaluated, and its value is used as the expression's value.

These statements alter the execution flow in response to a logical circumstance. Based on a condition, this function is used to execute or skip a sequence of instructions.

1) A simple "if" statement: An "if" statement is a strong decision-making statement that is used to regulate the flow of statements execution.

Syntax :

if (Condition or test expression) Statement; Rest of the program (OR) if (Condition or test expression) { Statement; }

Rest of the program;

It's a "two-way" decision statement in essence (one for TRUE and other for FALSE)

There is just one choice.

Only when the condition is true is the statement performed.

The compiler skips the lines within the "if Block" if the condition is untrue.

The condition is always wrapped in a pair of parentheses, i.e. ( ). Semicolons should never be used to end a conditional statement (ie ;)

Curly Braces are usually used to surround the statements that follow the "if"-statement.

The “if” statement's scope is indicated by the curly braces. One statement is the default scope. Even with a single statement, though, it is excellent practise to use curly brackets.

A single statement or a collection of statements can make up a statement block.

The Statement Block will be performed if the Test Expression / Conditions are TRUE, and the rest of the programme will be run.

The Statement Block is skipped if the Test Expression / Condition is FALSE, and the rest of the programme executes next.