Beginner C# Loops Exercise

In this exercise solution about having numbers entered and separated by commas and finding the max, I don’t understand what “// Assume the first number is the max” means. At first I thought it meant that the first number entered in the list had to be the max but the finished program does not care where the max was entered. I also don’t understand what the line under it is doing. Any help is appreciated.

        Console.Write("Enter comma separated numbers: ");
        var input = Console.ReadLine();

        var numbers = input.Split(',');

        **// Assume the first number is the max **

** var max = Convert.ToInt32(numbers[0]);**

        foreach (var str in numbers)
        {
            var number = Convert.ToInt32(str);
            if (number > max)
                max = number;
        }

        Console.WriteLine("Max is " + max);

In the first iteration of the loop max needs to have a value to make the comparison (number > max) work. So for the first iteration we assume that the first number is the current maximum.

1 Like

Thank you! It seems so obvious now!!!