13,260,108 members (48,148 online)
Rate this:
See more:
```int number;
int min_value = 100000;
for (int i = 0; i <= 4; i++)
{
Console.WriteLine("Enter a number: ");
if (number < min_value)
min_value = number;

Console.WriteLine(number);
}```
Posted 3-Dec-12 8:07am
Updated 3-Dec-12 11:54am
v2
Matt T Heffron 3-Dec-12 13:13pm

What happens when you enter "100001" (so it is > min_value)? What do you think SHOULD happen?
What happens when you enter "abc"? Again, what do you think SHOULD happen?

Exactly. Absolutely wrong.

But it explains why OP asks this question -- this is exactly the problem.
--SA

--SA
marchest_21 3-Dec-12 13:28pm

thanks

You are welcome. It will work for you, for sure. Please consider accepting the answer formally (green button) -- thanks.
--SA

Rate this:

Solution 2

Please see comments to the questions — by Matt T Heffron and mine. This is the problem.

And here is the solution: instead of pointless "100000" use `int.MaxValue` (and, to find the maximum, start with `int.MinValue`):
http://msdn.microsoft.com/en-us/library/system.int32.minvalue.aspx[^],
http://msdn.microsoft.com/en-us/library/system.int32.maxvalue.aspx[^].

All other integer types have similar property.

With floating-point types, things are way more interesting. You should use `NegativeInfinity` and `PositiveInfinity`:
http://msdn.microsoft.com/en-us/library/system.double.negativeinfinity.aspx[^],
http://msdn.microsoft.com/en-us/library/system.double.positiveinfinity.aspx[^].

The fundamentally important and amazing feature is: infinity values correctly compare with non-infinite values with operators '>', '<', '>=', '<=' and '=='!

—SA
v4
Marcus Kramer 3-Dec-12 16:55pm

+5. Well described.

Thank you, Marcus.
--SA
CIDev 3-Dec-12 17:32pm

Thank you.
--SA

Top Experts
Last 24hrsThis month
 CPallini 95 Peter Leow 90 F-ES Sitecore 90 Jochen Arndt 85 phil.o 65
 OriginalGriff 4,324 Karthik Bangalore 2,276 ppolymorphe 1,794 Dave Kreskowiak 1,624 CPallini 1,545