​​Code-Block Level Scope



A variable declared within a loop or any block within brackets has the code-block level scope. A variable declared within a loop or code block cannot be accessed outside of it, whereas a variable declared outside of the loop can be accessible within the loop.



In the above example, a variable i declared within a for loop. So, it can only be accessed within the for loop block and cannot be accessed outside for loop. In the same way, x is declared within the if block, so it can only be accessed in that block but not outside of it.



A variable must be declared outside of the code block to make it accessible to outside code.



💬 The channel has been running since 2018. It needs your support: https://www.patreon.com/csharp1001notes