Advanced Loop structure

Source: Internet
Author: User

Nineth Chapter

Pre-word:

    • Triangle Circle Diamond Password row
    • Triangle Loop Diamond Password Line

1:2 Heavy Cycle structure:

Grammar:

 Public classXIA2 { Public Static voidMain (string[] args) {//While --- while         while(condition 1) { while(Condition 2) {}}//Do ---whlie         Do{                         Do{                            } while(condition 1); } while(condition 2); //For and for         for(Condition 1 ) {             for(Condition 2) {}}//While and while         while(Condition 1){                         for(Condition 2) {} }}}

Two: Using a double cycle:

ImportJava.util.Scanner; Public classXIA2 { Public Static voidMain (string[] args) {intRows=0; System.out.print ("Please enter the line number of the right angle triangle:"); Scanner input=NewScanner (system.in); Rows=Input.nextint ();  for(inti = 1; I <=rows; i++) {             for(intj = 1; J <=2*i-1; J + +) {System.out.print ("*"); } System.out.println ("\ n"); }    }}

Three: Jump statement advanced

    • Continue statement:
 for (.....) {for(...) {......   Continue ;    .......   } .......}

Break statement:

 for (.....) {for(...) {   ......      Break ;      ......      }      ......}    
ImportJava.util.Scanner; Public classxia8 { Public Static voidMain (string[] args) {intcount=0;//counter, record bought a few clothes altogetherString Choice; Scanner input=NewScanner (system.in);  for(inti = 0; I < 5; i++) {System.out.println ("Welcome to the first" + (i+1) + "Home Shop");  for(intj = 0; J <3; J + +) {System.out.println ("To leave (y/n)"); Choice=Input.nextline (); if("Y". Equals (choice)) {                     Break; } System.out.println ("Bought a piece of clothing."); Count++; } System.out.println ("Close to the hotel"); } System.out.println ("Bought a total of" +count+ "clothes"); Choice=Input.nextline (); }}

Summarize:

A double cycle is a cycle in which the body of a loop contains another complete loop structure.

The execution of the Break...continue statement control program can be used in a double loop

                                                                                                                                                                                                 2016-07-09

                                                                                                                                                                                                    16:37:14

                                                                                                                                                                                                  Cheng.java


Advanced 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.