For loops

For some reason, anything after my for loops don't happen, what do I do?

Answers

  • edited October 2017 Answer ✓

    Hi @TeaPod! Could you please post a bit of your code so that we can find any bugs in it? Thanks!

    But for starters, you should check your restrictions on how your for-loop should run. for example:

    for (int i = 0; i <= -5; i--){
        println(i); // I want it to print number from 0 to -5
    }
    

    The above code will not run once because the conditions i <= -5 will never be met (the starting case,0, is greater than -5) and so the code in the for loop won't be executed

    Instead I should do this

    for (int i = 0; i >= -5; i--){
        println(i) // it will work now
    }
    
  • edited October 2017

    @Anix55 Here are the two relevant parts of the code, I hope this helps

    float [] firstonex = new float[50];
    float [] firstoney = new float[50];
    float [] seconex = new float[50];
    float [] seconey = new float[50];
    float [] thirdonex = new float[50];
    float [] thirdoney = new float[50];`
    
    void setup(){
      for (int number=0; number<50; firstonex[number]++, firstoney[number]++) {
        fill(0, 0, 255);
        ellipse(firstonex[number], firstoney[number], 10, 10);
      }
      for (int number=0; number<=50; seconex[number] = seconex[number]+5, seconey[number] = seconey[number]) {
        seconey[1]=500;
        fill(255, 0, 0);
        ellipse(seconex[number], seconey[number], 10, 10);
      }
    }
    
  • edited October 2017 Answer ✓

    @TeaPod I see the problem, you have created and INFINITE loop.

    for (int number = 0; number < 50; firstonex[number]++, firstoney[number]++){
        // code sample
    }
    

    Since you never increment number the conditions number < 50 will be satisfied forever and the loop will not end. Try incrementing the number variable and see if it helps. (idk what your code is supposed to do but incrementing number variable will solve the infinite loop problem).

    for (int number = 0; number < 50; number++, firstonex[number]++, firstoney[number]++){
        // code sample
    }
    

    I hope this helps.

    PS instead of changing the values in firstonex etc. in the bracket, why not do it in the for loop?

    for (int number = 0; number < 50; number++){
        firstonex[number]++;
        firstoney[number]++;
        // code sample
    }
    
  • @anix55 It worked! thank you so much

  • Answer ✓

    @TeaPod I'm glad I could help. be sure to mark your question as answered (;

Sign In or Register to comment.