0

Hi could someone please explain to me how the following loop prints 45. I guess I'm not quite understanding how these nested loops work:

#include <stdio.h>

int main() {
    int x, y, n;
    n = 0;
    for (x = 0; x < 10; x++)
        for (y = 0; y < x; y++)
            n++;

    printf("%d\n", n);
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189
Ethan Itovitch
  • 117
  • 1
  • 1
  • 6
  • 2
    Have you tried to step through that code with a [debugger](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) or _execute_ it on paper (try with a small x)? – litelite Oct 18 '17 at 19:59
  • Look at the condition of the inner `for` loop, look *up to how much* it counts. You could also try adding debug `printf` statements to show the evolution of the values of `x, y and n` (but don't forget to add `{...}`to enclose statements in appropriate blocks). – AntonH Oct 18 '17 at 20:00
  • Not really sure what you're expecting from us, stepping through would be easiest failing that, just dry run it. – George Oct 18 '17 at 20:00
  • 1
    What part is giving you trouble. As a brief explanation, the inner loop adds 1 to `n` for every value of `y` between 0 and `x` (including 0 but not `x`), or in total adds `x` to `n`. The outer loop repeats that for every value of `x` between 0 and 10. Thus, in total, it adds 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 = 45 to `n`. Is part of that unclear, or is there something I didn’t address? – Daniel H Oct 18 '17 at 20:01
  • @DanielH: your comment would make a clear and concise answer. – chqrlie Oct 18 '17 at 20:11
  • 1
    @chqrlie Yes, but dale_dale12’s is better, and I had expected the question would be closed before I finished writing the answer. – Daniel H Oct 18 '17 at 20:16
  • That is an implementation of sum(n), where n=10. That is n*(n-1)/2. When n=10, sum(n)=45. – Marius Bancila Oct 18 '17 at 20:20

1 Answers1

6

Try expanding the first loop out help yourself understand. If you did that you would get code like this:

#include <stdio.h>  
int main() { 
    int x, y, n;
    n = 0;

    /* x=0 */
    for (y=0; y<0; y++)
        n++; /* doesn't get hit n=0 */

    /* x=1 */
    for (y=0; y<1; y++)
        n++; /* gets hit once n=1 */

    /* x=2 */
    for (y=0; y<2; y++)
        n++; /* gets hit twice n=3 */

    /* x=3 */
    for (y=0; y<3; y++)
        n++; /* gets hit three times n=6 */

    /* x=4 */
    for (y=0; y<4; y++)
        n++; /* gets hit four times n=10 */

    /* x=5 */
    for (y=0; y<5; y++)
        n++; /* gets hit five times n=15 */

    /* x=6 */
    for (y=0; y<6; y++)
        n++; /* gets hit six times n=21 */

    /* x=7 */
    for (y=0; y<7; y++)
        n++; /* gets hit seven times n=28 */

    /* x=8 */
    for (y=0; y<8; y++)
        n++; /* gets hit eight times n=36 */

    /* x=9 */
    for (y=0; y<9; y++)
        n++; /* gets hit nine times n=45 */

    /* x=10 exit loop */

    printf ("%d\n", n); /* prints 45 */
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189
Dale
  • 1,911
  • 11
  • 18