I'm not sure how this code is compiled (using visual studio code just fyi)
int i,j;
for(i=0; i<=6; i++) {
for (j=1; j<=7-i; j++) {
Console.Write("*");
}
Console.Write("\n");
}
My question is when the code is beginning to get compiled, the first for loop is tested and it will be true, then the nested for loop is then tested and it will be true so "Console.Write(" * "); is written. But then from here I do not understand what happens or why it happens. Does the compiler then run Console.Write(" \\n "); or...
If you don't understand a for-loop you can always reduce it to simpler statements:
for ( init ; condition; increment )
body;
is just a shorter way of writing
init;
while (condition)
{
body;
increment;
}
So your loops
for(i=0; i<=6; i++) {
for (j=1; j<=7-i; j++) {
Console.Write("*");
}
Console.Write("\n");
}
is just a short way of writing
i = 0;
while (i <= 6)
{
j = 1;
while (j <= 7 - i)
{
Console.Write("*");
j++;
}
Console.Write("\n");
i++;
}
Is that more clear?
If that's still not clear, you can lower it further.
while(condition)
body
is just a short way to write
START:
if (!condition)
goto END;
body;
goto START;
END:
So your loops are just:
i = 0;
START_1:
if (!(i <= 6))
goto END_1;
j = 1;
START_2:
if (!(j <= 7 - i))
goto END_2;
Console.Write("*");
j++;
goto START_2;
END_2:
Console.Write("\n");
i++;
goto START_1;
END_1:
Is that now clear?
variables change like this.
i = 0 : j changes from 0 to 7 ( 7- i, but i = 0)
i = 1 : j changes from 0 to 6 (7 - i, i = 1)
.
.
.
.
i = 6: j changes from 1 to 1 (7 - i, i = 6)
in each i-loop you're printing j-loop and a new line character.
|j-loop|i-loop|
|******|'\n' |
So, you will get the output,
*******
******
*****
****
***
**
*
I will not show you the output simply because your question is not what the output is but how the for
loop works.
Let's do this step by step. Here is some pseudo code:
for ( init; condition; increment )
{
Console.Write("*");
}
init
is executed first and it is only done once when the loop is encountered. That's right only once for the for
loop. condition
is evaluated. If it is true the body is executed. So Console.Write("*");
is executed. increment
is executed. condition
is evaluated again. If it is true the body is executed. So Console.Write("*");
is executed. Steps 3 and 4 are done until the condition
is false. Once it is false the loop exits and the following line of code is executed.
In your question you have a loop and if the condition is true it starts another loop. This loop starts at 1 and if i
is 0, it is subtracted from 7, 7 - 0 is 7, and since j
is 0 and smaller than 7, it performs the body and prints a *. It keeps doing steps 3 and 4 and prints a * seven times. Then the condition is false so it jumps outside the inner loop and prints a new line.
Then step 3 and 4 are executed for the outer loop.
It keeps doing that until the condition in the outer loop evaluates to false.
i: 0
, j: 1 to 7
print *
, then print \\n
i: 1
, j: 1 to 6
print *
, then print \\n
and so on and on
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.