I'm super new to c# and programming in general. I'm trying to do some exercises online to familiarize myself with the basics of the language before I start school next month. I've written a super simple program and I just can't understand why it's doing what it's doing and I haven't been able to find an answer anywhere. Here's 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 is suppose to do is a simple pyramid with the same number.
My question is that in the second for loop it writes i
but then it re-evaluates j++
and j<=
instead of writing \n
until the end of the last run of the loop. I don't understand why? The program works but I am not understanding why. Isn't the for loop always suppose to execute everything in it unless you break it?
thanks and sorry for the very novice question but its driving me nuts!
In programming every statement have a scope block in which it run. By default for loop have only one statement scope that just comes after it. If we wanted to run more then one statement in for loop scope then we use curly braces {} to define the code block.
in your case you need to use curly braces like this to run both statements.
for (j = 1; j <= i; j++)
{
Console.Write("{0}", i);
Console.Write("\n");
}
its best programming practices to used braces {} in all you conditional and looping constructs. It make easy to read and understand the code.
A for loop will execute everything in its scope. Without using { } the scope of a for loop is the next line after it. To execute more than one command you need to put them in curly braces like so:
for (j = 1; j <= i; j++)
{
Console.Write("{0}", i);
Console.Write("\n");
}
It's generally best practice to always have the { } for readability and to easily update the loop if needed. Though you could write your for loop like so and it would be perfectly readable.
for (i = 1; i <= rows; i++)
for (j = 1; j <= i; j++)
{
Console.Write("{0}", i);
Console.Write("\n");
}
The above code will do the same thing keeping the second for loop in the scope of the first.
As an aside, I noticed you declared your variables outside of the for loop, being new to programming you may not be aware of certain features of the for loop.
You can declare your iterator right in the for loop:
for (int i = 1; i <= rows; ++i)
In most cases it's better to declare your iterator in the for loop like this as it gives 'i' the same scope as the for loop.
Another neat feature of the for loop is that all the options between the semi-colons are actually optional. So for instance, if you declared your iterator outside the for loop you can omit it in the for loop:
int i = 1;
for (; i<= rows; ++i)
You can omit any part or all of it even:
for (;;) // a perfectly valid for loop that will loop forever.
You can even have multiple iterators:
for (int i = 0, j = 0; i < 5; ++i, ++j)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With