Why can't you declare a variable inside the expression portion of a do while loop?

前端 未结 6 1987
独厮守ぢ
独厮守ぢ 2021-02-06 23:23

The following syntax is valid:

while (int i = get_data())
{
}

But the following is not:

do
{
} while (int i = get_data());
         


        
6条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-06 23:39

    Your first syntax is valid while the second is not. However, your while loop will loop forever, even if your function get_data() returns 0. Not sure if that's exactly what you want to happen.

提交回复
热议问题