Unsigned integers in C++ for loops

前端 未结 4 1293
情书的邮戳
情书的邮戳 2020-12-14 03:36

I have made some research on Stackoverflow about reverse for loops in C++ that use an unsigned integer instead of a signed one. But I still do NOT understand why there is a

4条回答
  •  -上瘾入骨i
    2020-12-14 04:09

    The problem is here:

    for (unsigned int i = 9; i >= 0; i--) 
    

    You are starting with a value of 9 for an unsigned int and your exit definition is i >= 0 and this will be always true. (unsigned int will never be negative!!!). Because of this your loop will start over (endless loop, because i=0 then -1 goes max uint).

提交回复
热议问题