Sergiu Botezatu
Sergiu Botezatu

Reputation: 1

While loop breaks unintentionally

I am in my second week of C# training, so I am pretty new to programming. I have to make a program that returns the smallest integer out of a series of random integer inputs. Once the input = 0, the program should break out of the loop. I am only allowed to use while and for loops. For some reason my program breaks out of loop after the second input and it looks like it doesn't even care if there is a "0" or not. Could you please see where I went wrong? I have been busting my head off with this. Sorry if this question has already been posted by somebody else but I did not find an answer to it anywhere.

PS: The zero input should be taken into account for the comparison. So this is what I've got so far:

 class Program
 {
     static void Main()
     {
        int i = 0;
        int input = Int32.Parse(Console.ReadLine());
        int min = default;

        while (input != 0)
        {
            Console.ReadLine();
            if (i == 0)
            {
                min = input;
                break;
            }

            if (input < min && i !=0)
            {
                input = Convert.ToInt32(Console.ReadLine());
                min = input;
            } 
            i++;
        }
        Console.WriteLine(min);
    }

Upvotes: 0

Views: 375

Answers (2)

Hardood
Hardood

Reputation: 523

Try This:

       int input;
       Console.Write("Enter number:");
       input = Int32.Parse(Console.ReadLine());
       int min = input;

       while(true)
       {
           if (input == 0)
               break;

           if (min > input)
               min = input;

           Console.Write("Enter number:");
           input = Int32.Parse(Console.ReadLine());
       }

       Console.WriteLine(min);
       Console.ReadKey();

I hope it helps.

Upvotes: 0

oRole
oRole

Reputation: 1346

First of all you will want to re-read the documentation for for- and while-loops. There are several useful pages out there.. e.g. for / while.

Problem

The reason why your loop breaks is that you initialize i with 0.

int i = 0; 

Inside your loop you are using the if-statment to break the loop if the condition "i is 0" is met.

if (i == 0)
{
    min = input;
    break;
}

The input that the user has to provide each iteration of the loop is ignored by your program as you are never storing this kind of information to any variable.

while (input != 0)
{
    Console.ReadLine(); 
    // ...
}

Possible Solution

As a beginner it is helpful to tackle tasks step by step. Try to write down each of this steps to define a simple algorithm. As there are many solutions to this problem one possible way could be:

  1. Declare minimum value + assign max value to it
  2. Use a while loop and loop till a specific condition is matched
  3. Read user-input and try converting it to an integer
  4. Check whether the value is 0 or not
    • 4.1. If the value is 0, go to step 8
    • 4.2. If the value is not 0, go to step 5
  5. Check whether the value is smaller than the current minimum value
    • 5.1. If the value is smaller, go to step 6
    • 5.2. If the value is not smaller, go back to step 3
  6. Set the new minimum
  7. Go back to step 3
  8. Break the loop
  9. End program

A program that handles the above steps could look like:

using System;

namespace FindMinimum
{
    public class Program
    {
        static void Main(string[] args)
        {
            // Declare minimum value + assign initial value
            int minValue = int.MaxValue;

            // Loop until something else breaks out
            while (true)
            {
                Console.WriteLine("Please insert any number...");

                // Read io and try to parse it to int
                bool parseOk = int.TryParse(Console.ReadLine(), out int num);

                // If the user did not provide any number, let him retry
                if (!parseOk)
                {
                    Console.WriteLine("Incorrect input. Please insert numbers only.");
                    continue;
                }

                // If the user typed in a valid number and that number is zero, break out of the loop
                if (parseOk && num == 0)
                {
                    break;
                }

                // If the user typed in a valid number and that number is smaller than the minimum-value, set the new minimum
                if (parseOk && num < minValue)
                {
                    minValue = num;
                }
            }

            // Print the result to the console
            Console.WriteLine($"Minimum value: {minValue}.");

            // Keep console open
            Console.ReadLine();
        }
    }
}

Upvotes: 3

Related Questions