Tutorial On C# Conditional Statements

Tutorial On Conditional and Decision Making Statements in C#. This Tutorial will Explain How to Use If, If-Else, If-ElseIf, and Nested If Statements:

Classes, Objects, and Methods in C# were explained in detail in our previous tutorial.

A class is basically the blueprint of data and objects are the instances of the class. Methods, as we have already discussed, contains specific instructions/actions that need to be performed inside a class.

=> Explore The Entire Series Of C# Training Tutorials Here

In this tutorial, we will be covering the Conditional Statements that are used in C#.

C# conditional statements

Conditional Statements In C#

C# conditional statements are used when we want to execute a certain action depending upon an available condition.

Decision-making statements require a few conditions that can be evaluated by the program and set of statements that can be executed if the condition evaluates as true or another statement that can be executed when the condition values as false.

In this tutorial, we will be explaining how a conditional operator works with proper syntax explanation and some interesting examples. We will also be looking into nested and other different conditional statements.

Before we start, let’s have a look at a general flow of the conditional decision making flow.

conditional decision making flow

C# offers different decision-making statements. Let’s explore them in detail.

If Statement

The If Statement is made up of a boolean expression followed by a statement. The statement inside the “if” condition is executed only if the boolean expression returns “true”. If it returns false then the program will simply ignore the statement.

Syntax

The syntax for If is the keyword “if” followed by Boolean condition inside the round bracket followed by curly braces enclosing statement that needs to be executed when the Boolean expression returns true.

If(Boolean condition){
The executable statement
}

Example:

int a = 10;
int b = 10;
if (a == b) {
/* if the boolean condition returns true execute following statement*/
Console.WriteLine("Both a and b are equal");
}
Console.WriteLine("value of a and b are"+ a+" and "+b);
Console.ReadLine();
}
}

We passed two integer values a and b with the same data. If we run this program as both a and b are equal i.e. 10. This means the Boolean condition will return true, hence the statement inside the condition will be executed and the result will be as shown below.

Both a and b are equal
value of a and b are 10 and 10

If we change the value of a to 11, thereby making them not equal. This means that the Boolean expression will return a false value. If we execute the program now, then it will print the following result.

value of a and b are 11 and 10

As you can clearly see in the above example, the program didn’t execute the statement inside the “if” condition and it directly executed the statement outside the boundary of the condition.

If Else Statement

The next decision-making statement is “if-else”. It is basically an “if” statement with an optional “else” statement that comes into picture if the boolean condition returns a false value.

Syntax

The syntax is also similar to the “if” followed by an else statement. It starts with the keyword “if” followed by a round bracket enclosing the Boolean condition that is again followed by curly brackets containing statements. The enclosure of the “if” statement is followed by the “else” keyword enclosing the statement fenced within the curly bracket.

If(Boolean condition)
{
Statement to be executed with the condition of the Boolean expression is true
}else{
Statement to be executed if the condition of the Boolean expression is false.
}

Example:

int a = 11;
int b = 10;
if (a == b) {
/* if the boolean condition returns true execute following statement*/
Console.WriteLine("Both a and b are equal");
} else{
                   /* if the boolean condition returns true execute following statement*/
Console.WriteLine("Both a and b are not equal");
	          }
Console.ReadLine();
}
}

In this program, we have two integer values, a and b. Hence, if the condition is true then the statement inside the “if” block will execute and if the condition is “false” then the statement inside the else block will execute.

As both a and b are not equal, the Boolean condition will return “false” value and the else block will be executed with the following result.

Both a and b are not equal

If we change the value of a and b to make them equal then, the Boolean condition will become true and the “if” block will be executed with the following result.

Both a and b are equal

If … ElseIf… Statement

The “if… else if…” statement has an “else if” positioned after the “if” statement. This condition is very useful for handling various conditions by using a single if followed by multiple “else if” each of which represents a separate condition.

You can use a number of “else if” after the “if” statement to handle all your conditions.

Syntax

The syntax is also similar to the “if” followed by a single or multiple “else if” statement depending upon the number of conditions that the user wants to handle. It starts with the keyword “if” followed by a condition within a round bracket and the statement enclosed inside the curly braces.

The closing curly bracket is followed by the keyword “else if” with its own condition defined inside the round bracket, followed by curly brackets enclosing the statement.

If(Boolean condition)
{
Statement
} else if(Boolean condition)
{
Statement
}

Example:

Let’s write a program to find if the integer value provided by the user is negative, zero or positive.

int input = 11;
if (input < 0) {
/* if the value is less than zero this condition executes*/
Console.WriteLine("Input value is a negative integer");
} else if (input == 0){
	 /* if the value is zero this condition executes */
Console.WriteLine("Input value is zero");
	} else if (input > 0){
	/* if the value is greater than zero this condition executes */
Console.WriteLine("The input value is a positive integer");
	}

As the input is a positive integer, the program will first evaluate the first condition (input <0) and this condition will return false, hence the program will move to next else if statement. Next condition (input == 0) and this condition will again return false, hence the program will again move to next else if statement.

Next condition (input > 0) returns a true value and the program will execute the statement inside the curly bracket of this condition.

When compiled and executed, it will return the following output.

The input value is a positive integer

Nested If Statements

Nested statements mean using “if” or “else if” statement inside another “if” or “else if” statement. This allows the user to implement multiple conditions.

Example:

Let’s say that we need to find a condition where the given integer is greater than 10. We also have another condition, if the number equals to 20, we need to print “The value of the integer is 20”, and if it’s not equal we will print “The value of the integer is not 20”.

int input = 30;
if (input > 10) {
		                if (input == 20) {
            	Console.WriteLine("The value of the integer is 20");
		                }else
			                          { 
			                          Console.WriteLine("The value of the integer is not 20");
			                          }	
		                } else{  
Console.WriteLine("The value of integer is not greater than 10");
	            }
Console.ReadLine();

In the above program, we have nested an “if-else” statement inside another “if-else” statement. Similarly, any conditional statement can be nested inside another conditional statement.

Conclusion

In this tutorial, we discussed conditional and decision making statements in detail. We saw how to use “if”, “if-else”, “if…else if” statements. We also discussed the nested if statements that can be used to host different conditional statements inside another conditional statement.

When a boolean condition returns a true value, the content inside “if” is executed and when it returns a false value, then the content inside “else” is executed.

We may or may not have an else statement after “if”. If…else if statement can be used to validate several different definite conditions.

Also read =>> VBA conditional statements

=> Check Out The In-Depth C# Training Tutorials Here