C# if, if else statement, and ternary operator with examples

(Last Updated On: September 10, 2021)


C# if, if else statement and ternary operator:- C# applications are ultimately made up of class definitions. Methods are defined in the classes and these contain the actual instructions that are executed when the application is called. In principle, the instructions of a method are processed one after the other, from top to bottom. This procedure meets the requirement for deterministic prediction of program execution contrary, but leaves the application no scope for its own intelligence. Already in the forties, the mathematician John von Neumann therefore postulated that it should be possible to execute the program interrupt at any time and continue at another point in the application. Three typical language constructs developed that are now in practically all of them structured programming languages ​​can be found:

  • A condition defined in the application is used to decide whether a Statement block or which of several alternative statement blocks is to be executed.
  • A specific instruction block is executed again and again as long as a defined condition is met.
  • Program execution changes from the current instruction to a certain other Direction redirected.

The following sections deal with the syntax of these language constructs, their practical use in programs as well as the potential pitfalls and sources of error that must be observed.


C# knows three forms of condition:

  • The simple if statement controls whether a subsequent block of statements should be executed or not.
  • The C# if else condition executes one of two alternative blocks of statements.
  • The switch condition jumps to one of several statement blocks.

The simple if statement in c#:

The simple if statement decides whether a subsequent statement block should be executed or Not.

Syntax of simple if statement in c#:

The condition here is any expression that evaluates to a Boolean value (true or false). If the condition is fulfilled (true), the block following the condition is executed, if the condition is not met (false), the block is skipped and the application starts with the next statement after the if statement.

If, as in the above case, the if-condition only controls a single statement, the block brackets can also be omitted:

In such cases, the compiler interprets the semicolon as an empty statement and would only pass this through the if-condition check.

The following code fragment implements a counter that acts like a two-digit mechanical counter is reset to 0 when the value reaches 100. An if-Instruction.

Instead of the if statement, you could also use the modulo operator here to reset the counter (counter% = 100;). The if statement, however, is easier to understand.

Example how to use simple if statement in c#:

c# if else

What is condition?

In order to be able to make decisions, an application must contain criteria on the basis of which the decisions are made. The programmer writes these criteria or conditions in the source code as boolean expressions.

  • In the simplest case, a Boolean expression consists of a Boolean value: one of the literals true or false, a variable of the type bool or the call of a method that has a Boolean Returns value:

  • Comparative expressions are far more interesting. All comparison operators return as Returns true or false, depending on whether the comparison is true or false.

  • Finally, several comparison expressions can be used in a condition using the logical operators.


C# does not do any automatic conversion from 0 to false or any other int values ​​to true and therefore does not allow int expressions as conditions. So if you do If you only want to execute a statement block when an int variable points is equal to zero, you have to instead of if (points) write explicitly: if (points == 0). Be rewarded for the extra effort accidental assignments in conditions (such as if (points = 0)) intercepted by the compiler!

C# if else Statement:

With the if statement, you cannot just execute a single statement or a block of statements control, you can also alternatively use an instruction block A or an instruction block Let B do it. To do this, the if construction is extended by an else clause.

Syntax of c# if else statement:


The first thing that is checked when the application runs is the condition. If this is fulfilled (results in the expression in the condition true), the following statement block is executed. Is this processed, the application is continued directly with the next statement under the else block. The else block itself is skipped. If the condition is not met (false), the statement block is displayed directly under the if-condition skipped and the application is continued with the else-block.

The following code reads in a number and uses an if condition to test whether the number is positive. If yes, it outputs the root of the number, otherwise an error message.

Example how to find the square root of a number using C# if else statement:

c# if else

c# if else

C# if else chains and nesting:

You can chain c# if else statement by adding another if else statement:

During the program execution, the conditions of the C# if else chain are checked one after the other and it becomes either the statement block of the first applicable if-condition or the last, final one else block executed. Finally, of course, there is also the option of nesting C# if else statement.

The question occasionally arises as to which if-condition a given else part belongs to dangling else Problem). The answer is: The else part always belongs to the last preceding if part, the does not yet have an else part. In the example below, the else part belongs to the if condition if (moisture> 50) and not, as the misleading indentation suggests, to the uppermost if-condition.

The conditional  or Ternary operator ?: in c#:

Simple C# if else statement can also be expressed using the conditional or ternary operator ?:. The ternary operator expects three operands as the only operator: a condition and two expressions, one of which is executed depending on the outcome of the condition.

Condition? Expression1: expression2;

If the condition is true, the value of Expression1 is returned as the result, otherwise the value of expression2.

example how to use ternary operator in c#:

c# if else

c# if else


Recommended For You

About the Author: Engr Fahad

My name is Shahzada Fahad and I am an Electrical Engineer. I have been doing Job in UAE as a site engineer in an Electrical Construction Company. Currently, I am running my own YouTube channel "Electronic Clinic", and managing this Website. My Hobbies are * Watching Movies * Music * Martial Arts * Photography * Travelling * Make Sketches and so on...

Leave a Reply

%d bloggers like this: