简体   繁体   中英

I have a question about c programming using While

#include <stdio.h>
int main() {
    int n;
    scanf("%d", &n);
    while(n>0) {
        printf("%d ", n);
        n--;
    }
    return 0;
}

Why doesn't this command work when the while conditional sentence is (n==0)?

while(condition) cycle runs, as its name proposes, while the condition is True (or non zero in C). As soon, as it becomes False (or 0), the cycle is aborted. If you place n==0 as a condition, the cycle is aborted right away. You can change this by typing while(n != 0) , or while(!(n==0))

The operator > excludes the number itself. Use >= if you want it to work with the number you are using as operand

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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