Programming languages use conditional statements to execute programs based on certain conditions. Continue reading the C# tutorial below by Mytour to learn more about conditional statements in C#.
Conditional Statements in C#
1. Conditional Statements in C#.
2. If Statement in C#.
3. If-else Statement in C#.
4. If-else-if Ladder Statement.
5. Nested If Statement.
6. Switch Statement in C#.
7. Nested Switch Statement in C#.
1. Conditional Statements in C#
Types of conditional statements in C# include:
- if
- if-else
- if-else-if
- Nested if
- Switch
- Nested switch
Here, Mytour will delve into detailing each conditional statement in C#.
2. Conditional Statements in C#
Conditional statements in C# evaluate given conditions. If the condition is true, the statements are executed; otherwise, they are not executed.
Syntax:
if(condition)
{
//code to be executed
}
Note: If curly braces {} are not used with the If statement, the next statement is considered only associated with the If statement.
To gain a deeper understanding, readers are encouraged to refer to the example below:
if (condition)
statement 1;
statement 2;
In the example above, only statement 1 is considered associated with the if statement.
Illustrative diagram:
Example:
// C# program to illustrate if statement
using System;
public class Example {
public static void Main(string[] args)
{
string name = 'Mytour';
if (name == 'Mytour') {
Console.WriteLine('Mytour');
}
}
}
The output format is:
Mytour
3. If - else Statement in C#
The If statement evaluates code if the condition is true, otherwise, there is an alternative code. This statement informs the code what to do when the If condition is false.
Syntax:
if(condition)
{
// code if condition is true
}
else
{
// code if condition is false
}
Illustrative diagram:
Example:
// C# program to demonstrate
// if-else statement
using System;
public class Example {
public static void Main(string[] args)
{
string name = 'Mytour';
if (name == 'Mytour') {
Console.WriteLine('Mytour');
}
else {
Console.WriteLine('Mytour');
}
}
}
The output format is: Mytour.
4. If - else - if ladder
The if-else-if ladder executes one condition from multiple statements. The execution process starts from the top and checks each If condition. The If statement of the block is executed if the condition evaluates to true. If none of the If conditions evaluate to true, then another block is evaluated.
Syntax:
if(condition 1)
{
// code executed if condition 1 is true
}
else if(condition 2)
{
// code executed if condition 2 is true
}
else if(condition 3)
{
// code executed if condition 3 is true
}
...
else
{
// code executed if all conditions evaluated are false
}
Below is an illustrative diagram of the if-else-if ladder:
Example of if-else-if ladder in C#:
// C# program to demonstrate
// if-else-if ladder
using System;
class Example {
public static void Main(String[] args)
{
int i = 20;
if (i == 10)
Console.WriteLine('i is 10');
else if (i == 15)
Console.WriteLine('i is 15');
else if (i == 20)
Console.WriteLine('i is 20');
else
Console.WriteLine('i is not present');
}
}
The output format is: i is 20.
5. Nested If Statement
An If statement inside another If statement is called a nested If statement. The inner If statement in this case is the target of another If statement. When more than one condition is evaluated true, and one of those conditions is a child condition of the parent condition, nested If statements can be used.
Syntax:
if (condition 1)
{
// code executed
// if condition 1 is true
if (condition 2)
{
// code executed
// if condition 2 is true
}
}
Below is a diagram illustrating nested If statements in C#:
Example:
// C# program demonstrating
// nested-if statement
using System;
class Example {
public static void Main(String[] args)
{
int i = 10;
if (i == 10) {
// Nested - if statement
// Will only be executed if statement
// above it is true
if (i << 12)
Console.WriteLine('i is smaller than 12 too');
else
Console.WriteLine('i is greater than 15');
}
}
}
The output format is:
i is smaller than 12 too
6. Switch Statement in C#
The Switch statement in C# can replace the if-else-if ladder. The expression is checked for different cases. The Break statement is used to exit the Switch statement. If the Break statement is not used, the condition will be passed to all cases below until the Switch is found or terminated. If no cases match, the default case will be executed.
Syntax:
switch (expression)
{
case value1: // sequence of statements
break;
case value2: // sequence of statements
break;
.
.
.
case valueN: // sequence of statements
break;
default: // default sequence of statements
}
Below is the diagram of Switch - Case statement in C#:
Example:
// C# example demonstrating switch case
using System;
public class Example
{
public static void Main(String[] args)
{
int number = 30;
switch(number)
{
case 10: Console.WriteLine('case 10');
break;
case 20: Console.WriteLine('case 20');
break;
case 30: Console.WriteLine('case 30');
break;
default: Console.WriteLine('None matches');
break;
}
}
}
The output format is:
case 30
7. Nested Switch Statement in C#
Nested Switch statements are also used in C#. In this case, a Switch is nested within another Switch statement.
Example of nested Switch statement in C#:
edit
play_arrow
brightness_4
// C# example for nested switch case
using System;
public class Example
{
public static void Main(String[] args)
{
int j = 5;
switch (j)
{
case 5: Console.WriteLine(5);
switch (j - 1)
{
case 4: Console.WriteLine(4);
switch (j - 2)
{
case 3: Console.WriteLine(3);
break;
}
break;
}
break;
case 10: Console.WriteLine(10);
break;
case 15: Console.WriteLine(15);
break;
default: Console.WriteLine(100);
break;
}
}
}
The output format is:
This article Mytour has just introduced you to conditional statements in C#. Additionally, readers can explore other articles available on Mytour to learn more about C# program structure and basic C# syntax.
