Review: Looping (article) | Looping | Khan Academy (2024)

Want to join the conversation?

Log in

  • Enn

    10 years agoPosted 10 years ago. Direct link to Enn's post “Here it is said that most...”

    Here it is said that most programmers use for loops to avoid the risk of creating an accidental infinite loop but sometimes a while loop makes more sense.
    When is a while loop preferred over a for loop ?

    (389 votes)

    • wtasfias

      10 years agoPosted 10 years ago. Direct link to wtasfias's post “A for loop is used when y...”

      Review: Looping (article) | Looping | Khan Academy (4)

      Review: Looping (article) | Looping | Khan Academy (5)

      Review: Looping (article) | Looping | Khan Academy (6)

      A for loop is used when you know how much you need to repeat the code. However, while loops can be used if you don't know how many times you will need to repeat it.

      (546 votes)

  • dmkim2001

    10 years agoPosted 10 years ago. Direct link to dmkim2001's post “why do you do this?: rect...”

    why do you do this?: rect(col*20, row*20, 20, 20);
    why do they mutiply?
    and can somebody explain to me how to use a nested for loop? it's hard to understand...

    (116 votes)

    • Johnny Wei

      10 years agoPosted 10 years ago. Direct link to Johnny Wei's post “A nested for loop is a fo...”

      Review: Looping (article) | Looping | Khan Academy (10)

      Review: Looping (article) | Looping | Khan Academy (11)

      Review: Looping (article) | Looping | Khan Academy (12)

      A nested for loop is a for loop inside another for loop. It's a quick and easy way to draw multiple identical shapes in a grid. As for the code, we use variables instead of numbers for x and y values so that when the variable changes, the location of the shape will change as well. Or, in this case, changing the variable will draw another identical shape at a new location.

      (108 votes)

  • arjain

    9 years agoPosted 9 years ago. Direct link to arjain's post “In the project, Build a h...”

    In the project, Build a house you asked to use % operator to change color, how can I do that in this loop;
    for(var i=0;i<400;i= i+27) {
    var grass = getImage("cute/GrassBlock");
    image(grass,i,350,30,50);
    }

    (14 votes)

    • Eswag

      9 years agoPosted 9 years ago. Direct link to Eswag's post “Something like:```for(va...”

      Review: Looping (article) | Looping | Khan Academy (16)

      Review: Looping (article) | Looping | Khan Academy (17)

      Review: Looping (article) | Looping | Khan Academy (18)

      Something like:

      for(var i=0;i<400;i= i+27) {
      if(i%2===0){
      fill(color1);
      } else{
      fill(color2);
      }
      rect(i,350,30,50);
      }

      (60 votes)

  • Thendral

    7 years agoPosted 7 years ago. Direct link to Thendral's post “I feel while loops are wa...”

    I feel while loops are way more easier than for loops. Does other people feel that way, or is it just me who is thinking about that?

    (23 votes)

    • Bob Lyon

      7 years agoPosted 7 years ago. Direct link to Bob Lyon's post “Beginners like `while` lo...”

      Review: Looping (article) | Looping | Khan Academy (22)

      Beginners like while loops because there seems to be less going on. Once they recognize a pattern common to most loops, they appreciate the for loops.

      (22 votes)

  • IsaacGranata35

    5 years agoPosted 5 years ago. Direct link to IsaacGranata35's post “I don't understand the va...”

    I don't understand the variable++, can someone explain?

    (4 votes)

    • James McKinney

      5 years agoPosted 5 years ago. Direct link to James McKinney's post “It is a short hand to inc...”

      Review: Looping (article) | Looping | Khan Academy (26)

      Review: Looping (article) | Looping | Khan Academy (27)

      It is a short hand to increment a variable.

      x++; is the same as x = x + 1; it adds one to the current value of the variable.

      You can also do x += 1; or x+=2; x+=200; etc. This notation allows you to add any value to the current value of the variable.

      inside of a for loop it is indicating how much the variable changes every time the loop ends

      for example

      for (var i = 0; i < 10; i++){}

      here you have 3 statements that control the loop.

      var i = 0 states The loop is controlled by the variable i starting at the value 0

      i < 10 states the continue condition of the loop. If i is less than 10 run the loop again.

      *i++* indicates how the controlling variable changes each iteration of the loop.

      you could just as easily count backwards

      for (var i = 10; i > 0; i--){}

      start at ten count down to 0;

      or count by 2s

      for (var i = 0; i < 10; i+=2){}

      (32 votes)

  • Shelby

    9 years agoPosted 9 years ago. Direct link to Shelby's post “During the Challenge; Lin...”

    During the Challenge; Lined Paper I kept getting the message 'It looks like you're initializing your counter variable before you create the for loop. As best practice, you should be doing that as the first expression in the for()." and I don't know exactly what you mean?

    (1 vote)

    • Blaze

      9 years agoPosted 9 years ago. Direct link to Blaze's post “Okay. Here's what the Oh ...”

      Review: Looping (article) | Looping | Khan Academy (31)

      Okay. Here's what the Oh Noes Guy means:
      If you use a for loop,

      for(var i = 0; i<n; i++){//code};
      , the counter variable is the
      var i = 0;
      part. If, for instance, I put:
      var i = 0;
      for(var i = 0; i<n; i++){//code};
      , then I defined the variable before the for loop... which is what the Oh Noes Guy doesn't like. Hope this answers your question!

      (23 votes)

  • 0like.a.bird0

    8 years agoPosted 8 years ago. Direct link to 0like.a.bird0's post “If you had a loop, and yo...”

    If you had a loop, and you put a loop inside that loop, would you be able to keep putting loops within the previous loop for ever?

    (5 votes)

    • Larry Serflaten

      8 years agoPosted 8 years ago. Direct link to Larry Serflaten's post “That's a bit like asking;...”

      Review: Looping (article) | Looping | Khan Academy (35)

      That's a bit like asking; If you have a number that you divide in half, and then you divide that result in half, can you keep dividing the halved result, in half - for ever?

      Its that "for ever' part that throws a wrench into the mix. So, no, you cannot continue to nest loops for ever At some point the computer you are using will use up all of its memory trying to hold the text portion of your program.

      Suffice it to say you can nest so many loops that it goes well beyond any number of loops you are reasonably likely to use.

      (11 votes)

  • Falak Fathima

    3 years agoPosted 3 years ago. Direct link to Falak Fathima's post “Can we use other variable...”

    Can we use other variables rather than i and j?

    (5 votes)

    • AD Baker

      3 years agoPosted 3 years ago. Direct link to AD Baker's post “You can use any variable ...”

      Review: Looping (article) | Looping | Khan Academy (39)

      You can use any variable you like. Using i and j is a convention among programmers.

      (10 votes)

  • Raven2Sky

    4 years agoPosted 4 years ago. Direct link to Raven2Sky's post “In my project I am trying...”

    In my project I am trying to loop the grass but there is only one picture.

    for (var grassX = 0; x < 400; x += 80){
    image(getImage("cute/GrassBlock"), grassX, 268);
    }

    (5 votes)

    • AD Baker

      4 years agoPosted 4 years ago. Direct link to AD Baker's post “Check the variable that y...”

      Check the variable that your loop is incrementing.

      (8 votes)

  • j.guliyeev

    8 years agoPosted 8 years ago. Direct link to j.guliyeev's post “Why a programmer needs to...”

    Why a programmer needs to repeat code? are there lots of cases in programming? I mean, repetitive coding things

    (2 votes)

    • Alex

      8 years agoPosted 8 years ago. Direct link to Alex's post “That's called "code redun...”

      Review: Looping (article) | Looping | Khan Academy (46)

      That's called "code redundancy". It's always there, since a lot of processes have similar sub-processes. You can make it better by spotting redundant parts, and getting them into a function that you just call. That way you minimize redundancy.
      But sometimes it's just too much, and in the strive to end redundancy you end up with overly complex code that is very hard to modify.
      For example, you made a game, and have some code that draws a human character. You realize that drawing the human shape is redundant for each human, so you take it out into a function and just call it. But then the game progresses and you want to have short humans, tall humans, giant humans, fat humans, mutated humans, zombie humans, etc. The function deals with so many special cases, and becomes so complex, that it would just be better to be repeated everywhere it's needed, with its specific modifications each time.... or you just need a more advanced architecture.

      (12 votes)

Review: Looping (article) | Looping | Khan Academy (2024)
Top Articles
Latest Posts
Article information

Author: Edwin Metz

Last Updated:

Views: 6446

Rating: 4.8 / 5 (58 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Edwin Metz

Birthday: 1997-04-16

Address: 51593 Leanne Light, Kuphalmouth, DE 50012-5183

Phone: +639107620957

Job: Corporate Banking Technician

Hobby: Reading, scrapbook, role-playing games, Fishing, Fishing, Scuba diving, Beekeeping

Introduction: My name is Edwin Metz, I am a fair, energetic, helpful, brave, outstanding, nice, helpful person who loves writing and wants to share my knowledge and understanding with you.