How does one jump out of the current multi-cycle?

Source: Internet
Author: User

I recently saw a pen question:
How does one jump out of the current multi-cycle?


But I see a lot of answers: Break and return. I think this answer seems inappropriate.

So let's make a careful deduction.

First, it is not recommended to use return to jump out of the loop. The question also says that it is a multi-cycle,


If return is used, the entire function will jump out.CodeCannot be executed

So here, I sorted out my views,


Some of the loops we have learned are for, while, do while, and switch.


The following describes


Public static void main (string [] ARGs ){

// Use or mark a break


OK: For (INT I = 0; I <10; I ++ ){


System. Out. println ("XXX ");


For (Int J = 0; j <10; j ++ ){


If (j> 2)


Break; // = return;


System. Out. println (j );


}


}


It should be noted that OK is used to mark which loop to jump out.


When the OK Mark is in the first (outermost layer) loop, it is equivalent to the return function and jumps out of the entire loop.


"XXX" is output only once


When the OK Mark is on the second layer, it is equivalent to the break function and only jumps out of the current loop.


However, "XXX" completes the loop at the first layer without executing the second layer.


For (INT I = 0; I <10; I ++ ){


System. Out. println ("XXX ");


OK: For (Int J = 0; j <10; j ++ ){


If (j> 2)


Break OK; // = break;


System. Out. println (j );


}


}

// Use a custom Boolean variable and meet the conditions together to execute a loop

// In addition, when the bounce condition is set, not only the flag value is set, but also the break must be added.


It must be noted that ,! Flag is used to mark which loop jumps out.


When! When the flag Flag is in the first (outermost layer) loop, if the exit condition is true, the loop of the first layer is closed.


Function equivalent to return;


Boolean flag = false;


For (INT I = 0; I <5 &&! Flag; I ++ ){


System. Out. println ("XXX ");


For (Int J = 0; j <5; j ++ ){


If (j> 2 ){


Flag = true; break;


// The functions of the above two sentences are equivalent to return;


}


System. Out. println (j );


}


}

When! When flag is on the second layer,


Program"XXX" is executed first, and then the second loop is executed. When the bounce condition is set, the current loop is closed.


In the next loop, only the first loop is executed. Remember, this! Flag is to close the loop rather than jump out


Boolean flag = false;


For (INT I = 0; I <5; I ++ ){


System. Out. println ("XXX ");


For (Int J = 0; j <5 &&! Flag; j ++ ){


If (j> 2 ){


Flag = true;


Break;


}


System. Out. println (j );


}


}

The last type is the so-called break, starting from a loop to the second layer.


When the bounce condition is true, the current loop is displayed,


Then, the cycle starts from the first layer. At the second layer, when the bounce condition is established, the cycle starts from the second layer.


Recurrence


For (INT I = 0; I <5; I ++ ){


System. Out. println ("XXX ");


For (Int J = 0; j <5; j ++ ){


If (j> 2 ){


Break;


}


System. Out. println (j );


}


}

In addition, both while and switch can be marked, but it seems that the switch does not currently use boolean values as conditions.

Int I = 0;
OK: While (true ){
I ++;
System. Out. println (I );
If (I = 5)
Break OK;
}

Int I = 0;


Boolean flag = false;


While (true &&! Flag ){


I ++;


System. Out. println (I );


If (I = 5)


Flag = true;


}

Int I = 0;
OK: do {
I ++;
System. Out. println (I );
If (I = 5)
Break OK;
} While (true );

Int I = 0;


Boolean flag = false;


Do {


I ++;


System. Out. println (I );


If (I = 5)


Flag = true;


} While (true &&! Flag );

It seems that the switch can only be marked.


Int x = 3;


OK: Switch (x ){


Case 1:


X * = 10;


Break;


Case 2:


X * = 10;


Break;


Case 3:


X * = 10;


Break OK;


Case 4:


X * = 10;


Break;


Default:


X = 0;


}


System. Out. println (X );

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.