C - Nesting for loops

Introduction

You can stick inside a for loop is another for loop.

Demo

#include <stdio.h> 

int main()//from   w ww .  j a  va 2s.  com
{
  int alpha, code;

  for (alpha = 'A'; alpha <= 'G'; alpha = alpha + 1)
  {
    for (code = 1; code <= 7; code = code + 1)
    {
      printf("%c%d\t", alpha, code);
    }
    putchar('\n');      /* end a line of text */ 
  }
  return(0);
}

Result

The outer for loop counts from letters A to G. It contains the second, inner for loop and a putchar() function.

That function helps organize the output into rows by spitting out a newline after each row is displayed.

The printf() function displays the program's output, specifying the outer loop value, alpha, and the inner loop value, code.

The \t escape sequence separates the output.

Related Topic