C # tutorial, C # loop

Source: Internet
Author: User

C # Loops

Sometimes, you may need to execute the same piece of code multiple times. In general, statements are executed sequentially: The first statement in a function executes first, followed by the second statement, and so on.

Programming languages provide a variety of control structures that allow more complex execution paths.

Looping statements allow us to execute a statement or group of statements multiple times, following the general form of a looping statement in most programming languages:

Loop type

C # provides the following types of loops. Click the link to see the details for each type.

Loop type

Describe

While loop repeats a statement or statement group when the given condition is true. It tests the condition before executing the loop body.

The For loop executes a sequence of statements several times, simplifying the code that manages the loop variable.

The Do...while loop is similar to the while statement except that it is a test condition at the end of the loop body.

Nested loops you can have in while, for, or do: Use one or more loops within a while loop.


Loop control Statements

The loop control statement changes the normal sequence of execution. When execution leaves a scope, all automatic objects created in that scope are destroyed.

C # provides the following control statements. Click the link to see the details of each statement.

Control statements

Describe

The break statement terminates the loop or switch statement, and the program flow continues to execute the next statement immediately following loop or switch.

The continue statement causes the loop to skip the remainder of the body and immediately restarts the test condition.


Infinite loops

If the condition is never false, the loop becomes an infinite loop. The For loop can be used in the traditional sense to implement an infinite loop. Because either of the three expressions that make up the loop is not required, you can leave some conditional expressions blank to form an infinite loop.

Using System;namespace loops{        class program    {        static void Main (string[] args)        {for            (;;)            {                Console.WriteLine ("Hey! I am Trapped ");}}}    

When a conditional expression does not exist, it is assumed to be true. You can also set an initial value and an increment expression, but in general, programmers tend to use the for (;;) structure to represent an infinite loop.

The above is the "C # tutorial" C # loop content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!


  • 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.