Continue, return, break

Source: Internet
Author: User

1. Single-layer Loop

Using system;

namespace consoleapplication7
{< br> ///


// class1 abstract description.
///
class class1
{< br> ///
// application Program .
//
[stathread]
static void main (string [] ARGs)
{< br> //
// todo: add Code here to start the application
//
for (INT I = 0; I <10; I ++)
{< br> if (I = 7) continue;
// After continue, skip other statements in this loop, continue the next cycle
// if (I = 7) break;
// The statement after the break jumps out of the entire loop and continues to execute the loop
// if (I = 7) return;
// ruturn jumps out of the entire program and returns directly
console. writeline (I);
}< br> console. writeline ("this is the end. ");
}< BR >}

If (I = 7) continue; running result:
0
1
2
3
4
5
6
8
9
This is the end.
 

If (I = 7) break; running result:
0
1
2
3
4
5
6
This is the end.

If (I = 7) return; running result:
0
1
2
3
4
5
6

2. Nested loop

Using system;

Namespace consoleapplication7
{
/// <Summary>
/// Summary of class1.
/// </Summary>
Class class1
{
/// <Summary>
/// Main entry point of the application.
/// </Summary>
[Stathread]
Static void main (string [] ARGs)
{
//
// Todo: Add code here to start the application
//
For (INT I = 0; I <10; I ++)
{
For (Int J = 0; j <10; j ++)
{
// If (I = 7) continue;
// If (I = 7) break;
If (I = 7) return;
// If (j = 7) continue;
// If (j = 7) break;
// If (j = 7) return;
Console. Write (I );
Console. Write (j );
}
Console. writeline ();
}
Console. writeline ("this is the end .");
}
}
}

If (I = 7) continue;
If (I = 7) break;
The execution results of the preceding two statements are the same, and the line starting with 7 is empty. It indicates that the break statement can only jump out of one loop.

If (I = 7) return;
Output 7 rows starting with 0 to 6. After the program is executed, all subsequent statements are skipped.

If (j = 7) continue;
A column whose J is 7 is missing


If (j = 7) break;
The output of each column is only 6 to J. It indicates that the break statement can only jump out of one loop.

If (j = 7) return;
Some programs starting with 0 and ending with "06" are output, and all subsequent statements are skipped.

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.