This week's book giveaway is in the OCP forum.We're giving away four copies of OCP Java SE 8 Programmer II Exam Study Guide and have Kathy Sierra, Bert Bates, & Elizabeth Robson on-line!See this thread for details.
Win a copy of OCP Java SE 8 Programmer II Exam Study Guide this week in the OCP forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# confusion with for statement

Greenhorn
Posts: 4
Hello,
I'm beginning (absolut noob ) java and now I'm at Array's. But now I'm confused about what i=i+2 or i=i+3 does.
As I understand I++ means:
e.g  0+1

But how this works in this situation?

Result:
10
20
30
50

But if I try
I get:
10
30

What does that i+2 mean in this situation and how it's operating, because 20 and  especially 50 are left out.
And if    I get 10 50.
As I think 10 is always in results, because it always gets i=0, but I dont understand why others are left out.
If 0+2  <50

I now that  I'm missing something important here, so I would be really grateful for Your help !

Happy Holidays !

Rancher
Posts: 2862
32

What does that i+2 mean

That is the value assigned to the index i.  If i was 0 when i=i+2 is executed,  the index i will have the value 2 afterwards before the next iteration of the loop.
With i++, the value of the index i for the next iteration would be 1.

Greenhorn
Posts: 9
• 1
Line 3 :
values = new int[4];
This means that you defined an array with 4 elements with int datatype.
Now, here you need to take note that the array indexing always starts with 0.
So, here it will be 0 to 3.

1.
for(int i=0; i<values.length;i=i+2)

Here, at first the value of i is 0 so, you get the value of values[0] i.e 10.
i increments by 2 and becomes 2, so in the next iteration you get value[2] i.e 30.
i then in next iteration becomes 4 but the condition i<values.length does not satisfy here as 4<4 is false. So the loop terminates.
Hence you get the result as 10 and 30 which is values[0] and values[2] .

2.
for(int i=0; i<values.length;i=i+3)

In this case,
at first the value of i is 0.So, you get the value of values[0] i.e 10.
Next, i is incremented by 3 as you see that you have given the incrementation as i = i+3. So , i becomes 3 and in there you get values[3] i.e 50.
Then , when i = i+3 happens the value of i now becomes 6.
So in the next iteration the condition of i<values.length becomes false as 6<4 is false.
Hence you get the result as 10 and 50 which is values[0] and values[3] .

Jonas Ramanauskas wrote:. . . I'm confused about what i=i+2 or i=i+3 does.
As I understand I++ means:
. . .

Jonas Ramanauskas
Greenhorn
Posts: 4

Kunder Akshay wrote:Line 3 :
values = new int[4]; . . . Hence you get the result as 10 and 50 which is values[0] and values[3] .

Thank You very much! I read and read about these for loops very much and totally got confused and got it all wrong and now Im laughing at myself that I was so stupid. Now Im gonna follow less sources while learning.

I thought that  for(int i=0; i<values.length;i=i+2)    "i" is compared with 10 20 30 50
i=0 < 10
i=2 < 20
i=4  < 30
i=6   < 50  .
Can you imagine I thought that way, and why,  I dont understand myself too.
So it goes like this as now I understand:
i=0 < 0   values[0]
i=2 < 1     values[1]
i=4  < 2      values[2]
i=6   < 3     values[3] .

 Don't get me started about those stupid light bulbs.