0

Can we declare variables within if statement and will that particular variable be considered only in the if statement or can it be used outside the statement

15th Oct 2016, 2:42 PM
Dheeraj N
Dheeraj N - avatar
2 odpowiedzi
+ 1
Any variable declared for the first time in a block (if, for, def, etc) can only be accessed within that block. This is called scoping. The scope within the block is different from the scope outside of the block. While externally declared variables can be accessed from within a block, no internally declared variable can be accessed from outside the block in which it was declared. Many times what we do is declare the variable outside the block assigning it some harmless value like 0 or 1 or "" (depending on the use case), then modify it within the block. That way we can use it as we want.
15th Oct 2016, 3:49 PM
John Otu
John Otu - avatar
0
Thank you very much
15th Oct 2016, 4:37 PM
Dheeraj N
Dheeraj N - avatar