Z
Zachary Turner
Hello,
This seems like an extremely basic question, and I'm a bit embarassed
that I can't answer it myself.
I just recently started using GCC and tried to type the following
code:
while ((int i = getint()) != 0)
{
/* something */
}
which my compiler has been supporting for a good few years. Under GCC
this doesn't compile. At first it didn't even occur to me that maybe
the code was invalid. I just thought GCC was being stupid. But then
it occurred to me that this probably really isn't valid C++, and that
my compiler has just been breaking the rule. Can anyone shed some
light? Can you declare a variable in a while loop similar to how you
can with a for loop?
This seems like an extremely basic question, and I'm a bit embarassed
that I can't answer it myself.
I just recently started using GCC and tried to type the following
code:
while ((int i = getint()) != 0)
{
/* something */
}
which my compiler has been supporting for a good few years. Under GCC
this doesn't compile. At first it didn't even occur to me that maybe
the code was invalid. I just thought GCC was being stupid. But then
it occurred to me that this probably really isn't valid C++, and that
my compiler has just been breaking the rule. Can anyone shed some
light? Can you declare a variable in a while loop similar to how you
can with a for loop?