c sharp

C # If Statement

While performing programs, we often face situations to control the program flow, where we are implied to test the conditions whether they are true or false. In C sharp, like other programming languages, an if statement is used in this situation. If the condition applied to the statement is true, the program proceeds forward; otherwise, it is terminated. This article will contain the working of the if statement in C sharp.

Syntax

If ( boolean-expression / condition)

{

// Statement to be displayed if the condition is true

}

This is the syntax of a simple if-statement; on adding else and else-if parts, more conditional statements are added accordingly.

Example # 1: If statement in C sharp

To implement the if-statement in C sharp language, we need to include some libraries to execute the program. For example, ‘SYSTEM’ is used in the given example. The C sharp or .NET framework provides access to the system’s functionality; we use the system header file in our source codes.

Using System:

Similarly, the namespace is used to arrange logically the interfaces, classes, and structures. A single namespace can contain other namespaces, too that’s why it is also known as a nested namespace. As we know, C sharp evolved through the concept of Object-Oriented Programming; hence, we need to declare classes before starting any program. In this program, we have named a class ‘statement’; inside the class, the static main program is written having string-type arguments. Inside the main program, we will mention the logical part we need to implement.

As we are concerned with the if-statement. So we have used a single if statement in the program. The code is designed in such a way that it will check if the number is greater or smaller than 5. So we have taken an integer type variable that will contain an integer. Now for the comparison, we will use the ‘<‘ less-than operator in the code.

If (number < 5)

If the statement works on the boolean technique, because if the condition is attained, it returns true, otherwise false, and the control comes out of the if-body. The number is 4; when used in the loop, the if statement will check it, so the statement inside the loop will be executed. Like cout in C ++, console. Writeline is used in C sharp to print any result to the console terminal. A statement was written outside the body to ensure that this will always be executed. It has no concern with the if statement, so either the condition of the if statement is true or false, the statement outside the body will be executed.

Save the file with the ‘.cs’ extension. And use the Ubuntu terminal to execute the code written in the text editor. For C-sharp, we will use the MCS compiler that is installed easily on Ubuntu; now, after the code has compiled without any error, we will execute it through a mono keyword and the file name with the ‘exe’ extension.

$ MCS a.cs

$ mono a.exe

Example # 2. If statement with else.

In many scenarios, you need to provide more than a single conditional option according to the program demand that can be accomplished through a single if statement, so we use the if-else statement combine. In the if statement, if the condition is false, the control goes out of the body, but in this case, on a false condition, the control goes to the else part of the body. In other words, the else part is for the negative condition to be executed. In the example, a number is taken, and we have applied the same concept as in the first example. As the number 10 is greater than the number 5 used in the if-part of the statement, the control will go to the else part, so the statement inside the else part will be displayed.

Else {}

Again a statement outside the if-body shows that either the ‘if’ or the else part is not executed; this line will always be executed because it is not the part of the if statement.

You will see that the statement that is inside the else part is displayed and the outer one.

Example # 3: if-else-if statement

Another type of the ‘if’ statement along with another ‘if’ is used here. By using this approach, we can apply more than two conditions in the same program. This example uses the involvement of the user. The user will enter the number, which is checked according to the ‘if-else if statement. Inside the main program, we will use a ‘ReadLine’ function to get value from the user. A variable of integer type will accept the number.

Int number = Convert.ToInt32(Console.ReadLine());

This statement will convert the entered value first into the integer through the ‘TOint32()’ function.

Else if (number > 10)

The entered number will be checked through the ‘if statement’ and then to the ‘else if’ part, and if the condition is again false, it will go to the else part. On execution, the user will enter a number. For instance, the user first enters 17, so it is greater than 10; first, the control will be at the ‘if’ part, the condition will be wrong, then the ‘else if’ part will compare, the condition is true, so the statement inside it will be executed, and the control will come out of the body.

Now, if the user enters 10, that is the same number used for the comparison, so the third, else part will be executed. The else part does not contain any conditions and only displays a message directly.

Example # 4: Nested if statement

As we are well-known about the nested loops, similarly, the nested if-else statement works in C sharp language; inside the ‘if’ statement, there is another if statement. The logic works in such a way that if the outer if-statement is true, then the inner if statement with the else part is executed.

Otherwise, if the outer body is false, then the control goes to the else part of the outer body. The else part of the outer body further contains the ‘If statement’ inside it.

We have taken three integers. And now, we will compare all of them with one another through the if statement here, the outer if contains the statement that if the first value is greater than the second one, then again compare the first value with the third one, as the condition of outer if, becomes true, so inner if will compare, the control will go to the else part, and the program will be executed, as the else part will return true. Whereas the remaining code contains the same comparison of the second variable with the third one.

On execution, you will see that the third variable is the largest.

Conclusion

C sharp contains the if-statement in more than one way. Each type is explained by using simple examples to elaborate and highlight the working of the If-statement in the Ubuntu operating system. We have used mono, MCS compiler for the compilation. Another compiler may also be used for better results in an efficient way.

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.