19 C # loop statement skipping and interrupting continue and break,

Source: Internet
Author: User

19 C # loop statement skipping and interrupting continue and break,

 

In the C # loop statement, sometimes we want to skip a loop. Sometimes we want to terminate the entire loop directly when a condition is met. C # provides the continue and break statements for us.

The usage of continue is the same as that of break. Write this word directly and add a semicolon to it.

For example:

Continue;

Break;

These two statements can be used in both the while loop and for loop.

When a continue statement is encountered in a loop, the statements following this loop are no longer executed. Start to execute the next loop.

When a break statement is encountered in a loop, the entire loop is terminated directly. That is to say, not only does the current loop not run, but all the subsequent loops do not.

The continue and break statements are usually used in combination with the if statement.

 

You create a project. The solution name is Sloution19, the project name is Exercise19, and the project type is console program. Click Program. cs. Add the code to it.

Here is a brief explanation of the above Code.

1. When there are no break and continue statements, both the while loop and the for loop want to output numbers 1 to 10 in the dos window.

2. When I = 3 or j = 3, The if condition is true and the contiue statement is executed. The statement after continue is no longer executed. Start a new cycle. Note that all the statements before continue are executed.

3. When I = 7 or j = 7, the if condition is true and the break statement is executed. The statement after the break is no longer executed. The entire loop statement is terminated.

 

Running result

 

Exercise

1. Read this code and add comments to important statements.

2. Move the 17th lines of code to the 26th lines of code before braces. What is the result of running the program? What is the reason?

3. Try to write a piece of code and print an even number from 1 to 10 in a loop. Consider using continue.

4. Calculate 1 + 2 + 3 + .... If the sum is greater than 68, how many do we need to accumulate? Use break.

 

Pay-as-you-go answers to questions C # encountered during the course of getting started.

Zero-basic learning C # beginners, most problems can be solved through self-study or the Internet.

HoweverThere are still some problems that are not so easy to solve and become a stumbling block to your learning. In the end, it may even cause you to lose your learning experience. Generally, when you get started with C # programming, you will encounter more than 20 to 30 problems that you cannot easily solve.

What do you need most at this time?

Yes! A professional teacher will help you!

If you have any questions during this course, you can ask the author for help via qq or email.

Only 30 RMBYou can get three professional answers from the author of this tutorial. So you only need less than 300 yuan to successfully complete the C # entry, and then open the door to the software industry.

 

The payment method is very simple:

Scan the following QR code. Leave your QQ number or email address in the payment message.

 

Education is not cheap, but reliable! Truly smart people know how to spend money to make professional people serve themselves and improve their efficiency.

 

Instructor qq: 2040227296

Instructor email: 2040227296@qq.com

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.