D
D
Dima Sokolov2017-05-20 19:24:54
Programming languages
Dima Sokolov, 2017-05-20 19:24:54

Why do most programming languages ​​allow assignment in a condition?

This increases the number of errors and reduces the readability of the code. In what cases can such an assignment be useful?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Daniil Kolesnichenko, 2017-05-20
@KolesnichenkoDS

char c;
while ((c = getchar()) != EOF) {
  // do something
}

But in general it is a hack of course. In Python, Ruby, Rust, for example, this cannot be done, and I think this is right.

A
Alexey Nikolaev, 2017-05-20
@Heian

They operate with constructions and expressions. The expression is all that matters, and that's the reason expressions can be used in conditions. They can also be used in conjunction with the return statement - that doesn't bother anyone, does it? What about the expressions in the for condition? Therefore, expressions in conditions should not be embarrassing, all the more so because sometimes it is very beautiful.

X
xmoonlight, 2017-05-20
@xmoonlight

This is useful when you need to get a value from a function or method for a condition, and then apply it multiple times inside an expression in the condition:
if(a=getA()&&a>b){...}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question