Conditionals involve a program’s ability to deduce and do different things depending on the conditions of the codes written in that program.
This shot is targeted at beginners with a basic understanding of C# fundamentals.
Below are the types of conditional statements in C#:
a. The if(condition)
statement
b. The if(condition)-else
statement
c. The nested if(condition)-else
statement
d. The switch
statement
The basis for conditionals in C#, and most programming languages, is the if
statement. To make it more complex, block(s) of else
and else-if
statements can also be added.
if(condition)
statementThe if
statement is used as a single statement where the codes in the curly {}
brackets execute if, and only if, the conditions in the round ()
brackets are true.
// The `if` syntax
if(this expression or condition is true)
{
// execute this code;
}
The expression and condition terms refer to instructions the program uses to make decisions before executing any code.
The executable code below describes an if
statement:
using System;class Conditional{static void Main(){int age = 19;//Conditionif(age >= 13 && age <= 19)//True block{Console.WriteLine("You are a teenager!");//This block of code will execute because the condition is true.}}}
The output will be, “You are a teenager!” because the value of the age variable is equal to 19.
if-else
statement.The if-else
statement allows for two conditions to be checked. If the conditions in the first round ()
brackets are true
, the codes in the first curly {}
brackets will execute. Otherwise, the codes in the second curly {}
brackets will execute.
// The `if-else` syntax
if(this expression or condition is true)
{
// execute this statement;
}
else
{
// execute this statement;
}
The code below describes an if-else
statement.
using System;class Conditional{static void Main(){int age = 7;//Conditionif(age >= 13 && age <= 19)//True block{Console.WriteLine("You are a teenager!");//This block of code will not execute because the condition is not met.}else//False/Default block{Console.WriteLine("You are not a teenager!");//This block of code will execute instead.}}}
The value of the age
variable is less than 13
, which sets the condition in the if
block to false
and the output to ‘‘You are not a teenager!’’.
if-else
statement.This nested if-else
statement allows for a series of conditions to be checked continuously, from top to bottom, until a condition is met. Then, the code in that block is executed.
// The nested `if-else` syntax
if(expression1 or condition1 is true)
{
// execute statement1;
}
else if(expression2 or condition2 is true)
{
// execute statement2;
}
else if(expression3 or condition3 is true)
{
// execute statement3;
}
// You can nest as many else-ifs as you need
else
{
// execute this statement;
}
The code below describes a nested if-else
statement.
using System;class Conditional{static void Main(){int age = 30;//Conditionif(age >= 13 && age <= 19)//True block1{Console.WriteLine("You are a teenager!");//This block of code will not execute because the condition is not met.}else if(age == 20)//True block2{Console.WriteLine("Are you a college graduate?");//This block of code will not execute because the condition is not met.}else if(age == 30)//True block3{Console.WriteLine("What is your net worth?");//This block of code will execute because the condition is met.}else//False/Default block{Console.WriteLine("You are not a teenager!");//This block of code will not execute because there is a valid condition above it.}}}
The execution of this code will check the if
condition first. If the check is true, the statement will be displayed. If the check is false, the first else-if
is checked.
If it is true, then the statements in that else-if
block are executed; otherwise, the check will go to next else-if
part and continue until one of the conditions return true.
If the results of all the conditions are false, the check will go to the else
part and execute it.
switch
statement.A switch
statement in C# checks the value of a variable against multiple cases. If it matches any case, the statements in that case will execute and the break
keyword will stop the switch
.
Each case in a block of switch
cases holds an option called an ‘identifier’. When a value is entered, it is compared with the cases in the switch
block until an exact match is found.
If a match is not found, the default statement is executed.
// The `switch` syntax
switch(variable or value) {
case expression1:
statement(s)1;
break;
case expression2:
statement(s)2;
break;
case expression3:
statement(s)3;
break;
//you can add as many cases as is needed
default:
statement(s)default;
break;
}
The code below describes a switch statement.
using System;class Conditional{static void Main(){string city = Benue;switch(city){//False casecase "Lagos":Console.WriteLine($"Welcome to {city}, Nigeria!");//This block of code will not execute because the condition is not met.break;//Another false casecase "Abuja":Console.WriteLine($"Welcome to {city}, Nigeria!");//This block of code will not execute because the condition is not met.break;//True casecase "Benue":Console.WriteLine($"Welcome to {city}, Nigeria!");//This block of code will execute because the condition is met.break;//Default casedefault:Console.WriteLine($"Welcome to Nigeria!");//This block of code will execute if all cases do not match the switch.break;}}}
Here, the variable’s value is compared to all cases in the switch
block. Since it matches the third case, the statement in that case will be executed and displayed on the console.