Thought for the Dazed

I've had to give up that Distance Learning course as I was having trouble seeing the teacher.

Flickr
www.flickr.com
RobMiles' items Go to RobMiles' photostream
Twitter
C# Yellow Book

Search entire site
« Let's Go Skydiving | Main | Los Angeles and PDC 2008 Here I Come »
Saturday
Oct252008

Spot the Bug

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?

Reader Comments (11)

There's an extra ; after the end of the if condition. That means it will take no action, and whatever comes next is always executed. If you remove it, it will work.

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;
}
October 25, 2008 | Unregistered CommenterAlcides Fonseca
Prime example of one of the reasons I am not a fan of the semi-colon curley brace school of programming languages. :-)
October 25, 2008 | Unregistered CommenterAlfred Thompson
In perl you can do cute things like:

print('New Top Score') if (HighestSales <= EachSales);

Which I like a lot :-), and it only uses one ';'.

I like curly brackets too! :-P
October 25, 2008 | Unregistered CommenterPhilC
Not to mention it should be referenced in accordance to the EachSales compared to the Highest sales, and the colon messes things up aswell.

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?

:)
October 25, 2008 | Unregistered CommenterJDog - Joel Parkey
The extra semi-colon, agreed.

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.
October 25, 2008 | Unregistered CommenterShawn
@Shawn

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#.
October 25, 2008 | Unregistered CommenterJDog - Joel Parkey
I do think it's a little silly they carried this practice over to C#. If you want an empty control block you could just do { } and then removing the ability for ; to be an empty control block would stop this common beginner error.
October 26, 2008 | Unregistered Commenter[ICR]
Well done everyone. Visual Studio gives you a wavy "are you sure you want to do this" blue line underneath the offending character, but I agree that it is confusing.

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....
October 26, 2008 | Registered CommenterRob
To be specific that above code wouldn't print anything, would it Rob ? As it happenned in our quiz you posted on ebridge today there was a trick question much like this. Is 16/20 considered Ok ?
October 30, 2008 | Unregistered CommenterJDog - Joel Parkey
The code would always print "New Top Score". And 16/20 is a very good mark.
October 31, 2008 | Registered CommenterRob
It would but I was referencing the question,

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.
October 31, 2008 | Unregistered CommenterJDog - Joel Parkey

PostPost a New Comment

Enter your information below to add a new comment.
Author Email (optional):
Author URL (optional):
Post:
 
All HTML will be escaped. Hyperlinks will be created for URLs automatically.