while(n-)和while(n=n-1)之间的区别是什么

what is the difference between while(n--) and while(n=n-1)?

本文关键字:while 区别 之间 是什么 n-1      更新时间:2023-10-16

while(n--)while(n=n-1)之间有什么区别?当我在代码中使用while(n=n-1)时,我可以输入少于1个数字。

示例:第一次输入3比输入3乘以单个数字(但在while(n=n-1)中没有发生这种情况)。但当我使用while(n--)时,这是正常的。

我的代码是:

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int n;
    long long inum;
    scanf("%d", &n);
    while(n--)
    {
        scanf("%lld", &inum);
        if(inum == 0 || inum % 2 == 0)
        {
            printf("evenn");
        }
        else
        {
            printf("oddn");
        }
    }
    return 0;
}

n--的值是n 的前一个值

int n = 10;
// value of (n--) is 10
// you can assign that value to another variable and print it
int k = (n--);                     // extra parenthesis for clarity
printf("value of n-- is %dn", k);

n = n - 1的值比n 的前一个值小1

int n = 10;
// value of (n = n - 1) is 9
// you can assign that value to another variable and print it
int k = (n = n - 1);                     // extra parenthesis for clarity
printf("value of n = n - 1 is %dn", k);

while(n--)在其主体中使用n,递减的值用于下一次迭代。

while(n=n-1)while(--n)相同,后者递减并在其主体中使用n的这个新值。