Write a program to demonstrate scope of a variable with in a block.
0
/* Write a program to demonstrate scope of a variable with in a block. */
class Scope{
public static void main(String args[]){
int x;
for(x=0;x<3;x++){
int y=-1;
System.out.println("y="+y);
y=100;
System.out.println("y="+y);
}
}
}
/*
Output:
E:>javac Scope.java
E:>java Scope
y=-1
y=100
y=-1
y=100
y=-1
y=100
*/
class Scope{
public static void main(String args[]){
int x;
for(x=0;x<3;x++){
int y=-1;
System.out.println("y="+y);
y=100;
System.out.println("y="+y);
}
}
}
/*
Output:
E:>javac Scope.java
E:>java Scope
y=-1
y=100
y=-1
y=100
y=-1
y=100
*/
0 comments: