Problem understanding blocks in C
I'm learning to program in C and I am doing an exercise that requires me to wri开发者_StackOverflowte a program to print an Isosceles triangle.
#include <stdio.h>
int main()
{
int col, row;
for(col = 0; col < 6; col++)
{
for(row = 0; row <= col; row++)
{
putchar('*');
putchar('\n');
}
}
}
This code prints the (*) on a line by itself 21 times. So after a few minutes of trial and error I removed the inner braces on the inner for loop.
#include <stdio.h>
int main()
{
int col, row;
for(col = 0; col < 6; col++)
{
for(row = 0; row <= col; row++)
putchar('*');
putchar('\n');
}
}
And I finally achieved my desired result.
SO MY QUESTION IS: How do the inner braces{} to the inner for loop affect how the results are printed? And why does the first set of code print * on a line by itself 21 times as well?
I looked through and through my book and can't find a reasonable solution to why the { } on the inner for loop is affecting the output.
Your working solution is equivalent to
for(col = 0; col < 6; col++)
{
for(row = 0; row <= col; row++)
{
putchar('*');
}
putchar('\n');
}
In C, indentation has nothing to do with grouping of statements. Braces group statements together. In your original, not-working, code, the braces following your inner loop make all code inside the braces run each loop iteration. In the sample above, you can see that the braces include only the appropriate expressions to run for each iteration of the for loop.
As others might note: the indentation doesn't tell the compiler what to do, but you should indent so that visually you can interpret your code.
A for
(or a while
) loop executes the code between { }
.
But, there is a short form that doesn't use the { }
, for example:
for (i = 0; i < 10; i++)
function();
When for
doesn't find the { }
it interprets that only the next statement is going to be part of the loop body.
What you achieved by trial and error is the equivalent of:
for(col = 0; col < 6; col++) {
for(row = 0; row <= col; row++) {
putchar('*');
}
putchar('\n');
}
Note how the putchar('\n')
is outside the inner for loop.
The best way to look at it is to see that braces turn a group of statements into a single statement, as far as constructs outside of the braces are concerned. A for
loop executes the statement immediately following it, which in your first attempt is a block containing both print statements. Your indentation is misleading and would better be arranged as follows:
for(col = 0; col < 6; col++)
{
for(row = 0; row <= col; row++)
{
putchar('*');
putchar('\n');
}
}
and
for(col = 0; col < 6; col++)
{
for(row = 0; row <= col; row++)
putchar('*');
putchar('\n');
}
The indentation is used to indicate that consecutive lines at a given indentation level are in the same block, usually a block "owned" by the statement immediately preceding the block. If you want to use braces to make things even more clear (many coding styles suggest this), try:
for(col = 0; col < 6; col++)
{
for(row = 0; row <= col; row++)
{
putchar('*');
}
putchar('\n');
}
When you removed the pair of inner loop braces, you made the scope of that inner for-loop just the single statement that prints "*". Then the newline character "\n" only gets printed once instead of once for each time the asterisk was printed.
The curly braces in C group a "block" of statements together so that they execute together.
精彩评论