Where Is My Error In This For Statement

For fun, I am trying to take a integer and display it in boxes ala a speedometer: each digit in a frame. I am using a For statement to extract each digit. The results are that it appears to go thru the loop the correct number of times but only the results of the first pass thru the loop are printed. The code is:

int n = 567; int p,j; int xyz = 6; println ("starting with:" + n + " for " + xyz + " digits"); for (p =0; p < xyz; p = p + 1); { j = n -((n/10)*10); n = n/10; println (p +"=----------->"+j+"<------ n = " + n); }

Tagged:

Answers

  • edited November 2015

    You have a spare ; immediately after the for ()

    int n = 567;
    int p,j;
    int xyz = 6;
    println ("starting with:" + n  + " for " + xyz + " digits");
    for (p =0; p < xyz; p = p + 1);
    {
      j = n -((n/10)*10);
      n = n/10;
      println (p +"=----------->"+j+"<------ n = " + n);
    }
    

    end of line 5.

    meaningful variable names are good, calling things p, n and j isn't.

  • I see the extra ; but do not understand why that would cause the problem.

    Thanks for the modulo input. I will use it.

  • _vk_vk
    edited November 2015

    It ends the statement you end up with 2 separated statments

    1-

    for (p =0; p < xyz; p = p + 1)/**do nothing**/;
    

    2-

     {// just a block not related with previous line, runs once
      j = n -((n/10)*10);
      n = n/10;
      println (p +"=----------->"+j+"<------ n = " + n);
    }
    
  • edited November 2015

    It ends the statement you end up with 2 separated statments

    more explanation

    basically the for-loop command defines its properties inside () and expects a command right after the (...) part.

    This command closes with a ; like a normal line.

    Now when you put a ; after the (...) part, we say: execute all between (...) and ; which is unfortunately nothing.

Sign In or Register to comment.