OneBite.Dev - Coding blog in a bite size

implement a three nested loop in C

Code snippet on how to implement a three nested loop in C

  int i, j, k;
  for (i = 0; i < 3; i++) {
      for (j = 0; j < 5; j++) {
          for (k = 0; k < 3; k++) {
              printf("Hello\n");
          }
      }
  }

This code uses three nested for-loops to print “Hello” three times. The outermost for-loop is the first one, which starts at 0 and goes until the number 3, increasing by 1 each time. The inner two for-loops (the second and third) will be executed each time the outer loop runs. The second for-loop starts at 0 and goes until the number 5, increasing by 1 each time, and the third for-loop starts at 0 and goes until the number 3, also increasing by 1 each time. The third for-loop prints “Hello” every time it runs, and since it is nested within the outer two for-loops, it will run three times each time the outer two for-loops run. Therefore, the innermost for-loop will run fifteen times, resulting in “Hello” being printed fifteen times.

c