简体   繁体   English

如果条件为真,则在 do while 循环中发布增量(最佳方式?)

[英]Post Increment in do while loop if condition is TRUE (BEST WAY?)

It is quite simple, indeed.确实很简单。

int main()
{
    int n,i,j;
    n = 20;
    i = 0;
    char ch[8];

    do
    {
        ch[i] = (n%2) + '0';
        n /= 2;
        // SIMPLE WAY
        if(n != 0)
            i++;
    }
    while(n != 0);

    for(j=0; j<=i; j++)
    {
        printf("%c",ch[i-j]);
    }

    return 0;
}

But i don't like this WAY但我不喜欢这种方式

I tried the below WAY but code is bad我尝试了以下方式,但代码不好

int main()
{
    int n,i,j;
    n = 20;
    i = 0;
    char ch[8];

    do
    {
        ch[i] = (n%2) + '0';
        n /= 2;
    }
    while(n != 0 && i++); // THIS

    for(j=0; j<=i; j++)
    {
        printf("%c",ch[i-j]);
    }

    return 0;
}

How to get the value incremented only when the loop is true with BEST WAY?仅当使用最佳方式循环为真时,如何才能使值增加? OR just correct 2nd way while(n != 0 && i++)或者只是纠正第二种方式while(n != 0 && i++)

How to get the value incremented only when the loop is true with BEST WAY?仅当使用最佳方式循环为真时,如何才能使值增加?
OR just correct 2nd way while(n != 0 && i++)或者只是纠正第二种方式 while(n != 0 && i++)

I recommend neither approach.我不推荐这两种方法。

There is no need for a special test to see if code should increment i .不需要特殊测试来查看代码是否应该增加i

do {
  ch[i] = (n%2) + '0';
  n /= 2;
  i++;
} while(n != 0);
// Just decrement after the loop
i--;

for(j=0; j<=i; j++) {
    printf("%c",ch[i-j]);
}

Alternatively do not decrement at all或者根本不减少

do {
  ch[i] = (n%2) + '0';
  n /= 2;
  i++;
} while(n != 0);

for(j=0; j<i; j++) {
  printf("%c",ch[i-1-j]);
}

Or use a do ... while for printing also.或者也使用do ... while进行打印。

do {
  ch[i++] = (n%2) + '0';
  n /= 2;
} while(n);

do {
  printf("%c",ch[--i]);
} while (i);

Notes:笔记:

char ch[8]; is too small for an int outside the range [-255 ... 255] .对于[-255 ... 255]范围之外的int来说太小了。 For 32-bit int , use char ch[32];对于 32 位int ,使用char ch[32]; or wider.或更广泛。 In general, use char ch[sizeof(int) * CHAR_BIT];一般来说,使用char ch[sizeof(int) * CHAR_BIT];

ch[i] = (n%2) + '0'; will certainly incur unexpected results when n < 0 .n < 0时肯定会产生意想不到的结果。 Consider unsigned types instead.请考虑使用unsigned类型。

Well, you can always do:好吧,你总是可以这样做:

do {
   // code
} while(n != 0 && ++i);

This way, i is incremented right after the first part of && evaluates to True, and && is applied to n != 0 and the updated version of i .这样,在&&的第一部分计算结果为 True 之后, i增加,并且&&应用于n != 0i的更新版本。

In C++, you might do:在 C++ 中,你可能会这样做:

std::cout << std::bitset<8>(n); // 00010100 for n = 20

if leading 0 are ok.如果前导0

It can be like this:它可以是这样的:

int n = 20;
int i = 7;
char ch[] = "00000000";
while (n) // if n already = 0 then do nothing
{
    ch[i--] = (n % 2) + '0'; // assign then increment
    n /= 2;                // shift to right same as n >>= 1;
}
printf ("%08s", ch);
do
{ .......... }while(n && i++ ? 1 : n);

this stuff about && actions called short-circuit evaluation这些关于&&动作的东西叫做短路评估

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

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