Saturday
Oct252008
Spot the Bug
Saturday, October 25, 2008 at 03:56PM One of our students is having problems with his C# conditions:
if (HighestSales <= EachSales );
Console.WriteLine ("new top score");
It always prints the message. Why?
Rob |
11 Comments | in
Teaching,
development
Teaching,
development
Reader Comments (11)
A lot of programmers always use the curly braces since it's probable that you will add stuff there in the future. For example:
if (HighestSales <= EachSales) {
Console.WriteLine("new top score");
HighestSales = EachSales;
}
print('New Top Score') if (HighestSales <= EachSales);
Which I like a lot :-), and it only uses one ';'.
I like curly brackets too! :-P
i'd do...
if(EachSales >= HighestSales)
{
EachSales = HighesSales;
Console.WriteLine("New Highest Sales: " + HighestSales);
}
sorry, formatting messed me up. I also think DeMorgans theroem might be involved somehow, or am I trying to cover too many bases?
:)
I would question though, even though single lines after the condition are valid C# (I always use curly brackets myself), does the ; just act like an empty { } even though they are not used? Just curious.
I can't post comments on my own blog :( but I did read yours and would be interested, especially since Silverlight seems viable we should stick to a unifying language of C#.
On the other hand, I really want my students to understand that in programming the effect of a single misplaced character can be enormous, and that the machine never knows enough to "understand" what you are trying to do. So the only way forward is to get it right....
if(1 >= 0){
Console.WriteLine("X");
}
X would be printed wouldn't it ? or did you mean that statement, with no main initialised and such.
Anyhoo, its over now and you must be jetlagged so i'll let you have a break.