Give me your favorite four ways to do FOR

In listening to a recent Stackoverflow podcast Joel Spolsky was discussing one of his interview questions.  How to determine if a point is within a rectangle.    This was a great question and I spent the rest of my commute trying to determine how many different languages and ways you can do this.

My favorite has to be using the .Net framework’s Rectangle.Contains(Put Point Here). 

Point MyPoint2Test = new Point(10, 10); 
Rectangle MyRectangle
= new Rectangle(20, 40, 40, 40);

bool RectangleContainsPoint = ((MyPoint2Test.X >= MyRectangle.Left) &&
< MyRectangle.Right) &&
>= MyRectangle.Top) &&
< MyRectangle.Bottom));

== MyRectangle.Contains(MyPoint2Test));

This made me think. In any given day most developers, at least I do, program in several languages.  Even if you are only flipping back and forth between C# and Vb.Net.  This made me think of another basic interview questions.  Can you show me four different ways to write a simple FOR loop.  Bonus points for different languages.


for (var i = 0; i < arr.length; i++) {
// some code here



for (var i = 0, len = arr.length; i < len; i++) {
// some code here


for(int i = 1;i<=10;i++)
//Some code here


FOR i IN 1..1000 LOOP
-- Some code here


for (int i = 0; i < args.length; i = i + 1) { // Test and Loop
// Some code here


Post a Comment