Using switch statements and the ternary operator

和JSswitch的语法一样?

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 particular advantage in using one over the other. There‘s a switch statement can be easier to read and maintain if you need to test for many 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 particular case. So, this line of code has been executed. But of course, the same line of code would 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 decision 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.(跟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 of 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 Ternary Operator 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.

时间: 2024-10-18 14:04:27

Using switch statements and the ternary operator的相关文章

C--Conditional Statements

Conditionals Conditional expressions allow your programs to make decisions and take different forks in the road,depending on the value of variables or user input. C provides a few different ways to implement conditional expressions (also know as bran

python为什么不需要三目运算符和switch

对于三目运算符(ternary operator),python可以用conditional expressions来替代 如对于x<5?1:0可以用下面的方式来实现 1if x<5else 0 注: conditional expressions是在python 2.5之前引入的,所以以上代码仅适用于2.5以及之后的版本 对于2.5之前的版本,可以用下面这种形式 X<5and1or 0 对于switch,我们完全可以用dictionary来实现,看下面的例子 >>>d

coffeescript 1.8.0 documents

CoffeeScript is a little language that compiles into JavaScript. Underneath that awkward Java-esque patina, JavaScript has always had a gorgeous heart. CoffeeScript is an attempt to expose the good parts of JavaScript in a simple way. The golden rule

优秀的PHP开源项目集合

包管理Package Management Libraries for package and dependency management. Composer/Packagist– A package and dependency manager. Composer Installers– A multi framework Composer library installer. Package Management Related Libraries related to package ma

awesome-php中英文资源整理(同步更新)

中文版 收集整理一些常用的PHP类库, 资源以及技巧. 以便在工作中迅速的查找所需… 这个列表中的内容有来自 awesome-php 的翻译, 有来自开发者周刊以及个人的积累等. 一个前端组件的列表 awesome-frontend 推荐 学习资源 PHP相关的有参考价值的社区,博客,网站,文章,书籍,视频等资源 PHP网站(PHP Websites) PHP The Right Way – 一个PHP实践的快速参考指导 PHP Best Practices – 一个PHP最佳实践 PHP We

在vi中使用perltidy格式化perl代码

格式优美的perl代码不但让人赏心悦目,并且能够方便阅读. perltidy的是sourceforge的一个小项目,在我们写完乱七八糟的代码后,他能像变魔术一样把代码整理得漂美丽亮,快来体验一下吧!!! perltidy 主页: http://perltidy.sourceforge.net/perltidy.html 安装方法: 进入解压后的文件夹,然后运行一下命令 perl Makefile.PL make make test make install 用法: 配置一下vim,使得我们在写代

Unity 官方教程 学习

Interface & Essentials Using the Unity Interface 1.Interface Overview https://unity3d.com/cn/learn/tutorials/topics/interface-essentials/interface-overview?playlist=17090 2.The Scene View https://unity3d.com/cn/learn/tutorials/topics/interface-essent

[转载]两个半小时学会Perl

Learn Perl in about 2 hours 30 minutes By Sam Hughes Perl is a dynamic, dynamically-typed, high-level, scripting (interpreted) language most comparable with PHP and Python. Perl's syntax owes a lot to ancient shell scripting tools, and it is famed fo

官方的objective - c风格指南。

The official raywenderlich.com Objective-C style guide. This style guide outlines the coding conventions for raywenderlich.com. Introduction The reason we made this style guide was so that we could keep the code in our books, tutorials, and starter k