Why is this an infinite (while) loop?


I'm currently going though C++ Principals and Practices and starting on loops. I get the concept, how to use them, and what they're for but something is missing.

int main()
    char a = 'a';
    while (a < 150) {
        cout << a << endl;

This causes an infinite loop and i'm not sure why. a is being incremented by 1 each time, yet the condition of (a < 150) is never met. I'm thinking it might have something to do with conversion / type promotion (or lack thereof) but am not sure. Maybe a isn't being evaluated as an int in the condition?

I realize this is an obnoxiously trivial question but can't seem to find an answer elsewhere. I apologize in advance if it's a repost or if i missed something.

Thank you in advance!

Range of char is -128 to 127. '++a' wraps from 127 to -128. Try declaring a as an unsigned char instead of char and see what happens.
Whether this loop will be infinite depends on options of the compiler you are using. Type char can behave either as signed char or as unsigned char.
That did it! Thank you so much!!! :)
Topic archived. No new replies allowed.