Java (5) Follow the bad structure

Source: Internet
Author: User

one, while Loop

1, the advantages of the cycle?

Reduce the coding of repetitive code, the program will be more concise

2. Grammar

while (expression) {

1. The expression is a "loop condition" and the result must be a Boolean type

2, the code in {}, that is, "loop operation (body)"

3. "Iteration section" changes the loop condition to end the loop

}

3, Characteristics: First judge, then execute

4. While loop has 2 common ways

Mode 1:

while (1, loop condition: Determines whether strings are equal) {

2. Cyclic operation

3. Receive the string in the keyboard again, change the loop condition, to end the loop

}

Example 1: in order to help Zhang San improve his performance as soon as possible, the teacher gave him a daily study task, the morning transcription code, afternoon learning programming, the teacher every day to check the results of learning, if failed, then continue.

1  Public classpractice1{2      Public Static voidMain (string[] args) {3Scanner input =NewScanner (system.in);4System.out.println ("Have you passed?" (y/n) ");5String ASD =Input.next ();6              while(!asd.equals ("Y")){7System.out.println ("The Morning transcription code");8System.out.println ("Learn to program in the afternoon");9System.out.println ("Have you passed?" (y/n) ");TenASD =Input.next (); One             } ASystem.out.println ("Completing the Learning task"); -     } -}
Example 1

Operation Result:

Note: The steps to resolve the problem using the while-through-bad structure are as follows:

1) analyze cycle conditions and cycle operations

2) write code with while syntax

3) Check whether the loop exits

Method 2: "P97 page Example 4 code"

while (1, loop condition: int variable combined with relational operator for comparison) {

2. Cyclic operation

3, iteration part, int variable + + or--, to change the loop condition, end the loop operation

}

Example 2: printing with a while loop 50 papers

1  Public classPractice2 {2      Public Static voidMain (string[] args) {3         intPapers= 1;4          while(papers<=50){5System.out.println ("Print section" +papers+ "paper");6papers++;7         }8     }9}
Example 2

Operation Result: Omit 40 strips

second, Do....while cycle

1, applicable occasions: first cycle 1 times, according to the conditions of judgment, recycling

2. Grammar

do{

2. Circulating body (cyclic operation)

3. Change the cycle condition

}while (conditions); 1. Cyclic conditions

Example 3: calculates the sum of the values from 1 to 50 that are multiples of 7 and outputs.

1  Public classpractice3{2      Public Static voidMain (string[] args) {3     intNumber = 1;4     intNumber1 = 0;5      Do{6         if(number%7==0){7Number1 + = number;//equivalent to Number1 = number1 + number8         }9number++;Ten} while(number<=50); OneSystem.out.println ("Value:" +number1); A     } -}
Example 3

Operation Result:

third, the program debugging

3.1 Steps:

Step 1: Set a breakpoint (you cannot set a breakpoint in an empty space)

Step 2: Start debugging

Step 3: Debug code (F6 Single Step) notebook Fn+f6 (F5)

Step 4: End Debugging

3.2 Mastering the benefits of commissioning?

Clearly see that the order in which the code is executed

Quickly navigate to where the exception code appears and help modify or refine the code

Java (5) Follow the bad structure

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.