C# Program Control Structures

 General audience classification icon  General audience classification icon
C# [1] Specifying one or more conditions to be evaluated or tested by the program requires decision-making structures. The proper statements must be performed if the condition is true or false.

C# provides the following types of decision-making statements (table 1):

Table 1: C# 2010 Loops Definitions
Sr.No. Loop Type & Description
1 An if statement consists of a boolean expression followed by one or more statements
2 if…else statement – an if statement can be followed by an optional else statement, which executes when the boolean expression is false
3 Nested if statements – you can use one if or else if statement inside another if or else if statement(s)
4 switch statement – a switch statement allows a variable to be tested for equality against a list of values
5 Nested switch statements – you can use one switch statement inside another switch statement(s)
6 The ? Operator

if Statement
An if statement consists of a boolean expression followed by one or more statements. The syntax of an if statement in C# is:

if(boolean_expression) {
 /* Statement(s) will execute if the boolean expression is true */
}

If the boolean expression evaluates to true, the code block inside the if statement is executed. If the boolean expression evaluates to false, the first code set after the end of the if statement (after the closing curly brace) is executed.

Example:

using System;
 
namespace DecisionMaking {
  class Program {
    static void Main(string[] args) {
      /* Local variable definition */
      int a = 10;
 
      /* Check the boolean condition using if statement */
      if (a < 20) {
        /* If condition is true then print the following */
        Console.WriteLine("a is less than 20");
      }
      Console.WriteLine("value of a is : {0}", a);
      Console.ReadLine();
    }
  }
}

Output:

a is less than 20;
value of a is : 10

if…else Statement
An if statement can be followed by an optional else statement, which executes when the boolean expression is false. The syntax of an if…else statement in C# is:

if(boolean_expression) {
  /* Statement(s) will execute if the boolean expression is true */
} else {
  /* Statement(s) will execute if the boolean expression is false */
}

If the boolean expression evaluates to true, then the if block of code is executed; otherwise, else block of code is executed.

Example:

using System;
 
namespace DecisionMaking {
  class Program {
    static void Main(string[] args) {
      /* Local variable definition */
      int a = 100;
 
      /* Check the boolean condition */
      if (a < 20) {
        /* If condition is true then print the following */
        Console.WriteLine("a is less than 20");
      } else {
        /* If condition is false then print the following */
        Console.WriteLine("a is not less than 20");
      }
      Console.WriteLine("value of a is : {0}", a);
      Console.ReadLine();
    }
  }
}

Output:

a is not less than 20;
value of a is : 100

Nested if Statement
It is always legal in C# to nest if…else statements, which means you can use one if or else if statement inside another if or else if statement(s). The syntax for a nested if statement is as follows:

if( boolean_expression 1) {
  /* Executes when the boolean expression 1 is true */
  if(boolean_expression 2) {
    /* Executes when the boolean expression 2 is true */
  }
}

Example:

using System;
 
namespace DecisionMaking {
  class Program {
    static void Main(string[] args) {
      //* Local variable definition */
      int a = 100;
      int b = 200;
 
      /* Check the boolean condition */
      if (a == 100) {
 
        /* If condition is true then check the following */
        if (b == 200) {
          /* If condition is true then print the following */
          Console.WriteLine("Value of a is 100 and b is 200");
        }
      }
      Console.WriteLine("Exact value of a is : {0}", a);
      Console.WriteLine("Exact value of b is : {0}", b);
      Console.ReadLine();
    }
  }
}

Output:

Value of a is 100 and b is 200
Exact value of a is : 100
Exact value of b is : 200

switch Statement
A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable switched on is checked for each switch case.

The syntax for a switch statement in C# is as follows:

switch(expression) {
  case constant-expression  :
    statement(s);
    break; /* Optional */
  case constant-expression  :
    statement(s);
    break; /* Optional */
  
  /* You can have any number of case statements */
  default : /* Optional */
  statement(s);
}

The following rules apply to a switch statement.

  1. The expression in a switch statement must have an integral or enumerated type or a class type in which the class has a single conversion function to an integral or enumerated type.
  2. You can have any number of case statements within a switch. Each case is followed by the value to be compared to and a colon.
  3. The constant expression for a case must be the same data type as the variable in the switch, and it must be a constant or a literal.
  4. When the variable switched on is equal to a case, the statements following that case will execute until a break statement is reached.
  5. When a break statement is reached, the switch terminates, and the control flow jumps to the next line following the switch statement.
  6. Not every case needs to contain a break. If no break appears, the control flow will fall through to subsequent cases until a break is reached.
  7. A switch statement can have an optional default case, which must appear at the end of the switch. The default case can be used for performing a task when none of the cases is true. No break is needed in the default case.

Example:

using System;
 
namespace DecisionMaking {
  class Program {
    static void Main(string[] args) {
      /* Local variable definition */
      char grade = 'B';
 
      switch (grade) {
        case 'A':
          Console.WriteLine("Excellent!");
          break;
        case 'B':
        case 'C':
          Console.WriteLine("Well done");
          break;
        case 'D':
          Console.WriteLine("You passed");
          break;
        case 'F':
          Console.WriteLine("Better try again");
          break;
        default:
          Console.WriteLine("Invalid grade");
          break;
      }
      Console.WriteLine("Your grade is  {0}", grade);
      Console.ReadLine();
    }
  }
}

Output:

Well done
Your grade is B

Nested switch Statement
It is possible to have a switch as part of an outer switchstatement sequence. No conflicts will arise even if the case constants of the inner and outer switch contain common values.

The syntax for a nested switch statement is as follows:

switch(ch1) {
  case 'A':
    Console.WriteLine("This A is part of outer switch" );
   
  switch(ch2) {
    case 'A':
      Console.WriteLine("This A is part of inner switch" );
      break;
    case 'B': /* Inner B case code */
  }
  break;
  case 'B': /* Outer B case code */
}

Example:
<code C>
using System;

namespace DecisionMaking {
  class Program {
    static void Main(string[] args) {
      int a = 100;
      int b = 200;
         
      switch (a) {
        case 100: 
          Console.WriteLine("This is part of outer switch ");
            
          switch (b) {
            case 200:
              Console.WriteLine("This is part of inner switch ");
              break;
          }
        break;
      }
      Console.WriteLine("Exact value of a is : {0}", a);
      Console.WriteLine("Exact value of b is : {0}", b);
      Console.ReadLine();
    }
  }
} 

Output:

This is part of outer switch
This is part of inner switch
Exact value of a is : 100
Exact value of b is : 200
en/iot-open/getting_familiar_with_your_hardware_rtu_itmo_sut/raspberrypi_rpi/program_control_structures_winiot.txt · Last modified: 2023/11/23 10:29 by pczekalski
CC Attribution-Share Alike 4.0 International
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0