FuzzDuck
FuzzDuck
Switch
FuzzDuck
Switch

Switch

Setting up different checkpoints, and run the code inside the checkpoint, which condition is true.

Switch can be used when we want to choose one of different code blocks to be run, depending on a given case. We run a parameter and checks which case it matches. If the parameter does not match any case, we run a default code block. After each case check, we use a break command to get out of the switch statement. It is possible to use multiple cases for the same code block to be run.

In the example below we have a switch statement, in which an int is checked and the case matching the int is run.

Code Example - In the example below we have a switch statement with a string parameter.
// --- Declaring starting point of the program run with "main" method --- //
public static void Main(string[] args){

 // --- Switch example with int number with value 3 --- //
 int number = 3;

 switch (number) {
  case 1: Console.WriteLine("Case 1 is run.");
  break;
  case 2: Console.WriteLine("Case 2 is run.");
  break;
  case 3: Console.WriteLine("Case 3 is run.");
  break;
  default: Console.WriteLine("Default code is run.");
  break;
 }

 // --- Switch example with String bird with value "Duck" --- //
 String bird = "Duck";

 switch (bird) {
  case "Goose": Console.WriteLine("Case Goose is run.");
  break;
  case "Turkey": Console.WriteLine("Case Turkey is run.");
  break;
  case "Duck": Console.WriteLine("Case Duck is run.");
  break;
  default: Console.WriteLine("Default code is run.");
  break;
 }

 // --- Invoking 'ReadKey()' to make time to read the output --- //
 Console.ReadKey();

}
// --- Declaring starting point of the program run with "main" method --- //
public static void main(String[] args) {

 // --- Switch example with int number with value 3 --- //
 int number = 3;

 switch (number) {
  case 1: System.out.println("Case 1 is run.");
  break;
  case 2: System.out.println("Case 2 is run.");
  break;
  case 3: System.out.println("Case 3 is run.");
  break;
  default: System.out.println("Default code is run.");
  break;
 }

 // --- Switch example with String bird with value "Duck" --- //
 String bird = "Duck";

 switch (bird) {
  case "Goose": System.out.println("Case Goose is run.");
  break;
  case "Turkey": System.out.println("Case Turkey is run.");
  break;
  case "Duck": System.out.println("Case Duck is run.");
  break;
  default: System.out.println("Default code is run.");
  break;
 }

}
// --- Switch example with int number with value 3 --- //
$number = 3;

switch ($number) {
 case 1: echo "Case 1 is run.";
 break;
 case 2: echo "Case 2 is run.";
 break;
 case 3: echo "Case 3 is run.";
 break;
 default: echo "Default code is run.";
 break;
}

// --- Switch example with String bird with value "Duck" --- //
$bird = "Duck";

switch ($bird) {
 case "Goose": echo "Case Goose is run.";
 break;
 case "Turkey": echo "Case Turkey is run.";
 break;
 case "Duck": echo "Case Duck is run.";
 break;
 default: echo "Default code is run.";
 break;
}