The three demon kings in C language (three loop statements)

C language loop statement

C language loop control statement is a programming statement based on C language. The statement mainly includes while loop statement, do-while loop statement and for loop statement to realize the loop structure.

for loop

The basic grammar of for loop structure is:
for (expression 1; expression 2; expression 3) {

Loop body;

}The
Insert picture description here
first expression is initialization and will only be executed once when the for starts the loop. The second expression is the test condition. The expression will be evaluated before the loop is executed. If the value is false (in this example, i> number), the loop ends. The third expression is updated and evaluated at the end of each loop. Use this expression to increase ( i ) the value (or decrease, i- - )
case:

#include <stdio.h>
#include <stdlib.h>
//使用for循环的计数循环
int main()
{
    const int number=5;
    int i;
    for(i=1;i<=number;i++)
    {
        printf("第%d次循环\n",i);
    }
return 0;
}

Output result:
Insert picture description here

Other formats of for loop:
expression 1, expression 2, and expression 3 can be omitted.
Note: If expression 2 is not written, it will enter an endless loop. Even if expression 1 and expression 3 are not written, remember to write a semicolon. Expression 1 and Expression 3 can be written in any sentence, separated by commas.

while loop

The basic syntax of a while loop is:
while(expression)
{ loop body; } When the value of the expression is true (or simply, non-zero), then the loop body is executed once. Then, after the expression is judged once, the judgment and execution of the loop are repeated until the expression is false (0). Each loop is called an iteration, and the true and false values ​​in C are discussed later in the article


Insert picture description here

Case:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int n=5;
    while (n < 7)
    {
        printf("n = %d\n", n);
        n++;
    }
return 0;
}

Output result
Insert picture description here

Things to pay attention to while loop:

1. Prevent endless loops. Avoid conditions that are always true or false. If it is always true, it will loop endlessly; if it is always false, it will not loop, and it is meaningless to write.

2. After the while loop, do not add a semicolon, that is, do not add a semicolon (;) after the while. If you want to add a semicolon, you can add after the braces, (ie};).

3. Don't forget to write a statement that ends the loop.

do while loop

The basic syntax of do while loop is:
do{

Loop body

}While (expression); The
Insert picture description here
do while loop judges the expression after the loop body is executed, so the loop body will be executed at least once. The do while loop is suitable for loops that need to be iterated at least once. For example, in the following case, the following is a password verification that includes a do while loop. The loop will only terminate when the correct password is entered. Otherwise, continue to execute the loop body.
Case:

#include <stdio.h>
#include <stdlib.h>

int main()
{
   int a=123456;
   int b;
   do
   {
       printf("请输入密码:");
       scanf("%d",&b);
   }while(b!=a);
    printf("密码正确\n");
return 0;
}

Output result:
Insert picture description here

How to choose a loop

How to choose which loop you want to use?
First, you need to determine whether you need an inlet condition loop or an outlet condition loop.
Entry condition loop: for loop, while loop, exit condition loop: do while loop.
Generally, the entry condition loop is used more. 1. The general principle is that it is better to judge when the loop is executed. 2. The judgment is placed at the beginning of the loop, and the program is highly readable. Of course, some require the use of exit condition loops, so each has its own advantages.

True and false value

Case:

#include <stdio.h>
#include <stdlib.h>

int main()
{
   int true_val,false_val;

   true_val=(5 > 3);
   false_val=(10 == 2);
   printf("true=%d,false=%d\n",true_val,false_val);
return 0;
}

Output result: In the
Insert picture description here
above case, the two relational expressions are assigned to two variables respectively, that is, the true expression is assigned to true_val, and the false is assigned to false_val. It can be seen from the result that in the C language, the value of an expression that is true is 1, and the value of an expression that is false is 0.
Of course, there are other numbers that can also indicate true or false. In C language, generally speaking, all non-zero values ​​are regarded as true, and only zero is regarded as false.
The concept of C language is really wide!
This blog is introduced here, of course, this is just a preliminary introduction. It is also the most basic, but don't underestimate them! Their three demon kings are not for nothing.

Guess you like

Origin blog.csdn.net/weixin_43776724/article/details/85279351