Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
1.20/5 (5 votes)
See more:
C#
int number;
int min_value = 100000;
for (int i = 0; i <= 4; i++)
{
   Console.WriteLine("Enter a number: ");
   number = Convert.ToInt16(Console.ReadLine());
   if (number < min_value)
      min_value = number;

   Console.WriteLine(number);
}
Posted
Updated 3-Dec-12 10:54am
v2
Comments
Matt T Heffron 3-Dec-12 13:13pm    
And your question is????
Things to think about...
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?
Sergey Alexandrovich Kryukov 3-Dec-12 13:15pm    
Exactly. Absolutely wrong.

But it explains why OP asks this question -- this is exactly the problem.
--SA
Sergey Alexandrovich Kryukov 3-Dec-12 13:22pm    
So, I answered, please see.
--SA
marchest_21 3-Dec-12 13:28pm    
thanks
Sergey Alexandrovich Kryukov 3-Dec-12 13:53pm    
You are welcome. It will work for you, for sure. Please consider accepting the answer formally (green button) -- thanks.
--SA

1 solution

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
 
Share this answer
 
v4
Comments
fjdiewornncalwe 3-Dec-12 16:55pm    
+5. Well described.
Sergey Alexandrovich Kryukov 3-Dec-12 17:12pm    
Thank you, Marcus.
--SA
BillW33 3-Dec-12 17:32pm    
Nice answer, +5
Sergey Alexandrovich Kryukov 3-Dec-12 18:00pm    
Thank you.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900