C# | Decision Making

Decision making in programming is similar to decision making in real life. Even in programming, you need to execute a certain block of code if a condition is met. Programming languages ​​use control statements to control the flow of execution of a program based on certain conditions. These are used to advance the flow of execution and branches based on changes in program state.
The conditional statements of C# are:  

  • if
  • if-else
  • if-else-if
  • Nested if
  • Switch
  • Nested switch

1. IF Statement 
The if statement checks the given condition. If the condition evaluates to be true, then the block of code/statements will execute otherwise not. 
Syntax: 
 if(condition) {//code to be executed}

Note: If the curly brackets {} are not used with if statements then the statement just next to it is only considered associated with the if statement. 
Example: 
 if (condition) statement 1; statement 2;

In this example, only statement 1 is considered to be associated with the if statement.

if else - https://codeconfig.in

Example

// C# program to illustrate if statement
using System;
public class CodeConfig
{   
 public static void Main(string[] args) 
   {      
    string name = "CodeConfig";        
    if (name == "CodeConfig")
    {          
      Console.WriteLine("CodeConfig");    
    } 
}}

Output: 
 CodeConfig

IF – else Statement 

The if statement evaluates code if a condition is true. But what happens when the condition is not true is where the else statement comes into play. if tells the code what to do if the condition is false.
Syntax: 
  if(condition) { // code if condition is true } else { // code if condition is false }

Example

// C# program to illustrate// 
//if-else statement with eaxample
using System;
public class CodeConfig{
    public static void Main(string[] args)  
  {        string name = "CodeConfig";     
   if (name == "CodeConfig") { 
           Console.WriteLine("CodeConfig-IFBlock");
        }      
  else {            Console.WriteLine("CodeConfig-ElseBlock"); 
       } 

   }
}

Output: 
 CodeConfig

If – else – if ladder Statement 
The if-else-if ladder statement executes the conditions of multiple statements. Execution starts from the beginning and all if conditions are checked. The statements in the if block are executed and evaluate to true. If none of the if conditions are true, the final else block is evaluated.
Syntax: 
  if(condition1) { // code to be executed if condition1 is true } else if(condition2) { // code to be executed if condition2 is true } else if(condition3) { // code to be executed if condition3 is true } … else { // code to be executed if all the conditions are false } 

Csharp

// C# program to illustrate// if-else-if ladder
using System;class CodeConfig
 {    
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 25");    
    else          
  Console.WriteLine("i is not present");    
}}

Output: 
 i is 25

Nested – If Statement 
An if statement within an if statement is called a nested if. In this case, the if statement becomes the target of another if or else statement. You can use nested conditions when multiple conditions must be true and one of the conditions is a subcondition of a parent condition.
Syntax: 
  if (condition1) { // code to be executed // if condition2 is true if (condition2) { // code to be executed // if condition2 is true } }

Flowchart:  

nested if else - https://codeconfig.in

Example:

// C# program to illustrate// nested-if statement
using System;
class CodeConfig
{   
 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");    
    }  
  }}


Output: 
 i is smaller than 12 too.

Switch Statement 


Switch statements are an alternative to long If-Else-If ladders. The expression is checked in different cases and a single match is performed. A Break statement is used to terminate a switch. If you do not use a break, control passes to all of the following cases until a break is found or the switch is removed: There is a default case at the end of the switch (optional). If none of the cases match, the default case is executed.
Syntax: 
 switch (expression) { case value1: // statement sequence break; case value2: // statement sequence break; . . . case valueN: // statement sequence break; default: // default statement sequence }

Example

// C# example for switch case
using System;
public class GFG{   
 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;    
    }   
 }}

Output: 
 case 30

Nested switch 
Nested Switch case is allowed in C#. In this case, switch is present inside other switch case. Inner switch is present in one of the cases in parent switch.

// C# example for nested switch case
using System;
public class GFG
{   
 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; 
       }  
  }}

Output: 
 5 4 3

In this article we tried to explain C# | Decision Making with Examples. I hope you enjoyed reading this article. For more information you can check out Microsoft learn.

For more information you can visit our C# Section for more such articles