Why is this second loop not initialized? And why does he print five stars?

This program produces this output:

*******
 *****
  ***
   *

      

Here's the code:

#include  <stdio.h>
int main(void)
{
 int i, j;
 for (i = 1; i <= 5; i++)
 {
  for (j = 1; j < i;j++)
   printf(" ");
  for (; j <= 8 - i;j++)
   printf("*");
  printf("\n");
 }
 return 0;
}

      

What is the meaning for (; j <= 8 - i;j++)

? There is no initialization step, and I also don't understand why there are only five * in the second line.

+3


source to share


7 replies


For loops, you don't really need to have an initialization statement. Many loops prefer to include one, but this is not required. Therefore, you can think of this loop by saying "we don't need to do anything to initialize".

As for why there are five stars on the second line, take a look at this piece of code:

for (j = 1; j < i;j++)
 printf(" ");
for (; j <= 8 - i;j++)
 printf("*");

      



On the second iteration, it i

is 2. When the first loop is run, it will print out one whitespace character. The loop stops running when it j < i

stops being true, so when it finishes running, the value j

will be 2. So the second loop will run for j =

2, 3, 4, 5, 6, stops when j = 7

. This is why you see five stars.

Hope this helps!

+4


source


What does it mean for (; j <= 8 - i; j ++)?



This is to take the last (known) value of j used in the program

+1


source


 for (; j <= 8 - i;j++)
 printf("*");

      

There is no need for initialization here j

as the value j

comes from the previous loop.

The first case is i=1

and j=1

, but the condition in the loop is false and j

increases to 2

. And this value is included in the innermost cycle that continues until j=7

.

0


source


Okay, j is initialized in the outer most of the blocks relative to the for loops, so j is now accessible from the entire for loop (or any other loop).

it uses the last modified (incremented) value for the outermost loop.

I think you can figure it out now.

just compute it using copy and create a matrix for the variables of the for loop.

it will improve your programming skills for sure.

Happy coding.

you can try this:

options for loop in C

0


source


This loop uses the variable j

from the for loop directly above it. Each iteration is shorter by 2; one from the beginning and one from the end. This way the center text is maintained on screen.

So the first iteration in i=1

will not print any " "

. Then it will print 7 *

, from j

values ​​1-7.

The second iteration in i=2

will print one " "

, followed by 5 *, from a j

value of 2-6.

Each iteration subsequently follows this pattern.

0


source


#include  <stdio.h>
int main(void)
{
 int i, j;
 for (i = 1; i <= 5; i++) **1**
 {
  for (j = 1; j < i;j++) **2**
   printf(" ");
  for (; j <= 8 - i;j++) **3**
   printf("*");
  printf("\n");
 }
 return 0;
}

      

You will find that the variable j does not need to be initialized in loop 3 because it was initialized in loop 2 and it is in the local block scope (j exists in loop 1 and does not need to be initialized after loop 2).

Regarding your second request, consider the second iteration of loop 1, where i = 2. When loop2 finishes at this point, j = 2. Now consider loop3, where j is 2 (as stated earlier), and it runs until until j is less than or equal to 8-i (8-i = 8-2 = 6). So it prints a star for j = 2,3,4,5,6 = 5 stars.

0


source


Replace

printf("*");

      

printf("%d", j) 

      

and see how the value of j changes. This might explain how it works.

0


source







All Articles