1. Circular structure sequence

1. Circular structure sequence

Loop structure is a very important structure in programs. Its characteristic is to repeatedly execute a certain program segment until the given condition is met, until the condition is not met. The given conditions are called loop conditions, and the repeatedly executed program segments are called loop bodies. C language provides various types of loop statements that can form different forms of loop structures.。

1.1. while statement

The general form of a while statement is: while (expression) statement; The expression is the loop condition, and the statement is the loop body.

The semantics of the while statement are: to evaluate the value of an expression, and when the value is true (non-zero), execute the loop body statement.

When using a while statement, the following points should be noted:

  1. The expression in a while statement is usually a relational or logical expression, and as long as the value of the expression is true (non-zero), the loop can continue.

  2. If the loop body includes more than one statement, it must be enclosed in {} to form a composite statement.

  3. Attention should be paid to the selection of loop conditions to avoid dead loops.

1.2. do-while statement

The general form of a do while statement is

do statement;

while(Expression);

The statement is the loop body, and the expression is the loop condition.

The semantics of the do while statement are:

Execute the loop sentence once first, then determine the value of the expression. If it is true (non-zero), continue the loop; otherwise, terminate the loop.

The difference between do \ - while statements and while statements is that do \ - while executes first and then judges, so do \ - while must execute the loop body at least once. And while is judged first and executed later. If the condition is not met, the loop statement will not be executed.

The while statement and the do while statement can generally be mutually rewritten.

In this example, the loop condition is changed to -- n, otherwise the loop will be executed again. This is caused by executing first and then judging.

For do while statements, the following points should also be noted:

  1. In if and while statements, semicolons cannot be added after expressions, while in do while statements, semicolons must be added after expressions.

  2. Do while statements can also form multiple loops and can be nested with while statements.

  3. When the loop body between do and while consists of multiple statements, it must also be enclosed in {} to form a composite statement.

  4. When replacing do while and while statements, be careful to modify the loop control conditions.

1.3. For sentence

The for statement is a more powerful and widely used cyclic statement provided by the C language. Its general form is:

For (initial expression; Conditional expression; Iterative expression statement;

Expression Meaning of expression
Initial expression Usually used to assign initial values to loop variables, usually an assignment expression. It is also allowed to assign initial values to loop variables outside of the for statement, in which case the expression can be omitted.
Conditional expression is usually a cyclic condition, typically a relational expression or logical expression.
Iterative expression can usually be used to modify the value of a loop variable, usually in the form of an assignment statement.

These three expressions can all be comma expressions, meaning that each expression can be composed of multiple expressions. All three expressions are optional and can be omitted.

The "statement" in general form is a loop body statement. The semantics of the 'for' statement are::

  1. calculate the value of expression 1.

  2. Calculate the value of expression 2 again. If the value is true (non-zero), execute the loop body once. Otherwise, exit the loop.

  3. Then calculate the value of expression 3 and repeat step 2. During the entire for loop process, expression 1 is only evaluated once, while expressions 2 and 3 may be evaluated multiple times. The loop body may execute multiple times or not execute at all.。

When using the for statement, the following points should be noted::

All expressions in the 'for' statement can be omitted, but the semicolon delimiter cannot be omitted. For example:

for(; expression; The expression omitted the initial expression.

For (expression;; expression) eliminates conditional expressions.

For (expression; expression;) eliminates the iterative representation.

For (;;) omits all expressions.

  1. When the loop variable has been initialized, expression 1 can be omitted, as in example 3.27. If expression 2 or expression 3 is omitted, it will result in an infinite loop. In this case, efforts should be made to end the loop within the loop body.

  2. The loop body can be an empty statement.

#include <stdio.h>
void main()
{
    int n = 0;
    printf("input a string:\n");
    for (; getchar() != '\n'; n++);
    printf("%d", n);
}    

In this example, expression 1 of the for statement is omitted, and expression 3 is not used to modify the loop variable, but rather to count the input characters. In this way, the count that should have been completed in the loop body is completed in the expression. Therefore, the loop body is an empty statement. It should be noted that a semicolon is indispensable after an empty statement. If this semicolon is missing, the following printf statement will be executed as a loop body. On the other hand, if the loop body is not an empty statement, you must not add a semicolon after the parentheses of the expression, as this will consider the loop body to be an empty statement and cannot be executed repeatedly. These are common errors in programming that require great attention.