For a loop skipping a line

I am super new to C # and programming in general. I am trying to do some exercises online to become familiar with the basics of the language before I start learning next month. I wrote a super-simple program, and I just can’t understand why she does what she does, and I could not find the answer anywhere. Here is the code.

        int i, j, rows;

        Console.Write("\n\n");
        Console.Write("Display the pattern like right angle triangle which repeat a number in a row:\n");
        Console.Write("-------------------------------------------------------------------------------");
        Console.Write("\n\n");

        Console.Write("Input number of rows : ");
        rows = Convert.ToInt32(Console.ReadLine());
        for (i = 1; i <= rows; i++)
        {
            for (j = 1; j <= i; j++)
                Console.Write("{0}", i);
                Console.Write("\n");

        }

All this program should do is a simple pyramid with the same number.

My question is that in the second cycle he writes i, but then reevaluates j++and j<=instead of recording \nuntil the end of the last run of the cycle. I do not understand why? The program works, but I do not understand why. Shouldn't a for loop always do everything in it unless you break it?

, !

+4
3

for . {} for . , :

for (j = 1; j <= i; j++)
{
    Console.Write("{0}", i);
    Console.Write("\n");
}

{} , . for , .

for (i = 1; i <= rows; i++)
for (j = 1; j <= i; j++)
{
    Console.Write("{0}", i);
    Console.Write("\n");
}

, while .

, , for, , for.

for:

for (int i = 1; i <= rows; ++i)

for , "i" , for.

for , . , for, for:

int i = 1;
for (; i<= rows; ++i)

:

for (;;) // a perfectly valid for loop that will loop forever.

:

for (int i = 0, j = 0; i < 5; ++i, ++j)
+3

, . , . , {} .

, , .

for (j = 1; j <= i; j++)
{
   Console.Write("{0}", i);
   Console.Write("\n");
}

{} . .

+4

, for Console.Write :

for (j = 1; j <= i; j++)
    Console.Write("{0}", i); //inner for loop scope starts and ends on this statement

for. , , for, for.

OTER for , :

for (i = 1; i <= rows; i++)
{//outer for loop scope starts here
    for (j = 1; j <= i; j++)
        Console.Write("{0}", i);
    Console.Write("\n");
}//outer for loop scope ends here

, for Console.Write("\n");.

Typically, if you have only one statement to execute inside the for loop, you are backing it down by the tab, as shown in the code snippet.

+2
source

Source: https://habr.com/ru/post/1664738/


All Articles