Conditional Statement in C#

Synopsis

  1. Introduction
  2. Definition of conditional statement
  3. Why "if" is as conditional statement
  4. Use of conditional statement
  5. Type of conditional statement
  6. Conclusion

Introduction

Conditional statements are primarily used in programs, their use is frequent. Without conditional statements we cannot check a condition in the program logic. This article explains where and how to use conditional statements in programs. It is the same in all languages, but there is a small difference in syntax.

Definition

Conditional statements execute a statement (or statement block) based on a condition. "If" is a conditional statement in all languages and it is a reserved word. In other words we can't use it for a variable name, function name and others.

Why "if" is a conditional statement

Normally, "if" as a conditional statement. We can say that in the English language "if" means "condition", so we use "if" as a conditional statement in programming languages.

Use of conditional statement

If we need to verify a condition in programs thenwe use a conditional statement. Use a conditional statement for various scenarios. It has two parts, one is for "true" and the other part is for "false". A conditional statement accepts two values, one is "true" and the other "false".



Figure 1: Conditional Statement Parts

Syntax

  1. if (condition)   
  2. {  
  3.     True part statements;  
  4. else   
  5. {  
  6.     False part statements;  
  7. }  

You can see the syntax of a conditional statement above. If the condition is true then the work flows directly to the true part otherwise the work flows to the false part. In a conditional statement the false part is optional, if we need the false part then we provide it otherwise we need not write it.

Work flow of conditional statement

Figure 2: Work flow of conditional statement
 
Figure 2 shows the workflow of a conditional statement. If the check of the condition returns true the it executes the true part else it executes the false part.

Types of conditional statements

There are various types of conditional statements that we are following here as in the following:
  • Simple if
  • If else
  • Nested if
  • Else if later
  • Ternary operator

Simple if

If we have only the true part then it is called a simple if. That only executes the true part and if the condition is false then nothing is executed.

Example

Type 1

  1. static void Main(string[] args)   
  2. {  
  3.     int a, b;  
  4.     a = 10;  
  5.     b = 5;  
  6.     if (a > b) //condition true  
  7.     Console.WriteLine("A Value Is Greater");  
  8.     Console.Read();  
  9. }  
Output

A Value Is Greater

Type 2
  1. static void Main(string[] args)   
  2. {  
  3.     int a, b;  
  4.     a = 10;  
  5.     b = 5;  
  6.     if (a < b) // condition false  
  7.     Console.WriteLine("A Value Is Greater");  
  8.     Console.Read();  
  9. }  
In type 2 the output does not display because the condition (a<b) is false. In the simple type we have only the true part, so if the condition is false then it does not execute the false part.

If else conditional statement

An if else conditional statement is used in programs often. These conditional statements have two parts. One is the true part and the other is the false part.

Type 3
  1. static void Main(string[] args)   
  2. {  
  3.     int a, b;  
  4.     a = 10;  
  5.     b = 5;  
  6.     if (a > b) // condition true  
  7.     Console.WriteLine("A Value Is Greater");  
  8.     else Console.WriteLine("B Value Is Greater");  
  9.     Console.Read();  
  10. }  
Output

A Value Is Greater

In a type 3 the condition is true so it executes the true part else it executes the false part. In a type 4 we can see the false part as an output.

Type 4
  1. static void Main(string[] args)   
  2. {  
  3.     int a, b;  
  4.     a = 10;  
  5.     b = 5;  
  6.     if (a < b) // condition false  
  7.     Console.WriteLine("A Value Is Greater");  
  8.     else Console.WriteLine("B Value Is Greater");  
  9.     Console.Read();  
  10. }  
Output

B Value Is Greater

Nested If statement

When one if statement is inside another if statement then that is called a nested if. Each if statement has separate true and false parts.

Work flow of nested if statement

Figure 3: Work flow of nested if statement

Type 5

  1. static void Main(string[] args)   
  2. {  
  3.     int a, b;  
  4.     a = 10;  
  5.     b = 5;  
  6.     if (a < b) // condition false  
  7.     Console.WriteLine("A Value Is Greater");  
  8.     else Console.WriteLine("B Value Is Greater");  
  9.     Console.Read();  
  10. }  
Output

A Value Is Greater

The preceding program shows a nested if statement. Assume there is more than one line in the true part or the false part then we should use open parentheses and close otherwise it should make an error. For example:
  1. if (a > b)   
  2. if (a > c) Console.WriteLine("A Value Is Greater");  
  3. Console.WriteLine("A Value Is :" + a);  
  4. Else // Invalide expression terms ‘else’  
  5. Console.WriteLine("C Value Is Greater");  
  6. else if (b > c)   
  7. Console.WriteLine("B Value Is Greater");  
  8. else   
  9. Console.WriteLine("C Value Is Greater");  
The preceding code produces an error because more than one statement is used in the true part without parentheses. We can resolve it in the following way.
  1. if (a > b) if (a > c)   
  2. {  
  3.     Console.WriteLine("A Value Is Greater");  
  4.     Console.WriteLine("A Value Is :" + a);  
  5. }   
  6. else Console.WriteLine("C Value Is Greater");  
  7. else if (b > c) Console.WriteLine("B Value Is Greater");  
  8. else Console.WriteLine("C Value Is Greater");  
Here we resolved the error because we used parentheses to write two statements in the true part.

Else if later

Else if later is one of the important types of conditional statements. It has many conditional statements. It checks each condition step-by-step up to when it gets a true value. If once a condition is true then it prints the statement then comes out from the flow.

Work flow of else if later


Figure 4: 
Work flow of else if later
 
Figure 4 shows the workflow of an else if later. Type 6 is an example for "else if later".

Type 6
  1. if (a > b) if (a > c)   
  2. {  
  3.     Console.WriteLine("A Value Is Greater");  
  4.     Console.WriteLine("A Value Is :" + a);  
  5. }   
  6. else Console.WriteLine("C Value Is Greater");  
  7. else if (b > c) Console.WriteLine("B Value Is Greater");  
  8. else Console.WriteLine("C Value Is Greater");  
Output

First Greater

Here "Greater" is the value assigned to "O" so it should execute the statement "First Greate" and assuming there is not a matching condition it checks the next if condition, if it still does not match then it directly executes the first condition's else part.

Ternary operator

The Ternary operator is not a type of conditional statement but it is also used to check the conditions. It returns only one value based on conditions. Use the ternary operator to check many values in a single line of coding.

Two operators are used in the ternary operator. One is the question mark (?) and the other is a semicolon (:).

Syntax

Variable = condition? True Part : False Part;

The ternary operator returns a value to a variable, the if condition when true returns the true part value otherwise it returns the false part value.
"?" is like if and ":" is like else.

Work flow of ternary operator

Figure 5: Workflow of the ternary operator
 
Figure 5 shows the workflow of the ternary operator. The Green color shows the return value of the true part and the Red color shows the return value of the false part. Both returns a true and false value based on the condition.

Type 6
  1. static void Main(string[] args)   
  2. {  
  3.     int a, b, retValue;  
  4.     a = 10;  
  5.     b = 5;  
  6.     retValue = a > b ? a : b;  
  7.     Console.WriteLine("Greater Value:" + retValue);  
  8.     Console.Read();  
  9.   
  10. }  
Output

Greater Value: 10

Type 7
  1. static void Main(string[] args)   
  2. {  
  3.     int a, b, retValue;  
  4.     a = 10;  
  5.     b = 5;  
  6.     retValue = a > b ? a : b;  
  7.     Console.WriteLine("Greater Value:" + retValue);  
  8.     Console.Read(); 
  9. }  
Output

Greater Value: 20

Type 8
  1. static void Main(string[] args)   
  2. {  
  3.     int a, b, c, retValue;  
  4.     a = 10;  
  5.     b = 20;  
  6.     c = 30;  
  7.     retValue = a > b ? a > c ? a : c : b > c ? b : c;  
  8.     Console.WriteLine("Greater Value:" + retValue);  
  9.     Console.Read();  
  10. }  
Output

Greater Value: 30

It is possible to create a nested ternary operator. For example, the nested ternary operator shown above for Type 8.

Nested ternary operator

Figure 6: Nested ternary operator

Figure 6 shows a nested ternary operator. In a nested ternary operator, we have more than one condition and more than one true and false part based on conditions.

Conclusion

There are various types of conditional statements available. Each type of conditional statement uses a different scenario. It is the same in all programming languages, but only the syntax is different. Without a conditional statement we cannot complete the program logic that is very important.

Up Next
    Ebook Download
    View all
    Learn
    View all