@nakov/

infinite-loop-with-break-csharp

C#

Calculating the first 5 results that match a condition, using infinite loop with `break` in C#

fork
loading
Files
  • main.cs
  • main.exe
main.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
using System;

class InfiniteLoopWithBreakExample {
    public static void Main() {
        int value = 0, min = 100000, count = 0;
        while (true)
        {
            value = 2 * value + 1;
            if (value > min)
            {
                Console.WriteLine(value);
                count++;
            }
            if (count == 5)
                break;
        }       
    }
}