aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "doubt" Watch "doubt" New topic
Author

doubt

lp
Ranch Hand

Joined: Nov 08, 2000
Posts: 52
i came across this question in JQ++. it is as follows:
public class Test
{
public static void main(String[] args)
{
int sum=0;
for(int i=0,j=10;sum>20;++i,--j)
{
sum +=i+j;
}
System.out.println(sum);
}
}
i thought the answer would be 20 but instead when i saw the answer it was 0.could anyone please explain this .
Thanks
Lakshmi
Michael Ernest
High Plains Drifter
Sheriff

Joined: Oct 25, 2000
Posts: 7292

The for loop never executes because the test condition (sum > 20) fails on the first pass.
------------------
Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide


Make visible what, without you, might perhaps never have been seen.
- Robert Bresson
lp
Ranch Hand

Joined: Nov 08, 2000
Posts: 52
Thanks i got it .
Roopa Bagur
Ranch Hand

Joined: Nov 03, 2000
Posts: 267
You are getting 0 as the output because the condition sum>20 is false & the program jumps out of the for loop printing sum as 0.
Change the condition to sum<=20.<br />
Originally posted by lakshmi panda:<br /> i came across this question in JQ++. it is as follows:<br /> public class Test <br /> {<br /> public static void main(String[] args)<br /> {<br /> int sum=0;<br /> for(int i=0,j=10;sum>20;++i,--j)
{
sum +=i+j;
}
System.out.println(sum);
}
}
i thought the answer would be 20 but instead when i saw the answer it was 0.could anyone please explain this .
Thanks
Lakshmi

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: doubt
 
Similar Threads
Declarations and access control
Scanner class help
Euler31
Abundant Numbers
Fibonacci Sequence Problem