Java Control statement -- switch statement and modify witch statement
The equivalent judgment of the above if statement can be replaced by switch.
Note that break is usually added
has the specific syntax rule, the code must carry on the concrete logical operation to judge, the logical operation result has two, therefore produces the choice, executes the different code according to the different choice.
The Java language provides two types of selection structure statements:
If statement
Switch state
When there are too many conditional judgment statements (if statements), you can use switch statements to write them. The basic structure of a switch statement is:switch (integer) {Case Integer value 1: statement; BreakCase Integer value 2: statement; BreakCase Integer value
JAVA if Condition Statement, switch multi-branch structure, and logical operators, switch operators
I. if condition statements
Example:
Import java. util. role; public class Test {public static void main (String [] args) {role in = new role (System. in); System. out. println
Java selection structure (if statement switch statement)1.IF Selection Structure:①if (Boolean expression) {If the Boolean expression is true, the statement executed}* If there is only one statement after the IF
Java programming those things 34 -- switch statement syntax Zhengzhou game college Chen yuefeng from: http://blog.csdn.net/mailbomb
5.3.2 switch statementThe Chinese meaning of the switch keyword is the meaning of switch and conve
Multi-Select structure switch statement
In Java, a switch statement is specifically provided for the multipath branching selection process, and the switch statement chooses one of the
the statement in default is still related to the position of default in the sentence (this sentence is close to the nonsense, but when there is no break in the case statement, the entire switch is similar to the sequential execution)3. When there is no break in the case, its execution order and execution is continued down until the
) Execution process:A: Calculate the value of an expression firstB: Match each case, and if there is one, execute the corresponding statement body and see the break end.C: If there is no match, execute the default statement body n+1.(4) Precautions:A:case can only be constants, cannot be variables, and values after multiple case cannot appear the sameCan B:default be omitted?It can be omitted, but it is not
Switch syntax
The code is as follows
Copy Code
switch (expression){case constant Expression 1: statement 1;....Case constant Expression 2: statement 2;Default: statement;}
Default is to execute it if it is not matched, and default is no
) {//Create Object Scannersc=newscanner (system.in); System.out.println ("Please enter the first integer!"); Intx=sc.nextint (); System.out.println ("Please enter a second integer!"); Inty=sc.nextint (); system.out.println (" Please select the action you want to do:! "); System.out.println ("1: addition 2: Subtraction 3: Multiplication 4: Division"); intchoose=sc.nextint (); //inta =2;switch (choose) {case1:system.out.println ("sum of two numbers is:"
Switch statement is a single conditional multi-branch switch statement, its general format is defined as follows ( where The break statement is optional):Switch ( expression ){Case constant Value :a number of statementsBreakCase
Synchronize the original text to http://www.waylau.com/java-switch-use-string/ When I try to use the string parameter in a switch statement (note ctrType as a string) switch (ctrType) { case "01" : exceptionType = "读FC参数数据"; break; case "03" :
A switch statement consists of a control expression and multiple case labels.The types supported by the switch control expressions are byte, short, char, int, enum (Java 5), String (Java 7).The Switch-case
Using a switch statement is more concise when you need to make an equivalent judgment on an option. For example: According to the rank of the exam, give the top 4 different prizes. First prize notebook one, second prize IPAD 21, Third prize mobile power one; the last one to reward u disk.Grammar:Execution procedure: When the value of the expression after switch i
I. OverviewThe judging structure of the IF statement can be regarded as a judgment question, and the choice structure of switch is regarded as the choice problem. Judgement two results choose one, the choice of the question is more.A preliminary understanding of switch: the difference between switch and if, in addition
In the switch statement in Java, each case branch is a single entry, and if none of the conditions are met, it will go to the default branch. So will the location of the default branch affect the execution process? Packagecom.app.statement;importjava.util.scanner;/***createdbycharles on2015/7/12.*/publicclassSwitchTest{public Staticvoidmain (String[]args) { Syste
The format of the switch statement is as follows: (its function is to select a piece of code execution)
Copy Code code as follows:
Switch (integer selection factor) {
Case Integer value 1: statement; Break
Case Integer value 2: statement; Break
Case Integer valu
The switch case statement is used to determine whether the expression following the case is consistent with the switch expression, executes the case statement if it matches, and then break out. While default is executed without a case (it does not break out of the box), Defaul is equivalent to the "third case" and is n
PackageCom.swift; Public classSwitch_test { Public Static voidMain (string[] args) {/** Can the switch statement function on a byte, can it function on a long, or can it be on a string? */ byteZijie = 3; System.out.println (Zijie); LongChangzheng=3; Switch(Changzheng) {//cannot switch on a value of type l
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.