Synopsis
- Introduction
- Definition of conditional statement
- Why "if" is as conditional statement
- Use of conditional statement
- Type of conditional statement
- 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
- if (condition)
- {
- True part statements;
- } else
- {
- False part statements;
- }
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.
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
- static void Main(string[] args)
- {
- int a, b;
- a = 10;
- b = 5;
- if (a > b)
- Console.WriteLine("A Value Is Greater");
- Console.Read();
- }
OutputA Value Is Greater
Type 2
- static void Main(string[] args)
- {
- int a, b;
- a = 10;
- b = 5;
- if (a < b)
- Console.WriteLine("A Value Is Greater");
- Console.Read();
- }
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
- static void Main(string[] args)
- {
- int a, b;
- a = 10;
- b = 5;
- if (a > b)
- Console.WriteLine("A Value Is Greater");
- else Console.WriteLine("B Value Is Greater");
- Console.Read();
- }
OutputA 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
- static void Main(string[] args)
- {
- int a, b;
- a = 10;
- b = 5;
- if (a < b)
- Console.WriteLine("A Value Is Greater");
- else Console.WriteLine("B Value Is Greater");
- Console.Read();
- }
OutputB Value Is Greater
Nested If statementWhen one if statement is inside another if statement then that is called a nested if. Each if statement has separate true and false parts.
Figure 3: Work flow of nested if statement
Type 5
- static void Main(string[] args)
- {
- int a, b;
- a = 10;
- b = 5;
- if (a < b)
- Console.WriteLine("A Value Is Greater");
- else Console.WriteLine("B Value Is Greater");
- Console.Read();
- }
OutputA 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:
- if (a > b)
- if (a > c) Console.WriteLine("A Value Is Greater");
- Console.WriteLine("A Value Is :" + a);
- Else
- Console.WriteLine("C Value Is Greater");
- else if (b > c)
- Console.WriteLine("B Value Is Greater");
- else
- 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.
- if (a > b) if (a > c)
- {
- Console.WriteLine("A Value Is Greater");
- Console.WriteLine("A Value Is :" + a);
- }
- else Console.WriteLine("C Value Is Greater");
- else if (b > c) Console.WriteLine("B Value Is Greater");
- 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.
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
- if (a > b) if (a > c)
- {
- Console.WriteLine("A Value Is Greater");
- Console.WriteLine("A Value Is :" + a);
- }
- else Console.WriteLine("C Value Is Greater");
- else if (b > c) Console.WriteLine("B Value Is Greater");
- 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 operatorThe 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.
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
- static void Main(string[] args)
- {
- int a, b, retValue;
- a = 10;
- b = 5;
- retValue = a > b ? a : b;
- Console.WriteLine("Greater Value:" + retValue);
- Console.Read();
-
- }
OutputGreater Value: 10
Type 7
- static void Main(string[] args)
- {
- int a, b, retValue;
- a = 10;
- b = 5;
- retValue = a > b ? a : b;
- Console.WriteLine("Greater Value:" + retValue);
- Console.Read();
- }
OutputGreater Value: 20
Type 8
- static void Main(string[] args)
- {
- int a, b, c, retValue;
- a = 10;
- b = 20;
- c = 30;
- retValue = a > b ? a > c ? a : c : b > c ? b : c;
- Console.WriteLine("Greater Value:" + retValue);
- Console.Read();
- }
OutputGreater Value: 30
It is possible to create a nested ternary operator. For example, the nested ternary operator shown above for Type 8.
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.
ConclusionThere 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.