Can you store a variable inside a if-clause?

后端 未结 6 1626
不思量自难忘°
不思量自难忘° 2021-01-01 16:21

I\'m kinda waiting for a \'no\' answer on this question.

I was interested if you can save a variable at the same time when you checking it in an if-clause.

6条回答
  •  无人及你
    2021-01-01 16:53

    I have used that technique when iterating over lines from a BufferedReader:

    BufferedReader br = // create reader
    String line
    while ((line = br.readLine()) != null) {
        // process the line
    }
    

    So yes, you can do an assignment, and the result off that will be the left hand side variable, which you can then check. However, it's not legal to declare variables inside a test, as they would then only be scoped to that expression.

提交回复
热议问题