Guys, tell me what's wrong in the code?

According to the idea, he should output line by line to the console but they appear simultaneously

bool b = true; int t = 0; int t2 = 0; while (b) { t = t + 1; if (t == 10000) { t = 0; t2 = t2 + 1; } if (t2 == 10 && t == 0) { Console.WriteLine("11111"); } if (t2 == 20 && t == 0) { Console.WriteLine("1111"); } if (t2 == 30 && t == 0) { Console.WriteLine("111"); } if (t2 == 40 && t == 0) { Console.WriteLine("11"); } if (t2 == 50 && t == 0) { Console.WriteLine("11"); } if (t2 == 60 && t == 0) { Console.WriteLine("1"); } } 
  • one
    Probably not so that you are trying to implement a delay cycle, but the processor is already much faster than in those dark ages, when the delay was made cycles. - eigenein
  • PPC logic. She recalled this: How can you find out yesterday's date using this method? Very simple! void get_yesterday_date () {sleep (86400); // 60 * 60 * 24} - AseN
  • @ Denis Akulov, If you are given a comprehensive answer, mark it as correct (click on the check mark next to the selected answer). - Deleted

1 answer 1

They appear one after another, but the program runs quickly, and you do not have time to notice. Your program displays a line every hundred thousand idle cycles, this is a fairly short time on modern machines (especially if they are optimized).

Try inserting Thread.Sleep(1000) for the delay;

By the way, delays are considered cycles, as @eigenein correctly writes, an obsolete method somewhere at least since the days of DOS. They are unreliable, unpredictable, can be removed by a good optimizer, and load the processor with unnecessary pointless work.

  • Huge thanks to VladD! - Denis Akulov
  • @ Denis Akulov: please! - VladD
  • @VladD think there's nothing even to notice. A new Writln request arrives before the old one is completed. - alexlz
  • @alexlz: it is, output to the console in Windows is terribly slow. - VladD