c sharp

C# Inline IF

In this article, we will be discussing the inline if statement in the C# programming language. It is also called the Ternary operator. It is a substitute for providing a check-in, just a one-line statement. It is more suitable than a traditional if and else statement as it is concise and can be written in a single line as compared to a traditional if and else statement in which we have to divide the code into two parts.

The syntax for writing an inline if statement in the C# programming language is stated below:

condition_expression? first_expression : second_expression;

The ternary operator command is divided into three parts; in the first part, the condition is written in which the result of the check would be determined; then, after the question mark, there are two statements being divided by a colon, the first expression is the true statement, and the second expression is the false statement.

Example 01: Comparing an Integer Variable With a Number Using an Inline If Statement in Ubuntu 20.04

In this example, we will be comparing an integer variable with a predefined number in a single line with the inline if statement. The check will happen in a single line, and the result will be displayed once their decision is made only on the command line terminal of the Ubuntu 20.04.

Text, letter Description automatically generated

In the C# program, we have initialized an integer variable and then initialized a bool variable to store the check result. After that, we add a day in line if statement while defining the bool variable. The condition expression compares the integer variable with a number, and the first and second expressions are true and false. After the compiler determines the result, it will be displayed as the output of this program, as shown below:

As the output suggests that the integer variable was greater than the number, the compiler gave the First expression” true” as the output.

Example 02: Checking Whether a Number is Even or Not Using the Inline If Statement in Ubuntu 20.04

An integer variable would be checked whether it is an even number or not using the inline if statement in this example. The modulus of the integer variable will be taken by 2, and if it is equal to 0, the statement word will be provided with its true, and the result will be displayed on the output screen.

Text Description automatically generated

In the above-provided snippet, we can see a C# program in which an integer variable is declared along with a bool variable. After that, the bool variable is defined as an inline if statement in which the mathematical check for a number to be even or not is written in the condition expression, and after the question mark, the first and second expression is written as true and false, and in the last line, the result is displayed to the output screen.

A screenshot of a computer Description automatically generated with medium confidence

The above output suggests that the integer variable was completely divided by 2 and had no reminder, thus resulting in an even number. The compiler chooses the first statement to be printed on the output screen.

Example 03: Comparing Two Different Variables Using the Inline If Statement in Ubuntu 20.04

In the following example, we will initialize 2 variables of the integer data type and assign some values to them. After this, we will compare these two variables using the inline if statement in the C# programming language. The ternary operator will function as a substitute for the traditional if and else statement for checking these two variables, and that would be saved as text in a varchar variable.

In the above C# program, we can see that two integer variables named “x” and “y” have some values. Then there is a varchar variable named “answer” in which the inline if statement is defined. In the inline, if statement, the condition expression is comparing both the integer variables, and after the question mark, the two expressions for true and false conditions are written. After evaluating the condition expression, the first and second expressions would be saved in the “answer” variable and printed on the output screen.

A screenshot of a computer Description automatically generated with medium confidence

After compiling and executing the above program, we get the above output. As you can see, the value of variable “y” was greater than the variable “x”, and the condition expression resulted as false That is why the compiler saved the false expression in the “answer” variable and printed it on the output screen.

Example 04: Nested Inline If Statement in Ubuntu 20.04

The conventional if and else statement can be nested with “if-else” checks, and the inline if statement, which we examined as a replacement for the if and else statement, also contains a nested “if-else” check. We will compare two variables in this example; however, there will be four checks that will take the place of the “if-else” tests. In these “if-else” checks, the integer variables will be compared with great or less or equal checks, and the “answer” variable will store the correct decision and be displayed later on.

Text, letter Description automatically generated

We declared a varchar variable named “answer,” We typed the nested inline if statement in which the condition expression keeps changing after the second expression is substituted as another condition expression in this C# program. The “answer” variable will be used to record the comparison result of the two variables, which we will display on the Ubuntu 20.04 command line terminal.

A screenshot of a computer Description automatically generated with medium confidence

The above output suggests that the integer variables “x” and “y” are equal, and the nested inline if statement predicted the correct decision as both the values were the same.

Example 05: Determining the Result of a Subject Using the Inline If Statement in Ubuntu 20.04

As we know that the grading criteria of a subject are formed of several checks, we will write a C# program in which the marks of a subject will undergo several checks to be determined whether the student has passed or failed the subject. The passing criteria will depend upon three checks, and only after that will the result be determined.

Text Description automatically generated

In the above piece of code, we have initialized a variable named “marks” and assigned a value to it. After this, we have initialized a variable in which the nested inline if statement is written. In the inline if statement, the condition expression has the marks being compared three times for greater than 50, less than 50, or equal to 50. All these 3 condition expressions have a resultant expression which would be stored as the result if that certain condition is true.

A screenshot of a computer Description automatically generated with medium confidence

As you can see in the output screen, the result is displayed as “PASS” because the marks were greater than 50, and the nested inline if statement worked perfectly while determining the result and going through all three checks.

Conclusion

In this article, the inline if statement in the C# programming language was discussed in great detail. The inline if statement is a substitute for the traditional if and else statement, and it has the advantage of being compressed in a single line. We implemented several examples of the inline if statement using the Ubuntu 20.04 environment.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.