简体   繁体   English

在 C 编程语言中使用 do/while 时奇怪的 output

[英]Strange output when using do/while in C programming lang

I am trying to understand the output of this program.我试图了解这个程序的 output。 If I try to "translate" the code, I believe it should go like this:如果我尝试“翻译”代码,我相信它应该像这样 go :

  • while "j" is smaller than 3 - print "Ha" (this loop goes 3 times, so it gives 3 "Ha")而“j”小于 3 - 打印“Ha”(这个循环执行 3 次,所以它给出 3 个“Ha”)
  • do/while -> j is equal to j - 2 hence print "Hi" while ++j - In the end the program prints out "Hi" 4 times. do/while -> j 等于 j - 2 因此打印 "Hi" 而 ++j - 最后程序打印出 "Hi" 4 次。

How does the program prints it 4 times, how does the condition works here?程序如何打印 4 次,这里的条件如何工作?

#include <stdio.h>

int main() {

    int j = 0;
    while(j++ < 3){
        printf( "Ha ");
    }
    do{
        j -= 2;
        printf( "Hi "); 
    }
    while(++j);
    for(j = 1; j <= 3; j++){
        printf( "Ho ");
    }
    printf("\n");
    return 0;
}

The output is: output 是:

Ha Ha Ha Hi Hi Hi Hi Ho Ho Ho哈哈哈哈嗨嗨嗨嗨嗬嗬嗬嗬

The ++j is prefix increment, ie, the value will be increased and then, the incrased value will be used for the condition check. ++j是前缀增量,即值将增加,然后,增加的值将用于条件检查。

I have added a print statement for the ease of understanding:为了便于理解,我添加了打印声明:

do{
    j -= 2;
    printf( "Hi "); 
    printf("value of j before the while = %d\n", j);
}
while(++j);

and the output is: output 是:

Hi value of j before the while = 2
Hi value of j before the while = 1
Hi value of j before the while = 0
Hi value of j before the while = -1

So, in the所以,在

  • first iteration, in while (++j) , j is 2 , and ++j is 3第一次迭代,在while (++j)中, j2++j3
  • second iteration, 3-2 = 1 , and ++j is 2.第二次迭代, 3-2 = 1++j为 2。
  • third iteration, 2-2 = 0 , and ++j is 1.第三次迭代, 2-2 = 0++j为 1。
  • Fourth iteration, 1-2 = -1 , and ++j is 0 - this make the while chec false and the loop ends.第四次迭代, 1-2 = -1++j为 0 - 这使得while chec 为假并且循环结束。

After the first while loop在第一个 while 循环之后

while(j++ < 3){
    printf( "Ha ");
}

j is equal to 4. j 等于 4。

So within the first iteration of the do-while loop所以在 do-while 循环的第一次迭代中

do{
    j -= 2;
    printf( "Hi "); 
}
while(++j);

j is equal to 2. After the first iteration j is equal to 3. Within the second iteration j is equal to 1. After the second iteration j is equal to 2. Within the third iteration j is equal to 0. After the third iteration j is equal to 1. Within the forth iteration j is equal to -1. j 等于 2。第一次迭代后 j 等于 3。第二次迭代内 j 等于 1。第二次迭代后 j 等于 2。第三次迭代内 j 等于 0。第三次迭代后j 等于 1。在第四次迭代中 j 等于 -1。 So in this forth iteration in the condition所以在条件的第四次迭代中

while(++j);

j is equal to 0 and the control is passed to the next loop. j 等于 0 并且控制传递到下一个循环。 So the do-while loop was executed 4 times.所以 do-while 循环执行了 4 次。

That is the value of the postfix increment operator is the value of its operand before incrementing.即后缀自增运算符的值是其操作数在自增前的值。 And the value of the pre-increment operator is the value of its operand after incrementing.而前自增运算符的值就是其操作数自增后的值。

After first loop第一次循环后

j == 4
j -= 2 == 2
Hi
++j == 3
j -= 2 == 1
Hi
++j == 2
j -= 2 == 0
Hi 
++j == 1
j -= 2 == -1
Hi
++j == 0 //end of th loop

The best way to understand what your program does is to watch the value of j at each step of your program.了解程序所做的最好方法是在程序的每个步骤中观察 j 的值。

first j=0 then you enter into the while.首先 j=0 然后你进入 while。

    1. j=0; j=0; j<3? j<3? ; ; yes;是的; j=j+1; j=j+1; it print "Ha".它打印“哈”。
    1. j=1; j=1; j<3? j<3? ; ; yes;是的; j=j+1; j=j+1; it print "Ha".它打印“哈”。
    1. j=2; j=2; j<3? j<3? ; ; yes;是的; j=j+1; j=j+1; it print "Ha".它打印“哈”。
    1. j=3; j=3; j<3? j<3? ; ; no;不; j=j+1; j=j+1; leave the while statement离开 while 语句

j=4 j=4

then you leave the while to enter into de do, while() statement.然后你离开 while 进入 de do, while() 语句。 at this moment j=4.此时 j=4。

in the do, while():在做,while():

    1. j=j-2; j=j-2; j=2; j=2; it print "Hi";它打印“嗨”; j+=1; j+=1; j=3; j=3; j?=0 ? j?=0 ? yes是的
    1. j=j-2; j=j-2; j=1; j=1; it print "Hi";它打印“嗨”; j+=1; j+=1; j=2; j=2; j?=0 ? j?=0 ? yes是的
    1. j=j-2; j=j-2; j=0; j=0; it print "Hi";它打印“嗨”; j+=1; j+=1; j=1; j=1; j?=0 ? j?=0 ? yes是的
    1. j=j-2; j=j-2; j=1; j=1; it print "Hi";它打印“嗨”; j+=1; j+=1; j=0; j=0; j=0? j=0? no -> leaving the do while()否 -> 离开 do while()

at the end you enter in the for statement.最后,您输入 for 语句。

  • ++x is called Pre incrementation (the variable is incremented before the expression evaluation) ++x称为预增量(变量在表达式求值之前递增)
  • x++ is the post incrementation (the variable is incremented after the expression evaluation x++后自增(变量在表达式求值自增
  • i'm not sure about this, you need to check this informations.我不确定这个,你需要检查这个信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM