Why can't we define a variable inside a while loop?

后端 未结 6 1294
星月不相逢
星月不相逢 2020-12-03 03:27

We can do:

using (Stream s ..)

and:

for (int i ...)

Why can\'t we as well do something like:



        
6条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-03 03:39

    As opposite to the 'for' loop, 'while' doesn't have a initialization part. The 'for' syntax looks like this:

    for ( initializer; conditional expression; loop expression)
    {
          statements to be executed
    }
    

    and the 'while' looks like this:

    while (condition)
    {
          statements to be executed
    }
    

    The closest thing to your request is:

    int i;
    while ((i = NextNum()) > 0) { ... }
    

提交回复
热议问题