The Java conditional statement

Source: Internet
Author: User
Tags case statement logical operators

In life, we often need to make judgments before deciding whether to do something. For example, if your test score is greater than 90, reward an IPHONE 5S. You can use the if condition statement to implement situations where you need to determine the condition before the condition is met.

Grammar:

Execution process:

Such as:

Note: If the IF condition is set when the execution statement is only one, it is possible to omit the curly brace drop! But if there are multiple execution statements, then curly braces are indispensable.

The if...else of the Java conditional statement

The operation of the If...else statement is one step more than an if statement: when the condition is true, the code block of the if part is executed, and if the condition is not true, the else part is entered. For example, if the test result is more than 90 points, reward an IPHONE 5S, or do 500 push-ups.

Grammar:

Execution process:

Such as:

The multiple if of a Java conditional statement

If the multiple if statement is not satisfied with condition 1, the condition 2 is judged, and the code within the Else block is executed when the preceding conditions are not true. For example, if the test score is greater than 90, then an IPHONE 5S is rewarded, and if the score is between 70 and 90, a red meter is rewarded, or 500 push-ups are punished.

Grammar:

Execution process:

Such as:

The meaning of this code is: if the score value is greater than 90, then reward an Iphone 5s, when the score value is less than or equal to 90 o'clock, first determine whether the score is greater than 70, if score is between 70--90, then reward a red meter, if the score is less than or equal to 70 , 500 push-ups will be punished.

The use of multiple if statements is very handy when the condition to be judged is a continuous interval!

Nested if of a Java conditional statement

Nested IF statements determine the condition of the inner if if the condition of the outer if is true. For example, the schedule of activities, if today is a working day, then go to work, if today is the weekend, then go out to play, at the same time, if the weather is clear on weekends, the outdoor playground to play, otherwise go to the indoor playground.

Grammar:

Execution process:

For example:

Run the result: go to the outdoor playground to play

Switch of the Java conditional statement

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 is the same as the value after the case statement, it starts down from that position until the break statement is encountered or the switch statement block ends, and the code of the default block is executed if there is no matching case statement.

Such as:

Have to say a few little secrets:

1, switch the value of the expression after the parentheses must be integer or character type

2. The value after the case can be a constant value , such as 1, 2, or a constant expression , such as a. T, but not a variable or an expression with a variable, such as a * 2

3, case matching, execute the program code in the matching block, if not met break will continue to execute the contents of the next case block until the break statement or switch statement block end as

Operation Result:

4, the function of the same case statements can be combined, such as

5, the default block can appear in any position , you can also omit

The while of the Java Loop statement

In life, there are times when you need to repeat certain actions in order to accomplish a task. If you participate in a 10000-metre long distance running, you need to run 25 laps around the 400-metre track repeatedly. When implementing functionality in Java, it is often necessary to execute some code repeatedly, for example, in order to express "strong love", we want to output 1000 lines "I adore class net!" ”。 Obviously, at this time repeatedly knocked 1000 times output statement is not reliable drop!! So, there is a good way to solve the wood? There, looping statements !

3 types of loops commonly used in Java: While , Do...while, for

We're going to talk about while in this section.

Grammar:

Execution process:

< 1 >, determine if the condition behind the while is established (True/false)

< 2 >, when the condition is established, execute the operation code within the loop, then repeat < 1 >, < 2, until the loop condition is not established

Features: first judgment, after execution

Such as:

The do...while of the Java Loop statement

The Do...while loop is somewhat similar to the while loop syntax, but the execution process is quite different.

Grammar:

Execution process:

<1>, perform the cycle operation first, and then determine if the loop condition is true

<2>, if the conditions are established, continue to implement < 1 >, < 2, until the cycle conditions are not established

Features: first execution, after judgment

Thus, the Do...while statement guarantees that the loop is executed at least once !

For example, still output 1000 times "I adore class net", using Do...while implementation code:

The Java Loop statement for

In the Java loop structure, there is a for loop in addition to the while and Do...while, and three loops can be replaced with each other.

Grammar:

Execution process:

<1>, executes the loop variable initialization section, sets the initial state of the loop, which executes only once in the entire loop

<2>, the judgment of the cyclic condition, if the condition is true, the loop body code is executed, and if False, the loop is exited directly

<3>, execute cyclic variable change part, change the value of the loop variable, in order to make the next condition judgment

<4>, re-execute < 2 >, < 3 >, < 4, until Exit loop

Features: simpler and easier to read than the while and Do...while statement structure

For example, output 1000 times "I adore the net", use for the implementation code is:

Some small details to keep in mind:

1. The three expressions in parentheses after the For keyword must be separated by ";", and three expressions can be omitted, but ";" cannot be omitted.

A. Omit "loop variable initialization", which can be initialized by an assignment statement before the for statement, such as:

B. Omitting "cyclic conditions" may cause the cycle to continue, which is what we often call a "dead loop" phenomenon, such as:

Avoid the appearance of a "dead loop" during programming, so for the above code you can use break to force out loops in the loop body (the usage of break is described later).

C. Omit "Cyclic variable change", you can change the loop variable in the loop body, such as:

2. For loop variable initialization and cyclic variable changes, you can use "," to initialize or change the values of multiple loop variables, such as:

In the code, the initialization variable part simultaneously assigns the initial value to two variables I and J, and the change part of the cyclic variable also changes the two variables, running the result:

3, the loop condition part can use the combination of logical operators expression, to express complex judgment conditions, but must pay attention to the priority of the operation, such as:

The code must satisfy both the variable i is less than 10, and I do not wait until 5 o'clock to loop, the value of the output variable i.

Break of the Java loop jump statement

In life, we often interrupt the scheduled task for some reason. If the 10000-metre long run, only ran 500 meters due to lack of physical strength, need to quit the game. In Java, we can use the break statement to exit the specified loop and execute the code immediately after the loop.

For example, use a numeric value that loops out 1--10, where the value is greater than 2, and a multiple of 3 stops the output.

Implementation code:

Operation Result:

The continue of the Java loop jump statement

The function of continue is to skip the remaining statements in the loop body to perform the next loop.

For example, to print all the even numbers between 1--10, use the continue statement to implement the code:

Operation Result:

Multiple loops of a Java looping statement

The structure of a loop body that contains a looping statement is called a multiple loop . Three loop statements can be nested within themselves or nested within each other, the most common is the double cycle . In a double loop, the outer loop executes once, and the inner loop executes a circle.

As shown below:

For example: use * to print rectangles:

The implementation code is:

Execution Flow: When i = 1 o'clock, the outer loop condition is established, enters the inner loop, and begins to print the first line of content. At this point, J starts at 1, loops 8 times, wraps at the end of the inner loop, and achieves the output of the first row of 8 *. Next, return the outer loop I to 2, prepare to print the next line, and so on, until the rectangle is finished printing.

The Java conditional statement

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.