Question

I don't enter the loop using size_t. Why? If I use int I enter the loop and the output is : 11, 11

int main()
{
   int j = 11; 
   for (size_t i = 11; i > - 1; --j, i -=12)
   {

       std::cout << i << ", " << j << std::endl; 

   }
   return 0;
}
Was it helpful?

Solution

size_t is typically an unsigned value. In your for loop condition you're saying i > -1. Since i is an unsigned type the -1 is converted to this type, and will have the maximum value for that type.

Because of the promotion you're effectivly comparing 11 (the initial value of i) with the maximum value for size_t and the result is false, thus the loop is never entered.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top