简体   繁体   English

for循环运行long long int,但不运行unsigned long long int

[英]for Loop running for long long int but not for unsigned long long int

I am running the following for loop 我正在运行以下for循环

for(unsigned long long int i = N-1; i >= 0; i--){
    cin>>L[i];
}

when the program reaches this code segment, it stops responding. 当程序到达此代码段时,它将停止响应。 But when i remove the unsigned , like this 但是当我删除unsigned ,像这样

for(long long int i = N-1; i >= 0; i--){
    cin>>L[i];
}

it works fine. 它工作正常。 Why is this happening? 为什么会这样呢?

When i is unsigned, i >= 0 is always true, and so your loop condition is always satisfied. i为无符号时, i >= 0始终为true,因此始终满足您的循环条件。

When i is 0, i-- causes i to be equal to std::numeric_limits<unsigned long long>::max() , which varies by system, but a typical value would be 9223372036854775807. Your loop counter would then start counting down from there. i为0时, i--导致i等于std::numeric_limits<unsigned long long>::max() ,该值随系统而异,但是典型值为9223372036854775807。然后,循环计数器将开始递减计数从那里。

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

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