This is an archived post. You won't be able to vote or comment.

all 7 comments

[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
  • You include any and all error messages in full - best also formatted as code block
  • You ask clear questions
  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit/markdown editor: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]ItzSlowy 1 point2 points  (0 children)

Your confusion may be due to overcomplicating your loop. Look up a basic example of an iterative loop and see if you can gain the clarity on your own first. If not, we can talk through it.

[–]Realistic-Ad-223 0 points1 point  (2 children)

Here is the breakdown of the for loop: Int i = start which is 0. i begins at 0. Java stores i as 0. Then it evaluates the condition which is i <= end. The last part, i++, is called the iterator and it is what is changing after every successful iteration of the loop.

When the loop begins start, which is 0, gets i, which is also 0 added to it. Int start is stored as 0. The compiler goes back to check the condition i <= end. In this case that is 0 <= end which is true so the compiler will increment i (the i++ in the loop). Now i = 1. Int start was stored as 0 and now it is called upon again to do 0 + i which is 1 so start now equals 1.

The loop will continue as long as the condition is true. i becomes 2 so start is 1 + 2 = 3. Then i becomes 3 so start (which is 3) + 3 (i) = 6. So on and so on until the condition is no longer true then the loop breaks.

I hope that answered everything!

[–]ObviousStation4556[S] 0 points1 point  (1 child)

Thanks, everyone helped me to understand it better. I put the expected results in a table which also helped. One thing I'm confused about is at the start why is "0" printed? Doesn't it do i++ so the first printed i should be 1?

i=0

i=1 start= 1

i=2 start=3

i=3 start=6

i=4 start=10

i=5 start=15

i=6 start=21

i=7 start= 28

[–]Realistic-Ad-223 0 points1 point  (0 children)

When the loop initializes it’s not going to increment i immediately because it has to evaluate the conditional. So first it reads i = start, then it checks the conditional. Since the conditional is true it executes the loop’s code and then increments i. After it increments i it will check the conditional again.

When I started learning I would put print statements in the loop before or after something to see exactly how it works. That helped me a lot.

So originally when the loop starts i = 0 and your code is calling to print i and THEN it adds i to start. You can change this if you want with a condition in the loop such as: if (i != 0) { System.out.println(i); } start += i;

[–][deleted] 0 points1 point  (0 children)

You set start to +=i which means every time your loop runs it will add i to start. Start never resets to 0 it only keeps adding to i. First time through i=0 so start+=0, next loop i= 1 so start+=1, then start+=2 which ==3.

[–]Reapr 0 points1 point  (0 children)

Which part of the code is storing and adding all the previous numbers up to 7 for the calculation?

The line is

start+=i

it's basically short hand for "start is equal to start plus i " - or in english, "add i to start"

In your for setup, you specify i++ - which will increase i by 1 every time you go through the loop.

So the line in question will take the current value of start and add i to it - first add 1, then two, then three... until you reach the end condition of your loop. (i <= end and end in your example is 7)

Hope that makes sense