Click here to Skip to main content
15,886,578 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So I've made quite a bit of progress but I'm currently puzzled by something: loops.

**TLDR at the bottom:**
Relevant Link:
[DELETED]

Here's what I understand from it all:

a) i is the equivalent of iterator.
b) the `for` loop is used to repeat a code.
c) the iterator expression appears in all three expressions that make up the `for` loop.
d) The start parts for the loop are: initialization, stopping condition and iteration statement.
e) Initialization: where the loop begins, the starting point if you will.

f) Stopping condition: when this condition is met, the loop will stop.

g) Iteration statement: that's how the loop with make its modifications with lack of better words.

h) from what I can tell, there's ++ and -- so far for the iteration statement. I would imagine there are more but I haven't learned them so far.

i) ++ and -- mean add one and subtract one, respectively.

j) In the following example:
![image|388x116](upload://igiXOQI2Pe0CoKL7FaMwWfscpQx.png)
The loop will begin at 0 (initializer), then go up by 1 at a time (iteration statement) until it reaches a number lower than 4 (stopping condition).

k) `console.log` would record the result of this: 0, 1, 2 and 3.

l) We can loop **through** an array. That means to perform the looping operation on each element of the array.

m) In order to loop through an array, we need to use the `.length` property in its condition.

n) In the following example:
![image|387x137](upload://tYK2cV9I9y97QHlgRo5K9NexhT5.png)
We're dealing with an array index so I need to use [n] to identify which array I'm referring to. In other words, [0] is the bear, [1] the Sloth and [2] the Sea lion.

o) Here a loop is introduced followed by the usual brackets ( ). Then a variable is declare as being the iterator which is a bit more confusing as I didn't realize I had to have a variable to use a loop. So now I'm feeling less solid but I carry on. Then I realize `i` here is just name of the variable so no need to fret about it. The next `i` that comes after is the stop condition.

p) So the initialization is at the string 'Grizzly Bear'. Then it says `animals.length` and the increment is +1 because of ++.

**TLDR**: The `.length` sort of puzzles me.
>Remember that arrays are zero-indexed, the index of the last element of an array is equivalent to the length of that array minus 1.

It seems like there's on indication as for when the loop to stop in this case. Could someone explain?

What I have tried:

I've tried what I've written in my original message above.
Posted
Updated 18-Aug-19 19:46pm
v3
Comments
Mohibur Rashid 18-Aug-19 18:40pm    
none of your image example is visible to us.

1 solution

Let's just go back a moment, and think about you when you do a repetitive task: reading a book for example.
You start at the first page (page 1) and read that. then you turn the page, and read page two. Then you turn the page, and read page three. this continues until you have read the last page in the book, when you stop. That's all normal (except for those who read the end first to find out what happened, and we don't need them round here!)
So we can consider that in terms of a loop:
1) set pageNumber to 1
2) if pageNumber > numberOfPagesInTheBook then all done, we've finished the book
3) read page(pageNumber)
4) add one to pageNumber
5) go to (2)

So if we think about it, we have all four elements of the for loop:
Initialization:
1) set pageNumber to 1
Termination test:
2) if pageNumber > numberOfPagesInTheBook then all done, we've finished the book

Body:
3) read page(pageNumber)
Increment:
4) add one to pageNumber

Indicator that it's a loop:
5) go to (2)
So we can write that as a for loop:
Java
for (int pageNumber = 1; pageNumber <= numberOfPagesInTheBook; pageNumber++)
    read(PageNumber);
You can see that that does the same thing, it's just more compact!
When you process each string in an array you are doing the same thing:
Java
String[] animals = {"Grizzly Bear", "Sloth", "SeaLion"};
for (int i = 0; i < animals.length; i++)
    System.out.println(animals[i]);
1) Initialization: Create a variable which will change each time round teh loop, and set it to the first element:
for (int i = 0; i < animals.length; i++)
    System.out.println(animals[i]);

2) Termination test: Have we processed the last row yet?
for (int i = 0; i < animals.length; i++)
    System.out.println(animals[i]);

3) Body: Do something with each element:
for (int i = 0; i < animals.length; i++)
    System.out.println(animals[i]);

4) Increment: move to next element:
for (int i = 0; i < animals.length; i++)
    System.out.println(animals[i]);

Make sense?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900