This is the seventh day of my participation in the August More text Challenge. For details, see: August More Text Challenge

cycle

There are three statements in C# that implement loops: while, do, and for.

Cycle structure has three elements: cycle initiation, cycle conditions and cycle control.

To write a loop, you need to accurately design three elements. Loop initialization occurs before the loop, making the loop “ready”; The cycle condition is the decision of whether the cycle can continue or stop, and the cycle control is the key process to realize the cycle condition inside the cycle. Circulars can be used directly or indirectly for calculation purposes, or they can be independent of the triplets.

It is theoretically feasible to use any of the three loop statements to solve the loop problem. The key point is to correctly analyze the three elements of the loop: loop initiation, loop control, loop condition and loop body.


While statement

The while loop is a simple loop:

The body of the while(expression) loop executes the statementCopy the code

Example: Find the sum of 1-10

class Program
{
    static void Main(string[] args)
    {
        int a = 0;
        int b = 0;
        while(a <= 10)
        {
            b += a;
            a++;
        }
        Console.WriteLine("Sum required:" + b);
        // Output: sum: 55Console.ReadLine(); }}Copy the code

Do statement

The do statement first executes the statement in the body of the loop, then evaluates the given expression value, and determines whether to execute the loop based on the result.

The body of the do loop executes the statement while(expression);Copy the code

Example: Find the sum of 1-10

class Program
{
    static void Main(string[] args)
    {
        int a = 0;
        int b = 0;
        do
        {
            b += a;
            a++;
        } while (a <= 10);

        // Output: sum: 55
        Console.WriteLine("Sum required:"+ b); Console.ReadLine(); }}Copy the code

For statement

The for statement is another statement that implements the looping function:

For (initialize the expression; Test expression; The body of the iteration expression executes the statementCopy the code

The initialization expression in a for statement is executed only once, and before any other part of the for statement. It is often used to declare and initialize local variables used in loops;

The test expression may be evaluated multiple times to determine whether the statement in the body of the loop is executed or skipped. It must be of type bool;

The iterated expression is executed after the statement in the body of the loop and returns to the top of the loop. The iterated expression is evaluated against the test expression immediately after execution;

  • Example: Find the sum of 1-10
class Program
{
    static void Main(string[] args)
    {
        int b = 0;
        for (int a = 0; a <= 10; a++)
        {
            b += a;
        }
        // Output: sum: 55
        Console.WriteLine("Sum required:"+ b); Console.ReadLine(); }}Copy the code

Two common cycles

  • There are two types of cyclic structures in practical application problems: counting type and conditional type.

1. Counting cycle: Counting cycle is used to process a given number of cycles. In counting cycles, loop control is performed by control variables. The control variable changes regularly (increasing or decreasing) every time the cycle is carried out. When the control variable reaches a predetermined number of cycles, the cycle ends. Counting loops often use the for statement.

2. Conditional cycles: Conditional cycles are used to process cycles with an unknown number of cycles and are called “indeterminate cycles”. In conditional cycles, since the number of cycles cannot be accurately known in advance, the control of cycles is determined by conditions. This condition is tested on each loop, and once the condition is satisfied, the loop ends. Conditional loops often use while and DO statements.