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
« Goodbye Graham | Main | Zero Marks »
Wednesday
Oct222008

C# Pop Quiz

This little exercise comes out of a First Year lecture last week. Given the code:

short i;
for (i = 0; i < 10; i--)
{
    Console.WriteLine("i is: " + i);
}

What would it do?

(For those of you that haven't seen it before, the i-- part of the for loop has the effect if reducing the value of i by 1. It is a shorthand version of "i = i - 1")

Reader Comments (18)

I guess the answer that sets of the QI sirens is "count down infinitely". Assuming short is signed in C#, it'll count down to the lower bound?

I might steal this for my Java students tomorrow ;)
October 22, 2008 | Unregistered CommenterNick Taylor
Isn't it mandatory to cast i to string?
Not sure, just a guess ;)
October 22, 2008 | Unregistered CommenterSjors Pals
I struggled to do the same thing in Visual Basic but the VB For loop doesn't work the same way. That's a good thing in my opinion BTW. I tried a While loop instead and came up with:
Dim i As Short = 0
While i < 10
i -= 1
Console.WriteLine("i is: " + i.ToString())
End While
VB knows an overflow and throughs an exception. That seems like a safer thing to do though of course it teaches a whole different principle.
October 22, 2008 | Unregistered CommenterAlfred Thompson
Well. Since a short in .net is a Signed 16-bit integer, it'll happily count down to -32,768....

Then all hell breaks loose...


PhilC.
October 22, 2008 | Unregistered CommenterPhilC
Well. Since a short in .net is a Signed 16-bit integer, it'll happily count down to -32,768....

Then all hell breaks loose...


PhilC.
October 22, 2008 | Unregistered CommenterPhilC
Whoops, double post there!
October 22, 2008 | Unregistered CommenterPhilC
LOL ;)....the value will be infinit till the end of short capacaty ...-
October 22, 2008 | Unregistered CommenterHossam
Well, my take would be that it would count down to -32,768 (printing out the "i is: " + i also) at which time the next i-- would decrement "i" again, causing it to wrap around to +32,768. At which time, the condition is broken, and the loop ends.
October 22, 2008 | Unregistered CommenterShawn
I'd do what PhilC said, since its logical and correct.
October 22, 2008 | Unregistered CommenterJDog - Joel Parkey
Beaten to it!
October 22, 2008 | Unregistered CommenterSimon Johnson
i would need to be i.tostring() or it wouldnt compile, other than that it would count down as above
October 22, 2008 | Unregistered Commentersam
Shawn wins (but there is no prize...)

The program as above actually runs as it is written. You don't need to add the ToString, but you can if you like. You can even write:

Console.WriteLine ("Hello".ToString());

...but this might be over doing it a bit.
October 23, 2008 | Registered CommenterRob
I thought PhilC said that? If not however, I agree with shawn, either way, I was in the lecture, so its fine :)

2^31 == 2,147,483,648 I was pretty close.

The only reason I knew was because of a little game called Hexic, were the maximum score used to be 2^31, since the patch we now have scores in the many trillions.

This given that a match of 3 is 35pts :)
October 23, 2008 | Unregistered CommenterJDog - Joel Parkey
PhilC said "all hell breaks loose." Which is not a programming term as I remember.

Then again.....
October 23, 2008 | Registered CommenterRob
Thats a good point...

I presume you were referencing your charlamaine, "Always make sure the 'crap' lands on the other persons head".
October 23, 2008 | Unregistered CommenterJDog - Joel Parkey
This is what I don't miss about college. Useless excercises that bear no sembalance to reality but are purely based on theory.

In the best of cases its an example of making something too terse.

In the worst of cases its a bug.

There is so much other stuff to teach that are more important. I can think of a couple. Knowing the lower bound of a short is not all that important. And in practice you would never write a for loop with that pattern.
October 30, 2008 | Unregistered CommenterJosh
He didn't specifically teach this, it simply cropped up and he was wondering how we thought C# treated this kind of thing. Most of us where surprised with the result, but in all nothing to dwell on...
October 30, 2008 | Unregistered CommenterJDog - Joel Parkey
it prints the value of i from 0 to -32767
and throws an compiler error after that
January 27, 2009 | Unregistered Commenterrajesh

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.