Getting Started with Java (7)--Looping and debug debugging

Source: Internet
Author: User

Loop:

While Loop:

Format:

int i = 0; ①//initialization conditions

while (I < 10) {②//Judging condition

System.out.println (i); ④//Loop Body

i++;③//Control conditions

}

Execution order:

First time: ①,②,④,③

Second time: ②,④,③

Third time: ②,④,③

......

Last: ②

If the condition satisfies, the loop body is executed, and the loop is not satisfied.

The judging condition is always executed more once than the control condition.

Do .. While Loop

Format:

int i = 0; ①//initialization conditions

do{②//Judging conditions

System.out.println (i); ④//Loop Body

i++;③//Control conditions

}while (I < 10)

For Loop:

Format:

①②③

for (int i=0; i<10; i++) {//The function of this loop is: To print a number between 0-9

System.out.println (i); This is the loop body.

}

Execution order:

First time: ①,②,④,③

Second time: ②,④,③

Third time: ②,④,③

......

Last: ②//conditions meet on the execution of the loop body, not satisfied on the end of the loop

Enhanced for loop: (underlying dependent iterator)

Format:

For (data type variable name: The collection or array to traverse) {

normal function Code}

loop control statement:

Break: Used to terminate a loop, the loop is no longer executed.

Continue: Used to end the loop and proceed to the next loop.

Common problems:

  What is the difference between the three cycles:?

The difference between the Do.while loop and the other two loops is:

First execute once, then in judgment. While the other two loops are judged first, after deciding whether to execute

The difference between a for loop and two other loops:

After the For loop is executed, the initialization condition is not available. The other two loops can also continue to be used.

  when do you decide which loop to use? (This refers to the while loop and for loop)

If the number of loops is fixed, use a for loop.

If the number of loops is not fixed, a while loop is used.

Automatic unboxing:

JDK1.5 new features: Automatic unboxing

Auto-unpacking: Wrapper--Basic type

Integer i = 100;

int a = i;

Auto-Boxing: Basic Type--packing class

Integer i2 = A;

Regular Expressions:

Overview: The correct expression (string) that conforms to a rule.

Function: A: Checks the string. B: Cut the string.

Debug Debugging:

1. Add a breakpoint. There's no point.

2. Go to the Debug interface.

3. How to look at the debug interface.

Top left: Where did the program go (the first line of the specific class)

Center left: Source code bottom Left: Console//Important

Top right: The process of changing variables. Very important

4. How to perform the next step.

F6: Progressive Debug F5: Step-by-step debugging

5. After the debugging is finished, remove the breakpoint.

Upper right--points---double X (break points)

Finally, we will give you some exercises:

Practice One:

Print all the even numbers between 1-100 with a for loop.

Use the For loop to print all the odd numbers between 1-100.

Exercise two:

Calculates all the even numbers between 1-100 and prints the results to the console.

Calculates all the odd numbers between 1-100 and prints the results to the console.

Exercise Three:

Creates a ArrayList collection object, adds elements inside it, and then iterates through it with the enhanced for.

Exercise Four:

Creates a HashMap collection object, adds elements inside it, and then iterates through it with the enhanced for.

Exercise Five:

Through the loop, print the following graphic on the console:

Requirement: Output statements can only print one at a time *

Hint: System.out.println (); Lines are wrapped when you finish printing data.

System.out.print (); No wrapping is done after the data has been printed.

A: Rectangle

*****

*****

*****

*****

*****

B: Positive Triangle

*

**

***

****

*****

Exercise Six: Calculates the difference between the maximum and minimum values of an integer array.

Getting Started with Java (7)--Looping and debug debugging

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.