+ 1

Can we declare a variable inside if statement?

Can we declare a variable inside if statement

18th Jul 2020, 10:51 AM
Prince
Prince - avatar
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?
18th Jul 2020, 10:56 AM
Théophile
Théophile - avatar
+ 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.
18th Jul 2020, 11:48 AM
Lothar
Lothar - avatar