Using switch statements and the ternary Operator

Source: Internet
Author: User

What is the same as jsswitch syntax?

Switch statements and the ternary operator are alternatives to if-else control structures for making decisions. the basic structure of a switch statement consists of the keyword switch followed by a pair of parentheses containing a value that you want to test. the body of the switch statement goes between a pair of curly braces. inside the braces, the case keyword preceeds the value you're looking for and the value is followed by a colon.

If the value passed to the switch statment matches, any code that follows is executed. in effect, this is the same as a conditional statement using if, and testing whether var equals value one. and on its own, it's a rather compass approach which you can test for multiple values by adding break at the end of each case and repeating the pattern. at the end of the switch block, you can add default followed by a colon and any code that you want to be executed if none of the values match.

You can also stack alternative cases like this. the effect is exactly the same as this control structure using if, elseif, and Else. there's no maid in using one over the other. there's a switch statement can be easier to read and maintain if you need to test for different options. let's take a quick look at the switch statement in operation. here, the value that I want to test for is name. and if the name is Arthur, then this line of code will be executed. break prevents any further code from being executed. but if the value is either Marvin or paranoid Android, then this line of code will be executed.

Again, brake prevents the code from going any further. and if name doesn't match any of the values proceeded by the case keyword. then the default is executed. so, if we test that in a browser. because Arthur is the name, the first block of code is executed. change the value of name to David. and refresh the browser. and this time, it doesn' t match any of the Case keywords.

So, the default is executed. and let's change it to paranoid android. and again, reload the browser. and we can see that it's matched this special case. so, this line of code has been executed. but of course, the same line of code wowould have been executed if the name had been Marvin. now, one of the things that you need to be very careful about with switch statements is if you forget to use break, the Code continues executing until it gets to the end of the switch block, or it gets to another break. so, let's just try that.

Reloading the browser. and you see, we 've now got not only, I 've got this terrible pain of the diodes. but also, the last piece of code, the default is also being executed. so, it's extremely important when you are using switch statements, to make sure that each block of code ends with break. unless, of course, you do want the default to be executed at the end anyway.

The other demo-making structure is the ternary operator which assigns different values to a variable depending on whether a condition is true or false. it's structure looks like this. (The same as JS) the condition follows the assignment operator and is followed by a question mark. to the right of the question mark is the value you want assign if the condition is true. next comes a colon, followed by the value if the condition is false. in effect, it's a short time way of writing this if, else statement.

Here's a simply example the ternary operator in action. as every fan of The hitchiker's Guide to the Galaxy will tell you, it took the computer deep thought seven and a half million years to work out that 42 was the answer to the ultimate question of life, the universe, and everything. so, if value isn't 42, well, let's see what the browser says. indeed, keep calculating. so, if we change the value to 42, we know that the result is the answer to the ultimate question of life, the universe, and everything. so, there you have it, two alternatives to If, else conditional statements. both can be difficult to understand in the early stages of working with PHP. but you'll find the ternoperator is a very convenient shorthand and is frequently used.

The switch statement is less common, but you definitely need to understand its basic structure and use.

Using switch statements and the ternary Operator

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.