Statement:Branching statements: if,if else,if ElseIf else,switch caseLooping statements: For,while,do While,foreachJump statement: Break,continueException statement: Try Catch finallyBranch statements:int a=5;if (a>0){Console.WriteLine ("This is a positive number");}Esle//If the a{Console.WriteLine ("This is a number less than or equal to 0");}else if (a>10){Cons
A switch statement is like a series of if statements to the same expression. In many times, you want to compare the same variable (or expression) with many different values, and perform different pieces of the program based on different comparison results. This is the use of the switch statement.
The following two exa
PhpSwitch StatementThe switch statement is used to perform different actions based on several different conditions.If you want to selectively execute one of several blocks of code , use the switch statement.GrammarSwitch (n){ Case Label1: If N=label1, the code here will be executed; Break Case LABEL2: If N=label2, the code here will be execut
When a case fails, it does not have a break statement, but there is a comment at the location of the break statement, which you see in the following case a note. When you write a switch statement, use the following format:
The code is as follows
Copy Code
Two days ago I flipped through the lab and saw that the first chapter of deep understanding of computer systems mentioned a problem. Is the switch statement the same as if else? Why? I was very interested in preparing for this.
However, after Google finds that other people's blogs talk about the same thing, let me learn -_-
In short, when the values of the switch
The C language does not limit the number of branches that if else can handle, but when there are too many branches, it is not convenient to use if else, and it is easy to have an if else pairing error. For example, enter an integer that outputs the corresponding number of days of the week in English for that integer:
#include
Run Result:
Input integer Number:3Wednesday
The C language also provides an alternative statement--
Switch statement• SyntaxStruct is used for integer typeThe flag after case must be a constant at compile time.The scaling parameter does not indicate the scaling of the range.String DaySuffix (int days){String result = "th ";If (days/10! = 1)Switch (days % 10){Case 1:Result = "st"; break;Case 2:Result = "nd"; break;Case 3:Result = "rd"; break;Default: // indicate
Switch...case statements are often used in the C language, let me show you a detailed question to be aware of when using this statement. Words not much to say, directly cite examples:Example 1:Switch (fruit){Case 1:printf ("Apple"); BreakCase 2:printf ("banana"); BreakCase 3:printf ("Orange"); BreakCase 4:printf ("pear"); BreakCase 5:printf ("Grape"); Break}When
Branch statements:if else if else; switch case--How to use if else if else:Console.WriteLine ("Please Punch");--Output "please punch"int a = Convert.ToInt32 (Console.ReadLine ());--Define a variable aRandom i = new random ();--Random numberint x = I.next (3);--variable x can be randomly given a number of 0-3if ((a-x = = 1) | | (A-x = =-2)){Console.WriteLine ("PC =" + x);Console.WriteLine ("Man wins");}else if (x==a){Console.WriteLine ("PC =" + x);Cons
When we use a switch statement in traversal, we sometimes need to terminate the entire traversal rather than the switch statement, so the implementation of the label statement is necessary.//Tags can be used to mark a loop body or switch
The Switch...case statement in Swift can determine the object type, and the objective-c must be an integer.Can not penetrate, can not write break,
var rank = "A"
switch rank{case
"a"://equivalent to if
print ("excellent") Case
"B"://equivalent to else if
print ("excellent")
case "C"://equivalent to else if
print ("excellent")
default:/
Answer supplementYou can do this easily: Switch (a) {Case 0: Case 1: Case 2: printf ("lslksdjfl/N"); Case 3: Case 4: case 5: printf ("sdlfjsdlf/N"); default: printf ....}
Answer supplementCan you use the if statement?
|All I know is that the switch does not have the if range limit.
|In this case, Case 0: Case 1: Case 2: Case 3: .. case 10: Default: Here is the
Jdk7 a constant in a new switch statement can be of type string,For example:@Testpublic void Test_1 () {String string = "Hello"; switch (string) {case "Hello": System.out.println (String); Default:throw New IllegalArgumentException ("illegal parameter");}}Behind the syntactic sugar, its practical treatment of string types, using the Hashcode () method to convert.
The switch case where you learned the choice statement today is a multi-select scenario that you can use.Case:Enter the month and the number of the output is how many days this yearThe 1,3,5,7,8,10,12 month is 31 days a year.This year's February is 28 days the other is 30 intM1 = to, M2 = -, M3 = to, M4 = -, M5 = to, M6 = -, M7 = to, M8 = to, M9 = -, M10 = to, M11 = -; ----don't need t
The variable definition inside the switch:1 inti =1;2 Switch. IO3 {4 Case 0:5 stringStr//Error6 intVal1 =0;//Error7 intVal2;// Right8 intVal3; Val3 =0;// Right9 Case 1:TenVal2 =9; Onecout Endl; A}C + + Primer P163: If a variable with an initial value is somewhere outside the scope and the variable is in scope at another location, then the behavior of jumping from on
"); Break;case South:System.out.println (" South "); break;default:break;} I can play the east in the switch statement, but the switch statement alone in the east does not come out//what is the principle?}}Results:Java is good, worth learning.Learning resources: Itcast and Itheima Video library. If you have public reso
This article is still written for beginners, and the following is for all the branch structures and switch branches of if, and hopefully it will help beginners.
An If statement includes a single condition, a single branch, a single condition, a double branch, and a multiple-condition multiple branch.
1. Single condition, single branch: parentheses () The value inside the bracket () must be a Boolean, follow
This article describes the use of switch statements in PHP, which is similar to the switch usage in other statements, but note that there is a break statement.
Standard syntax for switch statements in PHP:
switch (expression)
{case
Label1: The code to is
executed i
Use the switch statement to select one of several code blocks to execute.GrammarSwitch (n) {Case 1: Execute code block 1 break;case 2: Execute code block 2 break;default: N code that executes when different from 1 and 2How it works: first set an expression n (usually a variable). The value of the subsequent expression is compared to the value of each case in the structure. If there is a match, the code
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.