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)!