forloop why i can't access int a; why error | SoloLearn: Learn to code for FREE!

+2

forloop why i can't access int a; why error

public class Program{ public static void main(String[]args){ for(int a = 15; a > 0 ; a-- ){ System.out.println(a); } System.out.println(a); // i can't access int a; here } }

6/6/2018 1:19:56 PM

chander Bisht

3 Answers

New Answer

+4

a can only be accessed inside the loops brackets, the scope where it was declared. Outside the scope it is unknown, just like global and local variables work, too. https://en.m.wikipedia.org/wiki/Scope_(computer_science)

+3

you can not access the local variables outside the its block..

+1

thankx Aaron.....for loop also have scope lilke local or global