An example of Interrupting Single-layer and double-layer Loops

Source: Internet
Author: User

It involves creating a string array, traversing a foreach statement, and determining whether to interrupt a loop.

1. Interrupt a single-layer Loop

Package com. lixiyu; public class BreakCyc {public static void main (String [] args) {String arr [] = {"tiger", "White Crane", "Pigeon", "e ", "Crow"}; System. out. println ("tell me what birds are there before you find the first e. "); For (String s: arr) {// traverses the array if (s. equals ("Crow") {break;} System. out. println ("with" + s );}}}

If (s. equals ("Crow") {break;} indicates that when the element is traversed to the crow, the loop ends and the output is.


2. Interrupt a double loop

Implementation:

------------- Example of interrupting the double-layer loop ----------- baby's score in this exam: mathematics, Chinese, English, and art history 67 78 63 22. What is the score of 22? Why does it fail?

Code details:

Package com. lixiyu; public class BreakCy {public static void main (String [] args) {System. out. println ("\ n --------------- example of interrupting a double loop -------------"); // create a score array int [] [] myScores = new int [] [] {67, 78, 63, 22, 66}, {55, 68, 78, 95, 44}, {95, 97, 92, 93, 81}; System. out. println ("Baby's Exam Score: \ n mathematics \ t language \ t English \ t art \ t history"); No1: for (int [] is: myScores) {// traverse the score table for (int I: is) {System. out. print (I + "\ t" ); // Output result if (I <60) {// if the System fails, all output systems are immediately interrupted. out. println ("\ n, etc.," + I +? Why does it fail? "); Break No1 ;}} System. out. println ();}}}


The idea is to create a multi-dimensional array first, and then use the foreach statement to traverse cyclically, judge the elements smaller than 60, and interrupt the entire double-layer loop.

This article is from the "ToBeContinued" blog, please be sure to keep this source http://leexy.blog.51cto.com/4136883/1302906

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.