Last Updated:

C# Conditional Operators and Constructs (sharp)

Conditional operators allow you to build logical conditions. "If that's the case, then do this, do something else, do it this way" and similar examples. Any condition of the type "one way or another" can be issued in the form of a code.

It is thanks to conditional operators that you can build branching algorithms, logic elements and make your computer "think".

There are three kinds of conditions in C#:

  1. if () — if, the main condition, if used, then only once;
  2. else if () — an alternative condition, if the main condition does not work, can be used as many times as you like;
  3. else — triggers if none of the conditions have been met, can be used only once;

A Boolean expression is indicated in parentheses.


If the Boolean expression is true (for example, 2 is less than 10), the operator returns true (true or true), which means that the Boolean expression has been executed and the code in that block needs to be executed.


If the Boolean expression is not true (for example, 2 greater than 10), the operator returns false (false or false), which means that the Boolean expression did not execute and the code in this block does not need to be executed.

 

Returning a value means that some action has occurred and the operator (or function) has "responded" to us. We asked a question (which is more, this or that) and the compiler gave us the answer (either yes – true, or no – false).

 

In order to compare something with something, it is necessary to use special symbols allocated for this case. Such symbols are called logical relationship operators.

Logical Relationship Operators

 

In fact, there are not so many of these operators.

  1. > is more;
  2. < is less;
  3. == — checking the equality of values;
  4. != is not equal;
  5. >= is greater than or equal to;
  6. <= is less than or equal;

Actually, we will use these very operators when compiling logical expressions and thereby comparing the values with each other.

In order to write more complex logical conditions, logical operators are used:

  1. && — logical I;
  2. || — logical OR;

Thanks to these operators, we can combine logical conditions.

Right Conditions Model

Consider the conditional construction as an indicative model

Remember: in a conditional block (a block for checking some element in the program algorithm), use only one if()!
The compiler does not forbid them to be used many in one block, please. But, if you write solid if() instead of else if(), then each time a logical condition will be checked, and this in turn will slow down the program. Write high-quality code!

A real-world example of a conditional expression

Here is a practical example with the output of the message. Let's compare two variables and perform a certain action depending on the returned value.

As a result, we get

We got this result due to the fact that 10 is more than 5. Play around with the value of the variables, get a different result.

Combined Boolean expression

Here's an example of implementing a combined Boolean expression.

Now you can compare any values and build your own logical conditions!