Wednesday, May 12, 2010

A variable declared inside the for loop control cannot be referenced outside the loop?

True or False (Explain)A variable declared inside the for loop control cannot be referenced outside the loop?
True. A variable declared within a bracket cannot be referenced outside, though any declared outside can be used in any subsequent levels


For example


void main()


{


int i;


if(cond){


int j;


while(cond){


int k;


}


}


}


The variable i can be used anywhere in main. j can be used in the if or while statements only. k can only be used within the while statement.





True in C and Java, at least for the most part.A variable declared inside the for loop control cannot be referenced outside the loop?
True





Java example:





for(int i=0;i%26lt;5;i++)


{


int count=0;


count++;


}





If you do something like this, then everytime count will be reset to 0 and incremented by 1.


Neither you will be able to print its value from outside the for loop.





The correct way is:


int count=0;


for(int i=0;i%26lt;5;i++)


{


count++;


}


System.out.println(count);





The output will be 5. i.e. the no. times the for loop has run.
no it can be referenced if u give its declaration outside
What language?





It can be in PHP.....
can be done ...depends on the scope which u give to it

No comments:

Post a Comment