+ 1
Can we declare a variable inside if statement?
Can we declare a variable inside if statement
2 Answers
+ 6
It depends on the langage, I think, but it seems that many langages allow that.
You simply must be careful with the scoping rules (a variable declared in a 'if' block might not be usable outside this block).
[Edit]
Do you actually mean declaring a variable in the condition? Or in the following block?
+ 6
python allows creation of variables in an if... elif... else... clause, so that they can be used also outside the conditional block. This is also true for nested conditionals.