File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes an int outside a for loop, which is printed after the for loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "an int outside a for loop, which is printed after the for loop" Watch "an int outside a for loop, which is printed after the for loop" New topic
Author

an int outside a for loop, which is printed after the for loop

Jasper Vader
Ranch Hand

Joined: Jan 10, 2003
Posts: 284
What will happen if you attempt to compile and run the following code
public class TimDig{
public static void main(String argv[]){
TimDig td = new TimDig();
td.samcov();
}
public void samcov(){
int i=1;
int j=2;
if((i==20) && (j==(i=i*2))){
}
System.out.print(i);
if((i==20) & (j==(i=i*2))){}
System.out.print(i);
int x = i & 2;
System.out.print(x);
}
}

What is the deal with the variable int i? is it being affected by the for loops, and what will happen when
int x = i & 2; ?


giddee up
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8829
    
    5
Jasper -
I feel like we're old friends!
What are you trying to do with the various instances of '&&' and "&' in your code?
-Bert


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Jasper Vader
Ranch Hand

Joined: Jan 10, 2003
Posts: 284
hey there Bert!
not my code but the code of a mock-exam q.
&& is a short circuit operator to see if both are true. The first expression is not so nothing gets a look-in for the second expression.
The first & operator looks at the boolean values.
The second & operator does an AND job on the 0's and 1's (which && cannot do).
Jasper Vader
Ranch Hand

Joined: Jan 10, 2003
Posts: 284
public class TimDig{
public static void main(String argv[]){
TimDig td = new TimDig();
td.samcov();
}
public void samcov(){
\\here is where instance variable i is assigned the value of 1
int i=1;
\\here is where instance variable j is assigned the value of 2
int j=2;
\\here is where i nearly gets doubled but the short circuit AND prevents it. I guess if 'i' had been declared in that if loop, it's only scope would have been there. If 'i' had been declared in the class body then this method would have been doing stuff to what the reference gives but not changing the initial value of 'i'?
if((i==20) && (j==(i=i*2))){
}
\\if this print line was in the curly brackets after the AND line, it would not get printed. As it is, 'i' gets printed out (which is 1)
System.out.print(i);
\\aha, here is where 'i' gets multiplied by 2. But I can't help wondering what would have happened if i was defined inside this if loop - i guess it would not have been accessible outside.
if((i==20) & (j==(i=i*2))){}
\\again, this call to print to the screen is not inside the curly brackets of the for loop so it happens no matter what. Except that now 'i' has the value of 2.
System.out.print(i);
\\a new local variable is introduced, 'x',. 'i' is now 2, or 10 in binary, as is the literal 2. 0 AND 0 gives 0; 1 AND 1 gives 1, so i AND 2 gives the result 2, so x is 2 and gets printed out.
int x = i & 2;
System.out.print(x);
}
}
end situation - 122 gets printed.

[ January 15, 2003: Message edited by: Jasper Vader ]
 
Don't get me started about those stupid light bulbs.
 
subject: an int outside a for loop, which is printed after the for loop