Let's talk about the format of the switch () statement
switch (expression) {
Case Match 1:
When matching 1 and the expression match the successful execution of the Code;
Break
Case Match 2:
When matching 2 and the expression match the successful execution of the Code;
Break
Default
If the case statement does n
Switch statements are also the most common logical control statements in object-oriented programming. The Switch statement is used to execute different actions based on multiple different conditions-that is, when the conditions are different, different logical operations are executed. This article describes the usage of the s
The switch statement executes one line at a line (actually a statement-taking statement), and here's how multiple values for PHP switch statements match the same code block Let's talk about the format of the switch ()
(" has a great website ") Case
" Tim Cook ":
println (" CEO of Apple Inc. ")
Default:
println ("Someone Else")
}
In addition to being able to use a switch for strings, note here are some interesting things to do. Did not see the break. Because the execution of a case statement in the switch is not done down. No more chance to appear bug!
And for insta
A switch is a statement that is very similar to an if statement. However, a switch allows a condition to have multiple values.The basic structure of the program is as followsSwitch (textval){Case Val:Program codeBreakCase VAL2:Program codeBreakCase VAL3:Program codeBreak......Case VALN:Program codeBreak}A
The Switch statement in PHP is used to perform different actions based on a number of different conditions.
Switch Statement
If you want to selectively execute one of several blocks of code, use the Switch statement.
Use the
One, switch syntaxis a multi-branch statement that determines which branch to execute by judging the value of an expressionB Reak is used to end a case and then execute a statement other than switchS witch-switch: case-condition switch determines what happensSecond,
First, let's talk about the format of the switch () statement.Switch (expression ){Case match 1:Code that is successfully executed when match 1 and expression match;Break;Case matching 2:Code that is successfully executed when matching 2 and expression match;Break;Default:If the case statement does not have the code successfully executed with the expression;}It is very important to understand how a
Switch statement
Working principle:
1. Perform a single calculation on an expression (usually a variable)2. Compare the value of an expression with a case in a structure3. If there is a match, execute the code associated with the case4. After the code executes, the break statement blocks the code from jumping into the next case to continue execution5. If no cas
Use the Switch statement to avoid lengthy if. ElseIf. else code block.
How the PHP switch statement works:
Perform a single calculation on an expression (usually a variable)Compare the value of an expression with the value of a case in the structureIf there is a match, the code associated with the case is executedAfter
You can use multiple if ... else if statements, such as the previous section, to perform multiple branches. However, this is not always the best solution, especially when all branches depend on the value of a single variable.
Starting with JavaScript1.2, you can use it to handle exactly this case, using a switch statement, which is more efficient if not repeatedly using the IF ... else if statement.Grammar
The switch statement is also the longest common logical control statement in object-oriented programming statements. The Switch statement is used to perform different actions based on multiple different conditions-that is, when the conditions are different, perform different
.
The code is as follows
$f = "/usr/local/lib/php3.ini";if (file_exists ($f)) {echo "The following is the PHP3 profile ReadFile ($f);echo "else echo "I'm sorry, I can't find $f";?>
--------------------------------------------------------------------------------
The third type is recursive if. else loops, usually used in a variety of decision judgments. It will be several if.. Else take to combine the use of processing.
Look directly at the example below
Example of how to use a command object to replace the switch statement
This article describes how to use a command object to replace the switch statement. The switch statement is forbidden in the JS specification. Using the comman
This article describes how to use a command object to replace the switch statement. The switch statement is forbidden in the JS specification. Using the command object can perfectly solve this problem, if you need a friend, you can refer to someone who once said that there is no such program .. else, of course,
* number will not skip a specified number of columns of data, at the time of input, in the corresponding position to enter the * number.What is the difference between%LF and%f.L for input of Long Integer data (available%ld,%lo,%lx,%lu) and double type data (with%LF or%le)Double A;scanf ("%lf", a);//cannot use scanf ("%f", a);printf ("%lf", a);//can also be used with printf ("%f", a);float B;scanf ("%f", a);//cannot use scanf ("%lf", a);printf ("%f", a);//can also be used with printf ("%lf", a);
In an IF condition statement, a logical condition can only have one, and if there are multiple conditions, it can be resolved using nested IF statements, but this method increases the complexity of the program and reduces the readability of the program.
Using the switch Process Control statement can solve these problems perfectly, with the following basic struct
Today in the project encountered the use of switch statements to determine the condition, but the problem is more conditions, about dozens of conditions, full screen case judgment, whether there is a more elegant alternative to the switch statement? Suppose there is a scenario where the shopping malls often take different discounts according to the situation, and
This article mainly introduces the use of the Command object to replace the switch statement example, JS specification inside is prohibited to use the switch statement, the use of command objects can be a perfect solution to this problem, the need for friends can refer to the
It has been said that a really good progra
123Switch statement, the switch statement is used to perform different actions based on several different conditions. 4 If you want to selectively execute one of several blocks of code, also use a switch statement. 56 The syntax structure is as follows:78
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.