Problems with For loops

Problems with For loops
0.0 0

#1

Hello all. I’ve noticed that I’m having issues in creating for loops during the challenges. I understand the basic concepts of for loops, but I’m having trouble analyzing the key information needed to make a for loop and more importantly, what the for loop’s function is. In other words, I’m looking to see if there is any way I can practice recognizing a need for a for loop, its construction, and its function.


#2

Loops will do repeatable tasks for you. When you need to do something again and again you probably need a loop. Often you can write for or while loop for same task and both will work fine. But usually you will use for loop when you need task to be repeated n times, where n is known number, and you will use while when you need a task to be repeated until certain condition is met. That is, if you think “I need this code to run 10 times” it’s kinda calling for “for loop” and if you think “I need this question to be repeated until user gives right answer” then it’s “while loop”.

If you need exercises you can try codewars, pick there Javascript as language and “loops” in tags, start with easy examples (8-7 kyu) and go further to more complicated.


#3

Here is a trick that helps me sometimes. When I’m not sure how to code something, I’ll write an outline of what I’m trying to do–using plain English, not code. Then I read over the outline, and see if I notice one of the situations the previous poster mentioned.

For example, if my outline includes something like “look at each element in the array and see if it’s bigger than 3,” that is an example of doing a task a fixed number of times, so that means it’s time for a “for loop.”

On the other hand, if instead I wrote “look at each element in the array until I find one that’s bigger than 3,” that means I get to stop checking after I find a “big enough” element. So I’m doing a task until a certain condition is met, and it’s time for a “while loop.”

Hope this helps! :slight_smile:


#4

This link might be helpful:

Usually, his explanations are very clear.


#5

Hi @njanne19:

This is a very basic question in programming and one of the first thing to learn. If you continue further your journey as programmer loops will be something you will do daily, in its many forms.

Ok

First

###What is a loop?

The simplest answer was given above: something you have to do again and again. In a more abstract form, is an action you will repeat on an target. Bear in mind that action includes: “do nothing”.

First thing you might have to know is that the FOR loop is not a function: it is a statement. I won’t discuss that here though.

###What is a FOR loop?
There are few types of loops in programming. If you continue learning programming you will see that in JS those loops can be expressed in different ways. But let’s stick to the FOR loop.

First you need to know that for the FOR loop to work, it requires you to pass it an indexed collection, imagine a collection as a group of elements you keep together. Indexation is taking those elements and giving them a certain order. Usual indexation is an abstraction of position.

One type with those characteristics is the array. You can think of an array as a collection of elements usually indexed from 0 to any higher natural number including the 0 itself. The index represents a “position” in the collection. So 0 is position 0, 1 is position 1, en so for. Bear in mind that you can have an array with no indexing if there are no elements assigned to it.

An array is the usual type to run the FOR loop. The FOR loop (actually, one of its applications and forms) will verify the value of the index of the array, starting from and increasing it by a value you provide to the loop statement. When it reaches or goes beyond the number of elements in the array, the FOR loop stops and the code continues reading other commands after that or give an error.

One important thing is that once started, it is POSSIBLE to go back in the loop, although this is usually a bug if unintended.

###What the FOR loop does?
This is a big question because loops are used for almost everything in programming. In the particular case of the FOR loop, you will apply the same action over a target as many times as the setting of the FOR loop, based on how long the array is. If you have 10 objects in the array you want to check, the loop will run 10 times. You don’t have to know the length of the array in advance, but you have to know that the FOR loop will stop after reaching the end of the array, unless you stop the loop earlier - which you can do!

Bear in mind that for arrays without elements, the FOR loop won’t do anything.

###What else you can do with FOR loops?
Here the most important is what action you want to carry out on the target. Think that:

  • the target can be the elements of the array you are using to control the FOR loop, but not necessarily
  • you can nest loops
  • you can add conditions to skip cases and concentrate on few ones of interest
  • you can break the loop
  • as I said before, you can even go back in the loop (be careful with this one…)

###Where to apply them?
Here you need exercises…

Hope this helps!