Nested for loops!

I'm having troubles understanding how this program works.. I didn't make this program, it was given as study material for an upcoming test! I was hoping that someone could explain what happens after the first if statement..
Thanks you!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <cstdio>

int main()
{
   int r = 5;
   int c = 5;
   for (int row = 0; row < r; row++) 
   {
      for (int col = 0; col < c; col++) 
      {
          if ((row == 0) || (row == r-1))
          {
             printf("-");
          } 
          else if ((col == 0) || (col == c-1))
          {
             printf("|");
          }
          else if (col == row)
          {
             printf("*");
          } 
          else
          {
             printf(" ");
          }
       }
       printf("\n");
   }  
   return 0;
}
Try running it to understand it. Read each line in the for loop and imagine what number is it evaluating and it should be pretty easy.
Last edited on
well statements after else if are only evaluated if the statement before it is not true. That's a pretty big hint.
For a while I thought that after the first IF statement you would move onto the ELSE IF statement.. Which is where I was having troubles.. But after that, I figured it out! Thanks
Topic archived. No new replies allowed.