Learn Big Data-java basic-switch statements from scratch (6)

Source: Internet
Author: User

Tags: Big data Java CCIE learn big data from scratch


We start from scratch to learn big data technology, from Java Foundation, to Linux technology, and then deep into the big data technology of Hadoop, Spark, Storm technology, finally to the big Data enterprise platform building, layers of progressive, from point to face! Hope technology Daniel can come to guide the study.


The previous section learned about the Process Control statements, and this section learns the switch statements.

Before we begin, let's take a look at the exercises in the previous section.


First, the exercises

Practice for If statement format:

1. Get larger values in two data

2, determine whether a data is odd or even, and output

The results of the program execution are as follows:



Second, switch statement


Selection structure of Process Control statements

1 selection structure (also known as branching structure)

There are two types of statements for the selection structure, as follows:

    • If statement

    • Switch statement

The IF statement has been discussed earlier in this section to understand the next switch statement


Switch statement


Switch statement format:

switch (expression) {

Case value 1:

Statement body 1;

Break

Case Value 2:

Statement body 2;

Break

...

Default

Statement body n+1;

Break

}

Explanation of the format:

    • Switch: Indicates that this is a switch selection structure

    • Expression: The value of this place is limited

Byte,short,int,char

JDK5 can be enumerated later

JDK7 can be a string later

    • Case: followed by a value to compare with the expression

    • Statement body: The code to execute

    • Break: Indicates the meaning of interrupts and ends, and can control the end of a switch statement

    • Default: When all values and expressions do not match, the statement that executes the default control is actually equivalent to the IF statement's else


Execution process:

    1. First, the value of the expression is calculated and the case is compared

    2. Once there is a corresponding value, the corresponding statement body is executed.

    3. In the course of execution, the break will end

    4. Finally, if all the case does not match the value of the expression, the statement body in default is executed, and the program ends





Thinking:

    1. Can byte be used as a switch expression?

    2. Can long be used as a switch expression?

    3. Can string be used as an expression of switch?

Instance:

Keyboard input a data, according to this data, we output the corresponding week

    • Keyboard entry 1, corresponding output Monday

    • Keyboard input 2, corresponding output Tuesday

    • ...

    • Keyboard entry 7, corresponding output Sunday


Example Analysis:

1, keyboard input, with switch to achieve

2, we can use the IF statement, or you can use the switch statement

Program code:



Program execution Results:



switch statement considerations:


1 . The case can be followed only by a constant, not a variable, and the value after multiple occurrences cannot appear the same


2 . can default be omitted?
    • It can be omitted, but it is not recommended, because it is a hint to the incorrect situation.

    • Special cases: Casecan be fixed value (single option), you can consider omitting the default

3 . can break be omitted?
    • Can be omitted, will not error, but the final execution results are not what we want, there will be case penetration phenomenon.

    • Omitted is not recommended.


4.

must the default be at the end?
    • No, it can be anywhere, but recommended at the end, otherwise sinks produce case penetration.


5.

end condition of the switch statement


    • End when you encounter a break


Switch Considerations-Instance


Here we look at the relevant examples of the effect of attention to implementation.

1. Example ( Case ID & variable ID with the same name)

Program execution Results:

    • It can be seen that the case followed by a constant must not be a variable, and the value after the case cannot be the same.



2. example (omitting the default statement)

Program execution Results:

    • Visible, in the Swith statement, the default can be omitted, but will eventually lead to some incorrect situation is not excluded, can be entered normally. Although omitting default does not have an error, it is not recommended to omit it.



3. Example (Omit break)

Program execution Results:

    • It can be seen that, when the program executes, if there is no break for the program to interrupt, then the program executes sequentially in order until it is terminated. Although omitting the break does not have an error, it is not recommended to omit it, otherwise the program will print the result incorrectly.



4, example (the default statement does not put the last)

Program execution Results:

    • Visible, if the default is not put in the end, the program itself will not error, but the final execution, there will be case penetration.


5. Example: (switch statement end condition)

Program execution Results:

    • Visible, in the statement x, Y, encountered a break on the direct concluding sentence output;

    • In the statement A, B, no break is encountered, and the program executes until the final output.


extension Exercises (self-completed):

1, casually out of a choice problem, through the keyboard input, according to our choice to give your choice results.

2, according to the keyboard input string, to determine whether there is satisfied with the requirements, if there is output Otherwise, the prompt is wrong.


We'll get to the finish of this section, we'll learn the loop Structure statement in the next section


Previous article:


Starting from scratch, start with big Data-java Basics (1)

Starting from scratch Big Data-java basic Syntax (2)

Learn the big data-java operator from scratch (3)

Starting from scratch Big Data-java basics-Ternary operator/Keypad entry (4)

Starting from scratch Big Data-java basic-flow control Statement (5)




Learn Big Data-java basic-switch statements from scratch (6)

Related Article

Beyond APAC's No.1 Cloud

19.6% IaaS Market Share in Asia Pacific - Gartner IT Service report, 2018

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.