Looping 101

Here is a pop quiz for language aficionados.

Examine the following class:

public class Foo
{
   
public int GetUpperLimit()
    {
        Console.WriteLine(
"GetUpperLimit invoked");
       
return 5;
    }

   
public IEnumerable<int> GetCollection()
    {
        Console.WriteLine(
"GetCollection invoked");
       
return new int[] { 1, 2, 3, 4, 5 };
    }
}

Now examine the following console mode program:

class Program
{
   
static void Main(string[] args)
    {
       
Foo foo = new Foo();

       
for (int i = 1; i <= foo.GetUpperLimit(); i++)
        {
           
Console.WriteLine(i);
        }

       
foreach (int i in foo.GetCollection())
        {
           
Console.WriteLine(i);
        }
    }
}

 Without using a compiler, do you know:  

  • How many times "GetUpperLimit invoked" will appear in the output?
  • How many times "GetCollection invoked" will appear in the output?

Now, examine the same console program in VB:

Sub Main()
   
Dim foo As New Foo()

   
For i As Integer = 1 To foo.GetUpperLimit()
        Console.WriteLine(i)
   
Next

    For Each i As Integer In foo.GetCollection()
        Console.WriteLine(i)
   
Next
End
Sub

 Bonus third question:  

  • Will the output of the VB program be the same as the C# program?

I was suprised by the answer to the last question ...

Print | posted @ Sunday, April 29, 2007 1:12 AM

Comments on this entry:

Gravatar # re: Looping 101
by Aaron at 4/29/2007 4:10 PM

you've missed some code out of the c# example program...

Ok, quick guess, I'd say 5 GetUpperLimits, and 1 GetCollection.
For the VB, 1 GetUpperLimit and 1 GetCollection.

I'm sure I read an article about when the foreach loop would take the initialization outside the loop, but I can't remember what the rules were!
The most obvious answer would be 5 for everything, but I'm sure you wouldn't ask if that were true... would you :)

I've also got thoughts like debug vs optimized running round my head, but I suppose changing the behaviour would be a bit severe!
  
Gravatar # re: Looping 101
by scott at 4/30/2007 12:50 AM

Aaron: The VB answer is correct (1 and 1).

Oops, the C# code was bungled, sorry about that.

The answer of 5 for the GetUpperLimit is close...
  
Gravatar # re: Looping 101
by SimoneB at 4/30/2007 6:56 AM

The condition of the for loop is evaluated one additional time before exiting the loop, when the condition returns false, thus 6.
  
Gravatar # re: Looping 101
by Wayne Howarth at 4/30/2007 8:12 AM

Could the c# GetUpperLimit() be called 6 times maybe?

It is called at the very start after 'i' is initialised to 1, then again after 'i' has been incremented to 2, 3, 4, 5 respectively then finally after it is incremented to 6?
  
Gravatar # re: Looping 101
by Aaron at 4/30/2007 9:02 AM

Aha!
6 for the getUpperLimit - the last one being from the failing condition test!

I'm smiling now, with a warm fuzzy feeling... I REALLY should get out more :)))



  
Gravatar # re: Looping 101
by Marco Turrini at 4/30/2007 9:12 AM

Well, I must admit I made a guess which was wrong (I'd have said 5 and 5, both for C# and VB codes, totalling a 0 out of 4 of which I'm proud of), then I tried out and... well, I will not ruin the pleasure for the others, but... what's the reason for it? I've always told my buddies there were no difference in programming in C# or VB.NET and now you shake all my believings... )-; Luckily, today is an almost plenty monday here in my office...
  
Gravatar # re: Looping 101
by Eric D. Burdo at 4/30/2007 12:43 PM

I figured 6 for the C# code, and 1 for the VB.

C# - 5 for the loop and 1 for the failing test.

VB - The function is evaluated outside the loop and cached. So the routine is only called once.

In the VB world, that's why you can get better performance in a "For x = 1 to recordset.count" vs. "do until recordset.EOF"
  
Gravatar # re: Looping 101
by scott at 5/1/2007 2:37 AM

Marco:

Both languages are holding on to thier heritage (C++ for C#, VB for VB.NET). Overall, it's still about the runtime and the class library, so there generally isn't a huge difference in selecting one of these programming languages.
  
Gravatar # re: Looping 101
by Marco Turrini at 5/2/2007 7:40 AM

First of all, I want to apologize for being so late: due to timezone differencies (I live in Italy) and the fact the yesterday was a civil holyday here, I read your kind reply only a few minutes ago.
Last, but not least, I want to thank you for keeping this blog: I may not be the most qualified guy to tell this, but I think your blog is one the the five most interesting to read.
  

Your comment:

Title:
Name:
Email:
Website:
 
Italic Underline Blockquote Hyperlink
 
 
Please add 7 and 8 and type the answer here: