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


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "for loop" Watch "for loop" New topic
Author

for loop

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi,
class method
{public static void main(String a[])
{int num[] ={1,2,3,4};
for (int i = 0;i < num.length;++i)
System.out.println(num[i]);
}
}
The o/P;1,2,3,4.
But shouldnt it be 2,3,4.Since for every iteration first i is incremented by 1 and other statements are executed.IF for statement is replaced by
for(int i = 0; i < num.length; i++)
the o/p is same.
Anyone pl. explain me how is the program working?
Thanks!

[This message has been edited by Jim Yingst (edited August 03, 2000).]
[This message has been edited by avn (edited August 03, 2000).]
mita
Ranch Hand

Joined: Jul 19, 2000
Posts: 50
Hi,
I think the given answer is correct.The iteration part executes after the first execution of the loop.So it starts with i = 0 here.
Hope I made it clear.
Mita
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
hi
when the program executes the for loop first time it takes initilization value ,executes conditional expression and then if true enters the for loop ,the lost incremental expression ++i (i++) will be executed after every time body of the if loop completes.So i++ or ++i doesnot make difference out here .
now traverse the loop u will get 1,2,3,4
i hope this explanation helps
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: for loop
 
Similar Threads
NumberFormat (22,34,12,546.08)
Hello all ! Welcome to Maha's new Home. :-)
Simple program: NEED HELP!!
adding an argrument
please help figure out why the standard deviaion is not giving the correct output