find and solve ||
Please wait.....

C# Switch Statement

Loops can execute a block of code as long as a specified condition is reached. Loops can be handy as they save time reducing errors and making code more readable.

C# While Loop

The while loop loops through a block of code as long as a specified condition is True. This loop starts with the while keyword, it must include a Boolean conditional expression inside brackets that returns either true or false. It executes code block until specified conditional expression returns false.

while (condition)
   // code block to be executed

In the example given below, code in loop will run over again and again as long as a variable(i) is less than 10:

int  I = 0;
while( i < 10)

Note: We must not forget to increase the variable used in the condition, otherwise loop will never end.

Some rules  apply to a switch statement in c#
  • The expression used in a switch statement must have an integral or enumerated type, or be of a class type in which the class has a single conversion function to an integral or enumerated type.
  • We can  any number of case statements within a switch. Each case is followed by the value to be compared to and a colon.
  • 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.
  • When the variable being switched on is equal to a case, the statements following that case will execute until a break statement is reached.
  • When a break statement is reached, the switch terminates, and the flow of control jumps to the next line following the switch statement.
  • Not every case needs to contain a break. If no break appears, then it will raise a compile time error.
  • 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.

using System;
public class Program
 public static void Main()
      int weekId = 2;
  string weekName="";
       switch (weekId) {
            case 1:
                Console.WriteLine("This is, {0}",weekName);
            case 2:
                Console.WriteLine("This is, {0}",weekName);
            case 3:
               Console.WriteLine("This is, {0}",weekName);
            case 4:
               Console.WriteLine("This is, {0}",weekName);
             case 5:
               Console.WriteLine("This is, {0}",weekName);
    case 6:
               Console.WriteLine("This is, {0}",weekName);

This is Monday

C# switch statement string

In C#, the switch statement is used to select one of many code blocks to be executed. The switch statement compares the value of a variable (also called the "switch expression") to the values of the case labels, and the code associated with the first matching case label is executed.

Here is an example of a switch statement that uses a string variable as the switch expression:

string color = "green"; switch (color) { case "red": Console.WriteLine("The color is red."); break; case "green": Console.WriteLine("The color is green."); break; case "blue": Console.WriteLine("The color is blue."); break; default: Console.WriteLine("The color is unknown."); break; }

In this example, the value of the color variable is "green", so the code associated with the "green" case label will be executed, and "The color is green." will be printed to the console.

Related information