chrabyrd
chrabyrd

Reputation: 323

Can someone explain what a "for" loop is in the simplest terms possible?

I'm trying to learn coding but I can't wrap my head around what happens in a "for" loop.

if/else if/else statements are easy.

if (this is true) {
do this.

else [otherwise, but] if (this is true) {
do that.

else
do this.

"while" and "do/while" loops are easy too.

while (this is is true) {
do this.

or

do this {
}

while (this is true).

Now, I know the structure of a "for" loop: (start, finish, increment). I can even make one that works for basic problems. However, I don't know what the computer is "thinking." I can't put it into basic English terms like I can the others. Any help would be appreciated.

Upvotes: 1

Views: 8435

Answers (7)

Smith John
Smith John

Reputation: 1097

All "loops" are the same thing. "while" loop can be replaced by the "for" loop.

int i = 0;
while (i < 10){
do something;
i++;

}

and "for" loop is for (i = 0; i < 10; i++) do something;

Upvotes: 0

Yona Appletree
Yona Appletree

Reputation: 9142

In many programming languages, for loops are a short-hand form of a variety of commonly-used looping scenarios that could be accomplished, albeit more verbosely, with a combination of variables, if statements and while loops.

Every language defines its own for loop semantics (or doesn't have for loops at all), but there are two very common types that many languages implement:

The "setup", "condition", "update" loop which is commonly used to iterate a certain number of times, updating a variable to keep track of where in the iteration you are. In C-style languages (e.g. C/C++, Java, Javascript) it looks like this:

for (setup; condition; update) statement;

This can be represented using a while loop like this:

setup;
while (condition) {
    statement;
    update;
}

In english, you can think of it as:

Perform these { setup } steps.
While { condition } is true: perform { statement } and { update }. 

As mentioned, this is commonly used to iterate a certain number of times:

for (int i=0; i<10; i++) doSomethingWith(i);

The iterate collection or for each loop is used to do something with each element in a collection of elements, like a list, array or map / associative array. The syntax for this style of loop is less consistent across languages, but generally takes a form like for (element in collection) statement;

In Java, for example, iterating over a collection takes this form:

for (Type obj : collectionOfObj) statement;

which, in english, reads something like this:

for each thing in { collectionOfObj }, assign the thing to a variable named { obj } of type { Type } and then execute { statement }.

Many languages have different syntax or slightly different rules about how for loops function. Some languages are designed to allow the programmer to build different looping constructs using the language itself. I suggest that you read up on the constructs in the language you're using and if you understand while and if, you'll have no trouble getting used to the various forms of for as you need to use them.

Upvotes: 0

user4413591
user4413591

Reputation:

Basics

For loops are short-hand while loops in numerical contexts.

Rather than write this:

x = 0;
while (x < 5) {
  x++;
}

You can write this:

for (x = 0; x < 5; x++)

Syntax

Oh, and the parts are as follows:

for (start; condition; increment)

Where they mean the following:

  1. The start or initialization is the initial variable.
  2. The condition is works like the part in parenthesis in a while statement; the for's body will continue to execute until this becomes false.
  3. The increment or update is how the variable is changed; without it, the loop runs indefinitely.

Upvotes: 6

Display name
Display name

Reputation: 1552

In C++ the for construct works very much like a while. The for loop has three fields in the ()'s. They are the initialization, the test (this is the part that works as a while) and the increment field.

When the for loop is first encountered, the initialization takes place, but it only takes place once; after that it is never executed again.

Then the condition is tested, if it is true then the body of the loop is executed.

Lastly the increment field is executed. Typically one would put in a variable increment like x++ or something like that but it can be valid statement.

To do the same thing as a for loop in a while syntax is possible as so:

int index = 0;
while( index < count )
{
  do_stuff();
  index++;
}

in a for loop it would look like this:

for( int index = 0 ; index < count ; index++ )
{
  do_stuff();
}

Upvotes: 0

Hack-R
Hack-R

Reputation: 23211

A for loop simply means that you're instructing the program to perform an action repeatedly.

What's the action? It's whatever you decide that it is.

How many times do you do it? For as many times as your for loop specifies.

What do you do it to? That's also something that you specify.

Example:

for(i in 1:nrow(x)){
  x[i,3] <- "A"
}

That's a for loop in R. It saying that for row 1 to the last row of an object called x make column 3 the character "A".

The action is <- "A" which means assign a value of the character A.

How many times to do it? 1:nrow(x)

What to do it to? x[i,3]

Another example:

for(i in 1:10){
    a[i] <- TRUE
 }

Here the action is to assign a value of TRUE.

The number of times to do it is (for) 1 to 10 (i.e. 1, 2, 3, 4, 5, 6, 7, 8, 9, 10).

What to do it to? The elements of object a.

Upvotes: 1

nick_eu
nick_eu

Reputation: 3723

Basically the computer is looking for a list of values, and is saying "whatever you put in your loop, I'm going to do once for each item in this list". Thus the "for".

What makes this confusing is that you often don't see the "list" the computer is using. The format of for-loops varies by programming language, but in your case, consider the example where you're working with:

for (x = 0, x < 4, x ++){
    print(x)
} 

you can think of there being a list of numbers that starts at 0 and runs to 4 with step sizes of 1.

So the computer is thinking "for each value in the list of values 0,1,2,3, I'm going to do what's in the loop".

Upvotes: 0

Denley Bihari
Denley Bihari

Reputation: 601

A for loop is just convenience notation of a common structure of a while loop.

Consider a while loop structured like so (a very common use case):

int i = 0;
while (i < size) {
    float value = arrayOfValues[i];

    // do something with the value

    i = i + 1;
}


The following for loop does the same thing but it just simplifies the code a little, and it reduces the scope of i to the loop in some languages such as java.

for(int i = 0; i < size; i = i + 1){
    float value = arrayOfValues[i];

    // do something with the value
}


In some languages (such as java) there is an even further simplification of the notation like so:

for(float value:arrayOfValues){
    // do something with the value
}

Upvotes: 0

Related Questions