Introduction
It's all about making decisions in life. Decision-making is primarily about selecting from a set of alternatives. Your decision-making will improve as your choices improve.
Similar scenarios arise in programming, where we must make decisions and then execute the following block of code depending on those decisions. To cope with these eventualities, we have a variety of decision-making statements, but first, let's define a decision-making statement.
Recommended Topic, Palindrome in C#, singleton design pattern in c#, and Ienumerable vs Iqueryable.
What is a Decision-making statement?
Making decisions in any programming language is an essential part of the learning process. It allows a program to choose between different execution paths based on an expression or a variable's value. Making decisions is considered the first milestone to achieve for a new programmer.
The following are the many types of decisions that can be made in C#:
- Selection statements allow you to pick between different execution paths.
- Jump statements allow a program to run in a nonlinear manner.
The selection statements such as if-else, nested if-else, and switch are the topic of this article.
If and switch is the two forms of selection statements supported by C#. These statements allow us to control the execution of our program. These two statements are both powerful and adaptable. Let's take a closer look at each of these statements one by one.
If and else Statement
An if statement is a conditional branch statement. It can be used to split a program's execution into two separate paths.
Following is the syntax of an If and else Statement:
if (condition) { statement1; } else { statement2; } |
The following is how the if Statement works: Statement1 will be executed if the condition is true. If that is not the case, statement 2 will be executed (if it is present). A single statement or a block of statements enclosed in curly braces can be used for each Statement. The condition returns a boolean value.
Note: The else clause is optional.
In the below code, if a is less than b is checked using the if and else statement.
using System;
namespace Demo {
public class Example {
public static void Main(string[] args) {
int a=10,b=5;
if (a < b)
Console.WriteLine("b is greater");
else
Console.WriteLine("a is greater");
}
}
}
Output:
a is greater |
Nested if-else Statement
A nested if-else statement is an if-else statement within an if-else statement. In programming, nested ifs are pretty prevalent. An else statement refers to the closest if Statement within the same block.
For example,
if(i == 10) // top-most if { if(j < 20) // first nested if a = b; { if(k > 100) // second nested if c = d; else // inner else a = c; } } else // final else a = d; // this else refers to the top-most if |
The last else is not related to the if just before it, as the comments imply. Instead, the top-most if is linked to the final else. Because it is the closest within the same block, the inner else is associated with the second nested if.
Example:
using System;
namespace DecisionMaking
{
class NestedIf
{
public static void Main(string[] args)
{
int number = 8;
if (number < 7)
{
Console.WriteLine("number is less than 7");
}
else
{
if(number < 15)
{
Console.WriteLine("The number is lesser than 15");
}
else
{
Console.WriteLine("The number is greater than 15");
}
}
Console.WriteLine("Lastly, this statement will be executed.");
}
}
}
Output:
The number is lesser than 15 Lastly, this statement will be executed. |
Switch Case
The switch statement is the C# equivalent of a multiway branch statement. The switch statement allows you to easily redirect execution to different areas of code based on the value of an expression.
As a result, employing the switch statement rather than a long succession of if-else-if statements is usually a better choice. The following is the generalized form of a switch statement:
switch (expression) { case value1: // statement sequence break; case value2: // statement sequence break; . . . case valueN: // statement sequence break; default: // default statement sequence } |
The switch case functions as follows:
Every literal value of the case clause within the switch block is compared to the expression. If a match is found, the code contained within that case is run. The default statement is used if none of the circumstances match the value of the expression. The default situation, on the other hand, is optional. Nothing happens if no case matches the expression and the default case is not present.
- To end a case statement, use the break statement with the switch statement. This aids in "jumping out" of the switch.
- If the break statement is omitted, the program will continue to the next case. The break statement is optional.
Example:
using System;
namespace Switch
{
class Program
{
static void Main(string[] args)
{
string title = "student";
switch( title ) {
case "teacher":
Console.WriteLine("This is a teacher.");
break;
case "student":
Console.WriteLine("This is a Student.");
break;
default:
Console.WriteLine(" Graduated ");
break;
}
Console.Read();
}
}
}
Output:
This is a Student. |
Nested Switch Statements
A switch can also be used inside a case statement. This is referred to as a nested switch. Because each switch statement produces its block, there will be no conflict between the two switch statements.
The following program shows the syntax of a nested switch:
switch(count1) // outer switch { case 1: switch(target) // inner switch { // nested switch case 0: System.out.println("target is zero"); break; case 1: System.out.println("target is one"); break; } // no conflicts with outer switch break; case 2: // and so on… |
Example:
using System;
namespace NestedSwitch
{
class Program
{
static void Main(string[] args)
{
string title = "student";
int grade = 1;
switch( title ) {
case "teacher":
Console.WriteLine("This is a teacher.");
break;
case "student":
Console.WriteLine("This is a Student.");
switch( grade ){
case 1:
Console.WriteLine("in school ");
break;
case 2:
Console.WriteLine("in college ");
break;
default:
Console.WriteLine("not known");
break;
}
break;
default:
Console.WriteLine(" Graduated ");
break;
}
Console.Read();
}
}
}
Output:
This is a Student. in school |