Java Basics (6)

Source: Internet
Author: User

I. Review of 3 cycle structures

1. While

L Syntax

The initial value of the conditional expression;

while (conditional expression) {

Cyclic operation;

Change the statement of the conditional expression;

}

L Features: First judge, then execute, there may be no cycle

L Applicable occasions: the number of cycles is unknown

L Presentation Form

N First: Number of cycles determined

U conditional expression, which determines the variable of an integral type in a range

U while (i<=5) {//Do not need to receive user input}

N Second: Unlimited number of cycles, user input a value end loop

U conditional expression, judging by the value entered by the user

U while (user-entered value is compared to a value) {

such as input 0 or (yes/no) end loop

U}

2, Do...while

L Syntax

The initial value of the conditional expression;

bo=

Cyclic operation;

Change the statement of the conditional expression;

}while (conditional expression);

L Features: First execution, then judgment, at least 1 cycles

L for Applications, same while loop

L representation, same while loop

3. For

L Syntax

for (initial value; conditional expression; iteration part) {

Looping operations

}

L Characteristics: The same while loop, are first judged, then executed, there may be 1 cycles are not executed

L Application: Fixed cycle times

Ii. 2 common statements in a cycle

break--Terminate Loop (when loop is not complete, end loop prematurely)

continue--end this cycle and go to the next cycle

Three or two heavy cycle

1, in the loop, nested another loop, the inner layer of the loop, as a circular operation of the outer loop

2, the common double cycle

Form 1: outer while or do...while

Inner layer for loop

Form 2: Outer and inner layers are for loops

Form 3: Outer layer is for loop, inner is while or do...while

3. Order of execution of double cycle

1, first execute 1 outer loop;

2, after entering the outer loop, it is necessary to cycle all the inner layers,

To proceed with the outer 2....N cycle.

public static void Main (String [] args) {        Scanner input=new Scanner (system.in);        int scores[]=new int[4];        for (int d=0;d<3;d++) {        System.out.println ("Please enter" + (d+1) + "class scores");        int  num=0;        for (int i=0;i<scores.length;i++) {            System.out.print ("Please enter section" + (I+1) + "Participant's score:");            Scores[i]=input.nextint ();            Num=num+scores[i];        }        System.out.println (the average score for participants is: "+ (num/4.0));}    }


Print right triangle with double cycle

public static void Main (String [] args) {        Scanner input=new Scanner (system.in);        System.out.println ("Please enter the number of lines right triangle:");        int Hang =input.nextint ();//triangle line number        for  (int i=1;i<=hang;i++) {for            (int j=1;j<=2*i-1;j++) {                System.out.print ("*");            }            System.out.print ("\ n");}    }

continue and break in the double cycle "recent principles"

1, continue: End the current cycle from its "recent cycle structure", into the cycle structure of the next cycle

2. Break: Terminates the cycle from its "nearest loop structure"

Five or two heavy cycle common ways

1. For loops nesting each other

for () {

For () {

}

..........................

0}

2. For loop, nested while or do...while

for () {

While () {

}

or

do{

}while ();

}

Use a double loop to hit the corresponding number of rows

public static void Main (String [] args) {        Scanner input=new Scanner (system.in);        System.out.print ("Please enter the number of lines isosceles triangle:");        int Hang=input.nextint ();        for (int i =1;i

Java Basics (6)

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.