For loops in Java

Source: Internet
Author: User

The FOR Loop statement is the most efficient and flexible loop structure in the Java language, typically used in cases where the number of cycles is known.

First, for loop syntax

1  for (initialization; condition; update) {2            statements; 3     }

Syntax Explanation: When the For statement executes, the initialization operation (initialization) is performed first, then the termination condition expression (condition) is satisfied and the loop exits if the termination condition is met. Otherwise executes the statement in the loop body, then executes the iteration part (update) to complete a loop. The next cycle starts with the judgment termination condition and the corresponding operation is performed according to the result.

! Note: The initialization operation (initialization) is performed only on the first loop.

Code Listing:

1. Print as shape (inverted triangle)

1  Public classfordemo1{2PiblicStatic voidMain (string[] args) {3              for(inti=0;i<5;i++){4                  for(intj=i;j<5;j++){5System.out.print ("*");6                 }7 System.out.println ();8                 }9             }Ten}

2. Print as shape (positive triangle)

1  Public classfordemo2{2      Public Static voidMain (string[] args) {3      for(inti=0;i<5;i++){4          for(intj=0;j<=i;j++){5System.out.print ("*");6             }7 System.out.println ();8             }9         }Ten}

(1) through the loop nested printing similar to this diagram, the outer loop control the number of rows, the inner loop control the number of columns, that is, the number of each row.
(2) for the graph of decreasing (inverted triangles) by rows, the initialization of the inner loop changes with the outer loop,
such as int j=i;
(3) for a graph that increases by row (the positive triangle), let the inner cycle condition change with the outer cycle, such as j<=x;

3. Print 99 multiplication table

1  Public classmulttable{2      Public Static voidMain (string[] args) {3         //using a For loop to generate a 99 multiplication table4 5System.out.println ("99 Multiplication Table");6          for(inti=1;i<10;i++){          7                  for(intj=1;j<=i;j++){              8System.out.print (j+ "*" +i+ "=" + (j*i) + "\ T"); 9             }Ten System.out.println ();  One         } A     } -}

See that it prints out a shape that doesn't look like a positive triangle, and adds a formula to each line, just like the graphic above (three or 2 questions). Let the inner cycle condition change with the outer loop, and thus circulate the 99 multiplication table.

For loops in Java

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.