Third day of Java learning

Source: Internet
Author: User

Cycle

Loops: Regular repetition, loops are composed of multiple if

1. Four structures

1), initial conditions

2), judging conditions

3), Circulating body

4), Iteration factor

2, while: when to type

1), Characteristics: When the first to determine the type of execution

2), Structure

An initial expression

while (conditional expression) {

Loop body

Iterative factors

The value of the multiplication

Doubleproduct = 1; 1.0

inti = 1;

while (i<=100) {

Product *=i;

i++;

}

System. out. println ("5!=" +product);

3. Do. While: until type

1), Characteristics: First execution after judgment, at least to perform

2), Structure

An initial expression

do{

Loop body

Iterative factors

}while (conditional expression);

1. A random integer

2, the judgment is equal to 88

3, if not equal to continue random integer---Continuous repeating loop

intrnd =0;//Promotion Scope

inti = 0; Counter

Do {

intrnd -->do use

Rnd = (int) (Math. Random() *101);

System. out. println ("Random number:" +rnd);

i++;

}while (rnd!=88);

System. out. println ("After" +i+ "appears 88");

4. For:

1), Features: Concise, can determine the number of cycles

2), Structure

For (an initial expression; a conditional expression; an iteration factor) {

Loop body

}

intsum = 0;

for (inti=1;i<=5;i++) {

Sum +=i;

}

System. out. println ("and for:" +sum);

5. Key words

1), break: Terminate, jump out of this layer cycle

2), continue: Pause this time, continue to execute next time

Code can no longer appear after a dependent scope

//temperature 0-->100 insulation

       int temp =0;

      while (temp<100) {

          temp +=10;

         if (temp==60) {//power outage

            break ;

        }

         System. out . println ("ignition warming" +temp);        

      

Temperature 0-->100 Insulation

inttemp = 0;

while (temp<100) {

Temp +=10;

if (temp==60) {//Blackout for a while ...

continue;

}

System. out. println ("ignition warming" +temp);

}

System. out. println ("insulation state");

6. Double (nested) loops

There is a loop inside the loop, the difficulty is the data change, there is correlation

for (intJ =1;j<=9;j++) {

The same code came up with J-Times-->for

for (inti=1;i<=j;i++) {

System. out. Print (i+ "*" +j+ "=" +i*j+ "\ t");

}

System. out. Print ("\ n");

}

}

Third day of Java learning

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.