C language basics: while and do while

        In the first chapter, we have briefly learned about the operation of using the while statement to implement a loop, now we will discuss the looping method of the while statement and the looping method of the do while statement in detail.


1. whlie cycle


        The while loop statement is the most basic looping method in C language. Its syntax structure is as follows:


while (conditional expression)
{
        //loop body
}


        while is a keyword in C language, followed by a conditional expression in parentheses as the condition for executing the loop, that is to say, when the result of the conditional expression is true, the program content in the curly brackets is executed. And when the result of the conditional expression is false, the contents in the curly braces are not executed. In fact, this is somewhat similar to the syntax of the if statement. When the conditional expression is true, the execution program after the if is executed only once, while the while will loop and execute until the result of the conditional expression is false.

Let's look at a simple example of a whlie loop that displays dates from 1 to 31 days:

int day = 1;
while (day <= 31)
{
    printf("%d\n", day);
    day++;
}

        The conditional expression of the while statement in this program is day <= 31, which means that when the value of day is less than or equal to 31, the program will execute the content in the curly brackets (loop body) in a loop. There are two statements in the loop body, the first The first statement is to execute a standard output and display the value of day, and the second statement day++; is very important, it means to add one to the value of day on the original basis, when this statement is executed, the program will return to the condition again In the expression, a true or false judgment is made, if it is true, the loop is continued, and if it is false, the loop is ended.
For example, when starting the while statement, the value of day is 1, so the result of day <= 31 is true, so it enters the first loop, displays the value of day 1, and adds 1 to the value of day; the program enters the conditional judgment again, day The value of 2 is 2, so the result of day <= 31 is true, enter the second loop, display 2, and add 1 to the value of day; the program enters the conditional judgment again... This is repeated 31 times, and the value of day is 32. The program enters my conditional judgment again, and the value of day <= 31 is false, so it no longer enters the loop body, and the while loop statement ends.


2. do while statement


        Similar to while, the do while statement is also a way to complete a program loop. Its basic usage is as follows:


do
{
        //loop body
}
while (conditional expression);


        You can notice that the syntax of do while is somewhat similar to while, and there are also some differences. The similarities are that it also has a loop body and a conditional expression, but the execution order is different from that of while. Do while executes the loop body first and then executes the condition. judge.

Note that a semicolon should be added after the conditional judgment of the do while statement; to indicate the end of the statement. Let 's learn the syntax again with an example showing 31 days:

int day = 1;
do
{
    printf("%d\n", day);
    day++;
}
while (day <= 31);

        程序在执行的过程为:首先进入第一次循环显示1并将day的值加1,然后做条件判断day有值为2,于是day <= 31的结果为真,返回到do后面的循环体进入下一次循环...直到day的值为31时,printf显示出31,然后day的值加1,然后做条件判断,day的值为32,于是day <= 31的结果为假,结束循环。

 

三、细微差别


        接下来我们来看一看关于while语句和do while语句的细微差别。do while语句在在执行时无论条件表达式的结果是真还是假,都会执行一次循环体,然后再进行条件判断。例如我们分别写这样两段程序,它们的执行结果是不一样的,请读者自己体会这两种循环的差别,并自己动手编写程序。

使用while执行循环:

int i = 0;
while (i < 0)
{
	i++;
	printf("%d\n", i);
}

        执行0次循环,没有运行结果;

 

        使用do while执行循环:

int i = 0;
do
{
	i++;
	printf("%d\n", i);
}
while (i < 0);

        执行1次循环,运行结果为0。


欢迎关注公众号:编程外星人

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325691283&siteId=291194637