JavasSE-04 Java Loop structure

Source: Internet
Author: User

Learning Essentials
    • While loop
    • Do-while Cycle
    • For loop
Loop what is the feature of the Loop loop

While loop syntax analysis

Case

The teacher every day to check the learning task is qualified, if not qualified, then continue.

The teacher assigned to Xiao Qiang every day of learning tasks: the morning reading materials, learning theory part, the afternoon machine programming, master the code part.

Case analysis
    • Analyze if there is a duplicate operation
    • Using the While loop implementation
      1. Determining loop conditions and looping operations
      2. Apply while syntax to write code
      3. Checks if the loop can exit
    • Note the point:
      1. Input string from console: Scanner input () method
      2. String equality comparison: "Str1". Equals ("str2")
      3. "= =" Can I compare strings? New String ("str") and "str"?
On-Machine Exercise: Complete the code implementation of this case

Program debugging
    • There are sometimes errors in the process of writing programs, but there is no good way to find and locate errors.

Solution One: Read the code and add the output statement

Solution Two: Program debugging

    • Program Debugging steps:

    • Demo Example: Using the while loop output variable i,i<4, expect to output 5 digits, Debug.
On-Machine Exercise: Complete the demo example and master the program debugging method.

Analysis on the structure of do-while cyclic grammar

Problem

After several days of study, the teacher gave Kaylee a test question,

Get him to write the program first.

Then the teacher checks whether it is qualified. If it is not qualified, continue writing. ......

Problem Analysis Demo Example: code implementation

Comparison of While loops and do-while loops

Different syntax

Different execution order

When the initial condition does not meet the cycle condition

    • The while loop does not execute at once
    • Do-while cycle is performed at least once in any case
On-Machine Exercise: Complete the Student Information management System main Menu loop input, input errors can continue to enter, until the correct menu to choose, exit the main menu. (except the Exit Function).

Why does A for loop use a for loop

For loop syntax structure

Note: For the use of expressions in the for loop, 1 parameters and 3 parameters support multiple expressions

Continue and break usage and differences
    • Continue: End the cycle and go to the next round.
    • Break: Terminate loop operation
Nested structures of loops
    • Typical three loops support nested loop structures
    • In actual development, loop nesting does not exceed triple

On-Machine Exercise: Use the For loop to output the sum of odd numbers within 1-100. On-Machine Exercise: Use the For loop to output the 99 multiplication table.

On-Machine Exercise: Use the For loop output isosceles triangle (high custom input for triangles).

On-Machine Exercise: Use the For loop to output a diamond.

JavasSE-04 Java Loop 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.