-6

I don't understand why this loop starts at 8 and 18 when I output it, but when I try "i < 10 and j < 10", it starts at 2 and 2. Can someone explain this to me?

class Program
{
    static void Main(string[] args)
    {
        for (int i = 2; i < 100; i++)
        {
            for (int j = 2; j < 100; j++)
            {
                Console.WriteLine("i = {0} and j = {1}", i, j);
            }                 
        }
    }
}

Screenshot of my issue:

Rand Random
  • 7,300
  • 10
  • 40
  • 88
Voidsower
  • 1
  • 2
  • These loops "start at" 2 and 2. It's not clear what problem you're trying to describe. – David Apr 30 '18 at 14:47
  • I don't observe the same behaviour with this code, running it starts the loops at 2 and 2 – Expired Data Apr 30 '18 at 14:47
  • 3
    [I can't reproduce this...](http://rextester.com/YIXJ17802) – Zohar Peled Apr 30 '18 at 14:48
  • 2
    Can you clarify your question of 'when i output it': if you have a second, unworking version of the code, please share it? – Jmons Apr 30 '18 at 14:48
  • 1
    I also [could not replicate this issue](https://repl.it/repls/ThornyHummingDistributionsoftware). – Joe Clay Apr 30 '18 at 14:48
  • For those who can't reproduce it, I'm using Visual Studio, and my console output starts at 8 and 18 when using < 100. I still can't understand why. – Voidsower Apr 30 '18 at 14:51
  • @Voidsower: I'm also using Visual Studio, copied and pasted the exact code from this question, and when I step through in my debugger the very first line of output shows the values 2 and 2. You must be doing something else wrong. Loops are not broken in C#. These values start at 2 exactly as you've defined them. – David Apr 30 '18 at 14:54
  • 1
    Maybe check with someone and try to answer his question - `How many fingers am I holding up?` :) – Rand Random Apr 30 '18 at 14:54
  • @RandRandom Before trying to act smart, I can send you a screen shot with the output. For some reason my console output is hiding the results. – Voidsower Apr 30 '18 at 14:57
  • @Voidsower Please post the screenshot with the output in your question – Jimenemex Apr 30 '18 at 14:58
  • Didnt try to act smart, thought it was funny sorry if it offended you. Yes, pls provide a screenshot than I can see the scrollbar thats hiding your output. (yes, now I acted smart) – Rand Random Apr 30 '18 at 14:58
  • No chance the window just doesnt scroll back that far? – BugFinder Apr 30 '18 at 14:58
  • Maybe that can help - https://stackoverflow.com/questions/1740876/more-lines-in-command-window - you can also try to redirect the console output to file https://stackoverflow.com/questions/503846/how-do-i-echo-and-send-console-output-to-a-file-in-a-bat-script – Rand Random Apr 30 '18 at 15:00
  • @RandRandom Just added a Screenshot, also showing to code for you to see that I'm not kidding. – Voidsower Apr 30 '18 at 15:04

2 Answers2

1

It is because you exceed the number of lines that your console is set to display at once.

The property is called Console.BufferHeight.

Try writing Console.WriteLine(Console.BufferHeight); to see the number of lines your console is set to display at once.

You can change it simply by writing setting it to your desired value, e.g. Console.BufferHeight = 20000; or changing the settings of the console by accessing the properties in the manner, that @Greg elaborated on.

thesystem
  • 554
  • 1
  • 10
  • 31
0

Are you sure that it isn't starting at i = 2 and j = 2? You are running this loop so many times that your console output may not show that it starts at 2,2, even though it does. Try setting a breakpoint in the second for loop, I think you'll see it is starting at 2, 2. That's because the buffer size is too small.

Here's what you can do:

  • Run your project.
  • Right-click on the title bar-> Properties-> Layout.
  • Set your height to something really large.

You can refer to this question for more.

eye_am_groot
  • 682
  • 6
  • 19
  • While I think you might be correct about the source of their confusion, this should probably be a comment rather than an answer. – Joe Clay Apr 30 '18 at 14:49
  • I do not have the reputation to comment. – eye_am_groot Apr 30 '18 at 14:50
  • You are right, if I set a break point, it shows 2 and 2. Any idea why is this happening? I'm using the latest version of Visual Studio community. Is it normal or just a bug? Thanks a lot. – Voidsower Apr 30 '18 at 14:53
  • The buffer size is too small. Run your project. Right click on the title bar->Properties->Layout. Set your height to something really large. This may help. (See: https://stackoverflow.com/questions/34160977/is-there-a-limit-on-the-output-of-console-window) – eye_am_groot Apr 30 '18 at 15:00