Saturday, September 6, 2014

The Selective Structure: Switch

THE SELECTIVE STRUCTURE: SWITCH

Ø    switch statement is a type of selection control mechanism used to allow the value of a variable or expression to change the control flow of program execution via a multi-way branch.

Ø    The switch statement body consists of a series of case labels, an optional default label and breaks:

a.      Case Statement
-          the case statement is the one which compares several possible constant values for an expression
b.      Default Statement
-          the default statement is executed when no case statement satisfies the inputted value or the value being switched

c.       Break
-          break makes the program jumps to the end of the switch selective structure once one of the case statements have been satisfied


Ø  How it works:
Switch evaluates expression and checks if it is equivalent to constant1, if it is, it executes a group of statements 1 until it finds the break statement. When it finds this break statement the program jumps to the end of the switch selective structure.
If expression was not equal to constant1 it will be checked against constant2. If it is equal to this, it will execute group of statements 2 until a break keyword is found, and then will jump to the end of the switch selective structure.
Finally, if the value of expression did not match any of the previously specified constants (you can include as many case labels as values you want to check), the program will execute the statements included after the default label, if it exists (since it is optional).

Ø  Things to remember when using Switch
·         It differs from the if statement in that switch can only test for equality whereas the if conditional expression can be of any type
·         No two case constants in the same switch can have identical values
·         A switch statement is more efficient than the if-else ladder


38 comments:

  1. ahh.. now i know!!!

    ReplyDelete
  2. Replies
    1. the samples are on my other post.. kindly visit and comment on it.. thanks!!

      Delete
  3. Thanks! this helps me verywell ^^

    ReplyDelete
    Replies
    1. you are very much welcome.. thanks too for visiting and commenting in this blog!

      Delete
  4. Thanks for the info.

    ReplyDelete
  5. ahh so this is the switch statement that ive been looking for.. thanks!!

    ReplyDelete
  6. this is the switch statement that i've been looking for..

    ReplyDelete
  7. i prefer this simple explanation over the wordy ones

    ReplyDelete
  8. finally i found you!

    ReplyDelete
  9. very much alike with the if else statement

    ReplyDelete
  10. It's helpful esp. for the students taking programming subject.

    ReplyDelete
  11. A brilliant blog! Indeed!

    ReplyDelete
  12. Thanks for the blog like this. :)

    ReplyDelete
  13. yehey.. i got this!

    ReplyDelete
  14. thanks for helping us students to do our assignments more easily.

    ReplyDelete
    Replies
    1. thanks too for visiting and commenting on my blog..

      Delete
  15. partially related to if else statements

    ReplyDelete
  16. NAPAKA DETAILED kaya ishare na to!

    ReplyDelete
  17. the title SWITCH explains the whole concept of the topic..

    ReplyDelete
  18. a reliable source...

    ReplyDelete
  19. its like you're choosing among a series of choices to be executed depending on your like... now i get it!

    ReplyDelete