| Author |
scjp chapter 5 q...5 page no 303
|
sapana jain
Ranch Hand
Joined: Oct 24, 2007
Posts: 42
|
|
class Foozit { public static void main(String[] args) { Integer x = 0; Integer y = 0; for(Short z = 0; z < 5; z++) if((++x > 2) || (++y > 2)) x++; System.out.println(x + " " + y); } } i think there is syntax error as they have not opened the curly brackets.....for for loop please help me..............
|
 |
P Ventura
Ranch Hand
Joined: Jan 24, 2007
Posts: 42
|
|
If a for isn't followed by braces, it loops the statement that follows the for. In this case "If" is the looped statement.
|
Objective: SCJP 1.5<br /><a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Thread.html" target="_blank" rel="nofollow">API</a><br /> <blockquote><font size="1" face="Verdana, Arial">code:</font><hr><pre><font size="2"><br />Double n1 = Double.NaN; Double n2 = Double.NaN;<br /> n1.equals(n2) // true even though Double.NaN != Double.NaN<br />-0.0 == +0.0; // true<br />Double n1 = -0.0;Double n2 = +0.0;<br />n1.equals(n2) // false even though -0.0 == +0.0<br /></font></pre><hr></blockquote>
|
 |
antonio ciambellari
Greenhorn
Joined: Sep 27, 2007
Posts: 11
|
|
|
Is the result x=4 and y=3?
|
 |
al nik
Ranch Hand
Joined: Oct 18, 2007
Posts: 60
|
|
I think the output is x=8 y=2 the syntax it's ok the for cycle iterates 5 times 1st: x=1 , y=1 2nd: x=2 , y=2 3rd: x=4 , y=2 4th: x=6 , y=2 5th: x=8 , y=2
|
SCJP5 - SCWCD5 - SCBCD5
|
 |
Manoj Macwan
Greenhorn
Joined: Aug 03, 2007
Posts: 22
|
|
The answer of this question is 8 2. As said in the earlier post - If a for isn't followed by braces, it loops the statement that follows the for. In this case "If" is the looped statement. Here we are looping 5 times and each time values of X and Y are: x y 0 - 1 1 1 - 2 2 2 - 4 2 (As the first expr is evaluated as TRUE, second expr won't executed) 3 - 6 2 (As the first expr is evaluated as TRUE, second expr won't executed) 4 - 8 2 (As the first expr is evaluated as TRUE, second expr won't executed)
|
 |
sapana jain
Ranch Hand
Joined: Oct 24, 2007
Posts: 42
|
|
Originally posted by P Ventura: If a for isn't followed by braces, it loops the statement that follows the for. In this case "If" is the looped statement.
thanks a lot..........i am preparing for scjp and having many problems........so i just take chance to post my problem and in returned i got many mails and now my problem is solved....... hope you will help me in future also.........
|
 |
 |
|
|
subject: scjp chapter 5 q...5 page no 303
|
|
|