Break usage and break usage

Source: Internet
Author: User

Break usage and break usage
Private static void test (){
For (int I = 0; I <5; I ++ ){
For (int j = 0; j <5; j ++ ){
System. out. println ("just jump out of the innermost for loop" + I + "/" + j );
If (j = 2)
{
Break; // only jumps out of the innermost for Loop
}
}

}
System. out. println ("end ");

}

// Result




Private static void test2 (){
 

A: for (int I = 0; I <10; I ++ ){
For (int j = 0; j <10; j ++ ){
System. out. println ("Jump to the specified code block" + I + "/" + j );
If (j = 2)
{
Break a; // jump to the specified code block
}
}
 
}
System. out. println ("end ");

 
}

Result ://





Import java. util. LinkedHashMap;
Import java. util. Map;
Import java. util. Map. Entry;


Public class test {
Private static LinkedHashMap <Integer, Integer> linkedHashMap;
Private static LinkedHashMap <Integer, Integer> nodeleteKey;
Private static int sum;


Public static void main (String [] args ){
LinkedHashMap = new LinkedHashMap <Integer, Integer> ();
NodeleteKey = new LinkedHashMap <Integer, Integer> ();
NodeleteKey. put (5, 5); // if the key is 5, it cannot be deleted.
For (int I = 0; I <25; I ++ ){
LinkedHashMap. put (I, I * 10 );
Sum + = I;
TrimToSize (I );
}
System. out. println (linkedHashMap. toString ());

}





/**
* @ Param I: filter set. Delete the earliest inserted key. Do not delete the key that is not deleted.
*/
Private static void trimToSize (int I ){
While (true ){
Synchronized ("trimToSize "){
// Meet the conditions and exit
If (sum <100 ){
Break;
}
// Obtain the first element of the linked list, that is, the element first put in.
Map. Entry <Integer, Integer> toEvict = null;
A: for (Map. Entry <Integer, Integer> entry: linkedHashMap
. EntrySet ()){
ToEvict = entry;
If (toEvict! = Null ){
For (Entry <Integer, Integer> iterable_element: nodeleteKey
. EntrySet ()){
Integer value002 = iterable_element.getValue ();
If (! ToEvict. getKey (). equals (value002 )){
// As long as it is not a key that cannot be deleted, it jumps out to a and runs the code after code block {}.
Break;
}
}
}
}


// It is not an element that cannot be deleted
If (toEvict! = Null ){
Integer key = toEvict. getKey ();
Integer value = toEvict. getValue ();
LinkedHashMap. remove (key );
Sum-= I;
System. out. println ("remove =" + key );
}
}


}
}
}

Result ://






Break statement usage

The break jumps out of the entire loop, and the continue stops the loop and continues the next loop. I wrote one to illustrate the problem:
Public static void main (String args []) {

Int inter1, inter2;

List list1 = new ArrayList ();
List list2 = new ArrayList ();
List1.add ('A ');
List1.add ('B ');
List1.add (null );
List1.add ('C ');
List2.add ('A ');
List2.add (null );
List2.add ('B ');
List2.add ('C ');
Inter1 = list1.size ();
Inter2 = list2.size ();

For (int I = 1; I <5; I ++ ){
System. out. print (I );
For (int j = 0; j <inter1; j ++ ){
If (list1.get (j) = null)
Break;
// Continue;
Else
System. out. print (list1.get (j ));
}
For (int k = 0; k <inter2; k ++ ){
If (list2.get (k) = null)
Break;
Else
System. out. print (list2.get (k ));
}
System. out. print ('\ n ');
}
}

The result is:
1abA
2abA
3abA
4abA
If the first loop is changed to continue, the result is:
1 abcA
2 abcA
3 abcA
4 abcA

Use break in C Language

Break statement
Break statements are usually used in loop statements and switch statements. When a break is used to switch a switch statement, the program can jump out of the switch and execute the statement after the switch. If there is no break statement, it will become an endless loop and cannot exit. The usage of break in the switch has been met in the previous example when we introduced the switch statement. Here we will not give an example.
When the break statement is used in do-while, for, and while loop statements, the program can terminate the loop and execute the statements following the loop. Generally, the break statement is always associated with the if statement. That is, the loop jumps out when the conditions are met.
Note:
1) The break statement does not work for the if-else Condition Statement.
2) in a multi-tier loop, a break statement only jumps one layer outward.
Your problem is to jump out of the for Loop and execute the statement after the loop, that is, str [I ++] = '.'; Statement

Related Article

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.