What is a Switch Statement in computer language?

Card Puncher Data Processing

About

A switch is a multi-way conditional branch that permits to control the flow

Syntax

There is basically two syntax:

In some language such as SQL, the case word is used in place of switch

Value Switch

Php:

switch ($i) {
    case 0:
        echo "i equals 0";
        break;
    case 1:
        echo "i equals 1";
        break;
    default:
       echo "i is not equal to 0 or 1";
}

Sql

CASE  Promotions."Promo Subcategory"
WHEN  'TV commercial'          then 'Commercial'
WHEN  'TV program sponsorship' then 'Sponsor'
ELSE  'Default'
END

Expression Switch

Php:

switch (true) {
    case $start - $offset === 1:
        print "A";
        break;
    case $start - $offset === 2:
        print "B";
        break;
}

Sql:

CASE WHEN "Sales Facts"."Amount Sold" > 600000  THEN 'Big'
     WHEN "Sales Facts"."Amount Sold" > 300000  THEN 'Middle'
     ELSE 'Small' END

Fall through behavior

The switch statement has fall through behavior therefore the break is important.

If you omit the break from a case, and that case matches or runs, execution will continue to the next case.

Without break

color = "green"
switch (color) { 
    case "blue": 
         console.log("blue");
    case "green": 
          console.log("green");
    case "red": 
    case "orange":
         console.log("red or orange");
    default:
         console.log("default");
}
  • Result: You will see all console output from the green case

With ''break'

color = "green"
switch (color) { 
    case "blue": 
         console.log("blue");
         break;
    case "green": 
          console.log("green");
          break;
    case "red": 
    case "orange": 
          console.log("red or orange");
          break;
    default:
         console.log("default");
}
  • Result: you will see only one output





Discover More
How to use the Switch Statement in Javascript?

This page is the switch statement in Javascript If you omit a break from a case, and that case matches or runs, execution will continue with the next case. Always create a block after each case...
Java Conceptuel Diagram
How to use the Switch expression (case) in Java?

This article is the implementation of the Switch branching statement in Java primitive typeenumcaseintInteger The value switch is supported. Java does not support expression switch, you should...
Card Puncher Data Processing
Language - Control Flow Statement (If, Switch, For) - Conditional Operators - Execution order - Branch Conditionals (aka decisions)

conditional are expressions that: determines the next statement to execute by evaluating a comparison expression Ultimately, they are predicate that returns a boolean. They model a flow of statements....
Card Puncher Data Processing
Php - Switch

This page is the switch expression in php continue in a switch is equivalent to a break. You should use then continue 2 This is one of the php control flow statement. Fall out:
Data System Architecture
What is the SQL case statement ? (or switch)

In sql, the case expression is a conditional expression that is the equivalent to the more general switch expression (same function, just another word). As for a switch statement, there is also two...



Share this page:
Follow us:
Task Runner