Hello,

Why break function doesn't work at that code ?:

double Number = 0, counter = 0; // declare here.
            for (int i = 1; i <= 10; i++)
            {
                //Entering ten numbers
                Console.Write("Enter the number:");
                Number += double.Parse(Console.ReadLine()); //keep incrementing number

                /*If user write number 0 in console before he conclude the 10th number,
                program will calculate just average of entered numbers*/
                if (Number == 0)
                    break;
                counter++; // Monitor the number of loops in case they break out early.
            }
            double avg = Number / counter;

            Console.WriteLine("An average of entered numbers is {0}", avg);
            Console.ReadLine();

Any help is welcome.

bisiii

Edited 5 Years Ago by bisiii: n/a

Where is Stevilo changed? I don't see any calls to any methods in the loop, so even if it is a global variable, without being changed, if it is not == 0 to start with, it will never be changed. Perhaps I am missing something, but that is hard to see without more of your code.

I edited Stevilo (it means Number), but anyway it works fine. I get an average of numbers, just break function doesn't work.
So I need to do more code that break function will work ?

Edited 5 Years Ago by bisiii: n/a

Did you see what happens if you enter 0 as the first number? From what I see, the code you gave will never break as long as the first number is not 0. The reason is that you are using

Number += double.Parse(Console.ReadLine()); //keep incrementing number
 
                /*If user write number 0 in console before he conclude the 10th number,
                program will calculate just average of entered numbers*/
                if (Number == 0)
                    break;

to make sure that the total value of all entered numbers, stored in Number, does not == 0. What you need to do is to either check the entered value first or create a new variable for it, so something like

double value =  double.Parse(Console.ReadLine());

                if ( value == 0)
                    break;
                else
                    Number += value;

Give that a try and see if it works.

Edited 5 Years Ago by croker10: n/a

At each iteration in your for loop you are adding a number to Number. Afterwards you test if it is zero to break the loop. This will not always happen.

This question has already been answered. Start a new discussion instead.