Showing posts with label switch statement. Show all posts
Showing posts with label switch statement. Show all posts

Wednesday, 14 April 2021

Php: switch statement

The body of a switch statement is known as a switch block. A statement in the switch block can be labelled with one or more case or default labels. The switch statement evaluates its expression, then executes all statements that follow the matching case label.

 

Syntax

switch (expression) {
  case label1:
    ......
    ......
    break;

  case label2:
    ......
    ......
    break;

  case label3:
    ......
    ......
    break;
    
    ...
  default:
    .....
}

 

Example

switch ($day) {
    case 1:
        echo "Sunday";
        break;
    case 2:
        echo "Monday";
        break;
    case 3:
        echo "Tueday";
        break;
    case 4:
        echo "Wednesday";
        break;
    case 5:
        echo "ThursDay";
        break;
    case 6:
        echo "Friday";
        break;
    case 7:
        echo "Saturday";
        break;
    default:
        echo "You entered wrong day number";
}

 

Find the below working application.

 

switch_demo.php

#!/usr/bin/php

<?php
$day = 3;

switch ($day) {
    case 1:
        echo "Sunday";
        break;
    case 2:
        echo "Monday";
        break;
    case 3:
        echo "Tueday";
        break;
    case 4:
        echo "Wednesday";
        break;
    case 5:
        echo "ThursDay";
        break;
    case 6:
        echo "Friday";
        break;
    case 7:
        echo "Saturday";
        break;
    default:
        echo "You entered wrong day number";
}

?>

 

Output

$./switch_demo.php 

Tueday

 

Explanation

As you see in the above program, variable “day” set to the value 2. So in the switch case, case 2 is executed. If the day is set to 1, then case 1 will execute and come out of the switch.

 

You can see in the above program, each case has a break statement associates with it, is it necessary ? depends on the use case, you may use or may not.

 

For example, for Saturday and Sunday, I would like to print the message ‘Happy weekend!!!!’. You can combine both cases 1 and 7 like below.

switch ($day) {
    case 1:
    case 7:
        echo "Happy weekend!!!!";
        break;
    .....
    .....
}

 

Find the below working application.

 

switch_demo_1.php

 

#!/usr/bin/php

<?php
$day = 1;

switch ($day) {
    case 1:
    case 7:
        echo "Happy weekend!!!!";
        break;
    case 2:
        echo "Monday";
        break;
    case 3:
        echo "Tueday";
        break;
    case 4:
        echo "Wednesday";
        break;
    case 5:
        echo "ThursDay";
        break;
    case 6:
        echo "Friday";
        break;
    default:
        echo "You entered wrong day number";
}

?>

 

Output

$./switch_demo_1.php 

Happy weekend!!!!

Can I use strings in switch statements?

Yes, you can use strings in switch statement.

 

switch_strings_demo.php

#!/usr/bin/php

<?php
$day = "one";

switch ($day) {
    case 'one':
    case 'seven':
        echo "Happy weekend!!!!";
        break;
    case 'two':
        echo "Monday";
        break;
    case 'three':
        echo "Tueday";
        break;
    case 'four':
        echo "Wednesday";
        break;
    case 'five':
        echo "ThursDay";
        break;
    case 'six':
        echo "Friday";
        break;
    default:
        echo "Wrong input!!!!";
}

?>

 

Output

$./switch_strings_demo.php 

Happy weekend!!!!

 

Alternative switch syntax

switch (expression) :
  case label1:
    ......
    ......
    break;

  case label2:
    ......
    ......
    break;

  case label3:
    ......
    ......
    break;
    
    ...
  default:
    .....
endswitch;

 

switch_alternative_syntax.php

#!/usr/bin/php

<?php
$day = "one";

switch ($day) :
    case 'one':
    case 'seven':
        echo "Happy weekend!!!!";
        break;
    case 'two':
        echo "Monday";
        break;
    case 'three':
        echo "Tueday";
        break;
    case 'four':
        echo "Wednesday";
        break;
    case 'five':
        echo "ThursDay";
        break;
    case 'six':
        echo "Friday";
        break;
    default:
        echo "Wrong input!!!!";
endswitch;

?>

Output

$./switch_alternative_syntax.php 

Happy weekend!!!!

 


 

  

Previous                                                    Next                                                    Home

Sunday, 27 January 2019

Groovy: Pass custom objects to switch statement


Groovy allows custom objects to be tested against switch statement. A custom object can be used in switch case, only if it implements isCase method.

HelloWorld.groovy
class Employee{
 int id
 String name

 Employee(int id, String name){
  this.id = id
  this.name = name
 }

 boolean isCase(Employee emp) {
  return emp.id == this.id
 }
}

emp1 = new Employee(1, "Krishna")
emp2 = new Employee(2, "Ram")
emp3 = new Employee(1, "Ram")

switch(emp1){
 case emp2 : println "Both emp1 and emp2 has same id"; break
 case emp3 : println "Both emp1 and emp3 has same id"; break
 default : println "No match found"
}

Output
Both emp1 and emp3 has same id

Groovy even enhances all the types by implementing isCase method. You can check the type of a variable using switch statement.


HelloWorld.groovy
a = 10.1

switch(a){
 case Integer : println "$a is of type integer"; break
 case Float : println "$a is of type Float"; break
 case Double : println "$a is of type Double"; break
 case BigDecimal: println "$a is of type BigDecimal"; break
 default : println "Do not match to any type"
}

Output
10.1 is of type BigDecimal

You can even pass ranges to the switch statement.


HelloWorld.groovy
a = 10

switch(a){
 case 0..5 : println "$a is in range of (0..5)"; break
 case 5..10 : println "$a is in range of (5..10)"; break
 default : println "Do not match to any type"
}

Output
10 is in range of (5..10)

You can even use closures, regular expressions and any custom type that implements isCase method as case statement in switch.


HelloWorld.groovy
a = 10

switch(a){
 case 0..5 : println "$a is in range of (0..5)"; break
 case {it % 3 == 0} : println "$a is divisible by 3"; break
 case ~/.../ : println "$a is a 3 digit number"; break
 case Integer : println "$a is of type Integer"; break
 default: println "Do not match to any case"; break
}

Output
10 is of type Integer




Previous                                                 Next                                                 Home