What is For loops: Definition and 24 Discussions

In computer science, a for-loop (or simply for loop) is a control flow statement for specifying iteration, which allows code to be executed repeatedly. Various keywords are used to specify this statement: descendants of ALGOL use "for", while descendants of Fortran use "do". There are other possibilities, for example COBOL which uses "PERFORM VARYING".
A for-loop has two parts: a header specifying the iteration, and a body which is executed once per iteration. The header often declares an explicit loop counter or loop variable, which allows the body to know which iteration is being executed. For-loops are typically used when the number of iterations is known before entering the loop. For-loops can be thought of as shorthands for while-loops which increment and test a loop variable.
The name for-loop comes from the word for, which is used as the keyword in many programming languages to introduce a for-loop. The term in English dates to ALGOL 58 and was popularized in the influential later ALGOL 60; it is the direct translation of the earlier German für, used in Superplan (1949–1951) by Heinz Rutishauser, who also was involved in defining ALGOL 58 and ALGOL 60. The loop body is executed "for" the given values of the loop variable, though this is more explicit in the ALGOL version of the statement, in which a list of possible values and/or increments can be specified.
In FORTRAN and PL/I, the keyword DO is used for the same thing and it is called a do-loop; this is different from a do-while loop.

View More On Wikipedia.org
  1. shivajikobardan

    How do for loops work in python?

    str1="hello" for stringElement in str1: print("str1","=",stringElement) Strings are arrays in python and they are represented like this. Now I want to understand how the for loop is working here. My dry run-: for h in str1 print h for e in str1 print e and so on till...
  2. B

    Motional EMF for loops of wire vs conducting plates

    I'm in an intro E&M class, and I'm trying to distinguish between Motional EMF for loops of wire and conducting plates. This question might be kind of silly, but are Eddy currents pretty much the same thing as induced currents in a loop of wire? More specifically, what I am trying to ask is if...
  3. W

    Java Java: Which Term determines this is a Sum?

    Hi all, I have the code below with a 'for' loop. I see the output is 17, the sum of all elements of the (variable?) result. I am a bit confused: I cannot tell which part of the code is used to determine that the values {1,2,10,4,5} must be added to each other? Please comment/correct: 1)We define...
  4. hbz88

    Mathematica For looping problem not getting answer

    Hi, I need some helps. I try to run the code below but its keep give me the value of intial value. Q = 1415; \[Mu] = 1000; \[Sigma] = 500; double dx; double x; double fx; double fxplusdx; double I1; double I2; I1 = 0; I2 = 0; For[x = 0, x <= Q - dx, dx += 0.01, fx = PDF[NormalDistribution[0...
  5. ChrisVer

    If-else statements in for loops

    I've come across a rumor that states that if-else statements can make the loops go slower. Well, it's not rumors since the writer offered numbers (times) to support the statement. http://blogs.sas.com/content/iml/2012/02/13/avoid-unnecessary-if-then-statements-in-loops.html I was wondering...
  6. ChrisVer

    Python Abstract number of for loops

    Hello, well I am not sure how to search for this online, but I raise this question here: Suppose that I have several bins of let's say cuts, here I list 3 but the main idea is to make their numbers tunable by the user: cut1 = { "p10" : "p <= 10" , "11p20 " : "10<p && p<=20" ...
  7. sukalp

    Introduction to Data Structures in C++

    In C++ we have 2 types of data structures :- 1)simple data structure so array and structure come in simple data structure group 2)compound data structure so stack,queue,link list,tree come in compound data structure group. In Indian board CBSE data structure linked list is not included it is...
  8. sukalp

    Is Processing.org the Best IDE for Learning Programming?

    I WANT TO ASK YOU THAT I HAVE LEARNED SCRATCH AS I AM KID OF 17 AND BEGINNER IN PROGRAMMING. I LEARN SCRATCH TO DESIGN BEFORE BUILDING,SOLVE PROBLEMS CREATIVELY. NEXT I AM LEARNING C++ AND FINDING DIFFICULTY WRITING PROGRAMS LIKE ARMSTRONG NUMBER SO I SHOULD LEARN PYTHON BEFORE C++ . I AKSED I...
  9. KonaGorrila

    MATLAB How to get the last variable of a for loop in MATLAB

    Aloha guys, I am just having trouble getting MatLab to output only the last variable of my "for loop." If anyone can help all I am looking for is an example of notation. p.s. I have done this before just not in a while and will continue to look through my old scripts and functions. just not in...
  10. L

    Can I use a for loop to end after entering a desired number of integers?

    I'm wanting to take user input and store it into an array.There is a a minimum and maximum number of input but otherwise the user should be able to enter any number of integers in between. Say the max number is 10 but I only want to enter 5 numbers. I used a for loop but it only ends if the...
  11. S

    MHB Understanding nested for loops when decrementing

    Hello! :-) I am having a hard time with understanding the logic behind decrementing nested for loops. The review problem I'm dealing with is below. This one makes the numbers go backwards. #include <iostream> using namespace std; int main(){ for (int i = 0; i < 5; i++){ for (int j = i; j >=...
  12. Feodalherren

    MATLAB For Loops Homework: Solving Arbitrary Matrices

    Homework Statement Homework Equations - The Attempt at a Solution I've been trying several different ways but I just can't seem to figure this one out. It seems to me like I will need nested for loops, which we haven't learned yet. The way I read the question is that this program needs to...
  13. A

    Using GOTO and For Loops in Quick Basic 64-Bit for Windows

    I am working with Quick Basic, 64 Bit for Windows, which is quite good indeed, but i met again the same usual difficulties to use [do ... loop] for efficient loops. So I decided to better use [goto], as anyone else! But I also found very useful the classical for loop. Here are some useful...
  14. A

    MATLAB Matlab for loops involving matrix

    Given the following system of equations 3x1 + ax2 = 1 ax1 + 4x2 = 0 where -pi ≤ a ≤ pi. We would like to find out the range of values of x1 and x2 that are observed as a is varied. Do this as follows: 1. Create a for loop that uses the variable a as the indexing value, varied from over...
  15. K

    Matlab: factorials without for loops or colon

    The Problem: Write a function that finds the factorial of a positive integer without using for or while loops, the factorial function, or the : range operator. Honestly, I don't really know how to start with this one. If I were able to use a for loop it would be easy, and I don't see how I...
  16. F

    MATLAB While and for loops in MATLAB

    I want to generate a Poisson distributed vector of random numbers, without any of the numbers being 0. The code I have is k = poissrnd(kmean,1,N); % where kmean is the mean of the distribution, and has been defined previously %The above generates a N by 1 vector of Poisson distributed...
  17. N

    MATLAB Matlab solve function in for loops

    Matlab "solve" function in "for" loops I need to solve a similar problem like shown in the below codes for a larger matrix, The 'x' here needs to be solved for each y(a,b). Each of this y(a,b) is equal to zero and 'x' will vary accordingly , so it will give a 2by 2 matrix for 'x' as well...
  18. E

    Solving 3 Non-Linear Equations with Fsolve and FOR Loops

    Hi I'm using the fsolve method to solve for 3 variables in 3 non-linear equations. I would like to find out, how the 3 variables change, when I change a parameter in the 3 equations. Let's assume the parameter runs from 1 to 5 (1:1:5). The iteration looks like this: c=(1:5); for i = 1:5...
  19. S

    Solving MatLab "for" Loops With Vectors

    MatLab "for" loops Homework Statement Write an m-file to evaluate y(x)= x^2 - 3x +2 for all values of x between -1 and 3, in steps of .1 . Do this twice, once with a for loop and once with vectors. Plot the resulting functions. Homework Equations The Attempt at a Solution...
  20. H

    Solve "For" Loops Problem with Mathcad 2001i

    "for" loops problem Homework Statement I would like Mathcad 2001i to calculate a trajectory for me. The idea is to take initiating x,y coordinates and x,y velocity, calculate the x,y acceleration at that point and then calculate where it will be after 1 t, then calculate the new acceleration...
  21. S

    MATLAB Matlab help: indexing in for loops

    Hello! I consider myself typically a confident Matlab user. But I have encountered a new problem that I have no idea how to solve, so I'm hoping I can find some help. I have set up a for loop in my code to the effect of for i = 1:n if MyArray(i,1) < some value perform other...
  22. H

    Setting up a "for loop" Eqn in Matlab for hc

    Homework Statement Can someone help me set up a "for loop" for this equation I intialize hc = x0 then I have to compute hc1 = C.*(log(hc./b)+1)+ gamma.*(1-exp(-hc./gamma)) hc2 = C.*(log(hc1./b)+1)+ gamma.*(1-exp(-hc1./gamma)) hc3 = C.*(log(hc2./b)+1)+ gamma.*(1-exp(-hc2./gamma)) hc4...
  23. C

    Matlab matrix selection of data using for loops and if statements ,,

    matlab matrix selection of data using for loops and if statements,,,,, Hi people I was wondering if someone could assist me with the following; You received the following data values from a coworker: data = [-1.2, 3.3, 5.1, 0.2, 2.9, -0.1]; It turns out that the sensor used to collect...
  24. ranger

    C/C++ Help with C++ for Loops & Patterns

    I need some help. So far I have some for loops that print the following patters: * ** *** **** ***** ****** ******* ******** ********* ********** and ********** ********* ******** ******* ****** ***** **** *** ** * Here is the code: for(int rows=1;rows<=10;rows++) { for(int...
Back
Top