Java Foundation Five

Source: Internet
Author: User

1. While loop

2. Do ... While loop

3. For loop

One, while

/*
While loop

Statement format:


while (Boolean expression) {
statement block;
}

Execution order:
The value of the Boolean expression is judged first, if true. The statement block is executed.
The value of the Boolean expression is then judged, if true. The statement block is executed.
The value of the Boolean expression is then judged, if true. The statement block is executed.
......
It ends when the Boolean expression is false.

*/

Exercises Series:

Print five times Hello World

Print output 1~10

To find the factorial of 1~10

For an even number of 100 or less

Second, do ... While

/*
Format of Do-while:

bo=
statement block;
}while (boolean-expression);

Execution order:
Executes a statement block, and then determines the Boolean expression, if True.
Continue execution of the statement block, and then determine the Boolean expression, if True.
Continue execution of the statement block, and then determine the Boolean expression, if True.
......
Continue execution of the statement block, and then determine the Boolean expression, if False. Stop it.
*/

Exercises:

Print three times with Do-while HelloWorld

Use Do...while to print all the odd numbers within 100

Numbers that can be divisible by 3, but not divisible by 5, within 100 of the print

Summarize:

The difference between while and Do-while:
While the condition is first executed, then the loop body is executed
The Do-while first executes the loop body and then determines the condition.

When the loop condition is not satisfied for the first time, the while is not executed at one time, and Do-while is executed once.


Third, for Loop

/*
For loop
Format:
for (loop variable initial value setting; loop condition Boolean value; statement executed after each loop) {
Circulation body;
}

Execution process:
The initial value setting is calculated first.
The Boolean value is then computed, and if true, the loop body is executed once, and then the statement executed after each loop is executed.
The Boolean value is then computed, and if true, the loop body is executed once, and then the statement executed after each loop is executed.
The Boolean value is then computed, and if true, the loop body is executed once, and then the statement executed after each loop is executed.
......
The Boolean value is then computed, and if false, it stops.
*/

Exercises:

Print three Hello world:

Look at the idea of the problem

Print out the qualifying content first, then add the count count, and wrap the line at 6: The following complex method

Then improve efficiency:

A * * * problem:

Answer:

First of all remember i++ must be positive?

The >>> is used here. : Unsigned Right Shift: No matter the highest bit is 0 or 1, the left side is 0. Inside thinking

i++ Count to the last

01111111 11111111 11111111 11111111 Positive, complement is the same as the source code

If you add one more:

11111111 11111111 11111111 11111111 is negative

Seriously, this problem is really boring!!! Let me verify that:

First, the above positive number plus one can be converted to negative numbers can be:2147483647+1=-2147483648

The following:2147483647+k+1=-(2147483648-k)

So the topic is i++, calculate is 3*i, also is the negative number of the maximum three times times (2147483648-k).

Then add one to one;

Until: as follows:

That is: three times times the -1431655763,int cast is equal to 7 of the!!!!!!!!

Java Foundation Five

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.