Java Process Control Statements

Source: Internet
Author: User

Sequential statements

Statement: A semicolon-delimited code is called as a statement.

Note: No code is written just a semicolon, but also a statement, called an empty statement.

Sequential statements are statements that are executed in the order they are performed from the top down.

Judgment (If...else)

In the process of looking for a job, we ask for more than two years of work experience and over 30 years of age.

What is a judgment statement: The statement used for judgment is called a judgment statement.

1. format one

if (judging condition) {

If the code that conforms to the condition executes;

Code block 1 for execution;

Code block 2 for execution;

...................;

code block n executed;

}

2. Format Two

if (judging condition) {

Code block 1 for execution;

Code block 2 for execution;

...................;

code block n executed;

}else{

Code block 1 for execution;

Code block 2 for execution;

...................;

code block n executed;

}

Observe that the IF Else statement is somewhat similar to the ternary operator. In fact, the ternary operator is a shorthand format for if else.

Both of these formats are the same. If else struct shorthand format: variable = (conditional expression)? Expression 1: Expression 2;

Ternary operators:

Benefit: You can simplify if else code.

Cons: Because it's an operator, you have to have a result after the operation.

3 . Format three

if (judging condition 1) {

Code block 1 for execution;

}else if (judging condition 2) {

Execute the statement;

}else if (judging condition 3) {

Execute the statement;

}

if Statement features

    1. The second format differs from the ternary operator: The ternary operator must have a value to appear after the operation. The advantage is that it can be written in other expressions.
    2. The conditional expression, no matter what it is written, only looks at whether the final structure is true or false.
Select a Judgment statement (switch)

Switch statement

Format:

switch (expression)

{

Case takes value 1:

Execute the statement;

Break

Case takes value 2:

Execute the statement;

Break

......

Default

Execute the statement;

Break

}

Switch statement features:

There are only four types of 1,switch statement choices: Byte,short,int, Char.

There is no order between 2,case and default. Judge all the case first, no matching case execution

Default

The condition for the 3,switch statement to stop is that it encounters the break keyword or the curly brace that ends the switch statement.

4, if the matching case or default does not have a corresponding break, then the program will continue to execute

The line can execute the statement until the break is encountered or the end of the switch ends.

The value in the 5,switch case must have the same data type as the value of the switch expression. And the value of the case followed must be constant, not with the variable.

Case is like one of the answers to the choice question. Break is if the answer is correct then you can jump out of switch, meaning that you have found the right answer. Then the problem is done. If the case does not match then proceed to the next match until the match is reached. Finally, if there is no match, then switch gives a default answer, that is, defaults.

Note : The case is followed by a colon:

The execution statement in each case must be added break;

if and the Switch The statement is very much like, specific what scenario , which statement to apply? ?

If the exact value of the judgment is not many, but the symbol Byte,short int char four types.

Although 2 statements are available, a switch statement is recommended. Because it is slightly more efficient.

Other conditions :

Judging the interval, the use of if if is more widely used if the result is a Boolean type.

If in addition to judging the specific values can also judge the interval. The switch judgment interval can be very laborious. To write a lot of case, the result is a Boolean if you can tell that a switch is not possible. For example: the ABCD a90-100 b80-89 c70-79 d60-69 e0-59 are divided according to the student's test scores.

How to choose the actual development? ?

If you want to judge a specific value, and the value is not much, then use switch to complete. Switch case conditions are compile-time integer constants, the compiler can do table jump query, Find Fast.

However, the limitations of switch must be 4 types, and the value is not too large. It is generally used if. Finally, the switch is enhanced in JDK 7 and can be used to determine the string. 5.0 added a judgment on the enumeration.

remark: JDK7.0 can start using switch to use string-type data.

While loop

Requirement: Need to print a line of string "Hello Gzitcast", 100 times

You will need to print the statement 100 times system. out. println ("Hello Gzitcast");

So how do you solve the problem?

Java provides a structure called looping to control the recurrence of an operation.

int count = 0;

while (Count < 100) {

System. out. println ("Hello Gzitcast");

count++;

}

System. out. println ("over");

The variable count initialization value is 0, and the loop checks if count<100 is true if the loop body is executed (while the statement between {}), outputs the "Hello Gzitcast" statement, and then count self-increment, repeating loop, The loop stops until count is 100, that is, when count<100 is false. Executes the next statement after the loop.

Java provides three types of looping statements: The while loop, the Do-while loop, and the For loop.

1. while statement format :

while ( conditional expression )

{

execute the statement;

}

Do While statement

Do While statement format:

Do

{

execute the statement;

}while ( conditional expression );

The Do while feature is conditional whether or not it is satisfied,

The loop body is executed at least once.

While: The condition is judged first, and the loop body is executed only if the condition satisfies.

Do While: The loop body is executed first, then the condition is satisfied, and then the loop body is resumed.

A simple sentence: Do while: the loop body executes at least once, regardless of whether the condition is satisfied.

Note the semicolon at the back of a detail do while;

For loop

1. Format: for (initialization expression; cyclic conditional expression; post-loop action expression)

{

Execute the statement;

}

For execution flow

For know to loop, when reading to X=0, space is opened in memory, the definition variable x is assigned a value of 0. Then the conditional judgment x<5, is true, this time to meet the conditions after the execution of the loop body content system. out. println ("Hello java"), when the loop body executes, x < 5 is executed, and the following expression is + x + +. X becomes 1 after self-increment, x<5 (int x=0 executes only once), and then runs system again if true. out. println ("Hello java"); if False, the For loop ends.

Final summary

1, for the two expression in the order of the run, the initialization expression is read only once, to determine the loop condition, for the real execution of the loop body, and then execute the loop after the operation of the expression, and then continue to judge the cycle conditions, repeat to find a process, until the condition is not satisfied.

2. While and for are interchangeable, the difference is that the for-loop-defined variable is released in memory at the end of the For loop. While the variables used by the while loop can continue to be used after the loop ends.

3. The simplest infinite loop format: while (true), the for (;;), the reason that an infinite loop exists is not knowing how many times the loop is, but depending on some conditions, to control the loop. Recommended use while (true)

Break, continue keyword

Break keyword: The break statement is used to terminate the nearest enclosing loop or the switch statement it is in. Controls the statement, if any, that is passed to the terminating statement.

Applicable: For loop, switch two kinds of loop statements.

Use of break:

    1. Used alone.
    2. Used with labels. (tag: a name that satisfies the criteria for an identifier).

Use the details: After the break statement, write other statements, never execute, compile error.

Continue keyword: The statement passes control to the next iteration of the enclosing iteration statement where it is located. (Jump out of this loop and perform the Next loop).

Applies to: While, do, and for loop statements

Usage Details:

1. If continue appears at the end of the loop (the last statement), you can omit it.

2. If the continue appears in the first statement of the loop, then the subsequent statements cannot be executed, so compile an error.

3. Can be used in conjunction with the tag.

Java Process Control Statements

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.