ZFGC

Resources => Coding => Topic started by: AoDC on March 11, 2008, 08:28:07 am

Title: For each loop...
Post by: AoDC on March 11, 2008, 08:28:07 am
My college is !@#$% annoying the !@#$% out of me. Every lesson we go on, there is always one question or more for homework that tells us nothing and expects us to do god's work. The answers are there as class files, so I decompile them, and sons of bitches, they cheat! While they expect us to write a 50 line algorithm, they've used one method that comes with a prepackaged java class. How is that legit at all.

Anyway, can somebody please thoroughly explain a for-each loop? Our presentation today told us nothing. They just showed it, with no example, and the teacher says "yea umm I don't know it looks like C#-ish but yea just use the .length property lolz" well haha no !@#$%. Somebody explain please. I've used it once and it has worked once but I'm still lost basically.

Thanks in advance.
Title: Re: For each loop...
Post by: Zero Beat on March 11, 2008, 09:10:19 am
I should be able to help, but first please tell me something as I have forgotten: Is there a difference between a for loop and a for-each loop? Or did you just call a for loop by the wrong name?
Title: Re: For each loop...
Post by: AoDC on March 11, 2008, 09:16:03 am
Yes, there is.

for(i=0;i<100;i++) is a for loop
for(Circle c : cArr) is a for-each loop (in java anyway)

Thanks anyway.
Title: Re: For each loop...
Post by: Zero Beat on March 11, 2008, 09:32:50 am
Yes, there is.

for(i=0;i<100;i++) is a for loop
for(Circle c : cArr) is a for-each loop (in java anyway)

Thanks anyway.

Wow, I've made about 5 games for Java from scratch, but I've never seen one of these before. LoL, thats quite funny to me. Anyway, could I trouble you for a link explaining a for-each loop in depth?
Title: Re: For each loop...
Post by: AoDC on March 11, 2008, 09:40:37 am
Anyway, could I trouble you for a link explaining a for-each loop in depth?
That is why I made this topic. I am looking for one. Haha =P
Title: Re: For each loop...
Post by: Infinitus on March 11, 2008, 09:54:41 am
For each blocks just iterate through all objects within a list.

Say you had a list of objects, using a for statement you could interate through it like this (syntax is C#, probably different for java but same principle applies).

Code: [Select]
for (int i = 0; i < myObjectList.Count; i++)
{
     object obj = myObjectList.AtIndex(i);
     // Modify object here.
}

The equivilent for-each loop would look like this;

Code: [Select]
foreach (object obj in myObjectList)
{
         // Modify object here.
}

Its essentially just syntax sugar, its has no real advantage over a for loop, it just automatically tracks the index.
Title: Re: For each loop...
Post by: Zero Beat on March 11, 2008, 11:37:09 am
Seems like it would fix the off by one error that happens a lot, but can you use a variable in that? Like object?
Title: Re: For each loop...
Post by: AoDC on March 12, 2008, 05:03:35 am
Started using it more today I understand how it works now. Thanks helios. And yea C# and Java use for each differently ;)
Title: Re: For each loop...
Post by: nitz on March 15, 2008, 12:10:31 am
Seems like it would fix the off by one error that happens a lot, but can you use a variable in that? Like object?

(Speaking from a C# standpoint:)

foreach is just as versatile as a for loop.

When most people learn for loops, they learn syntax like:

Code: [Select]
for (int i=0; i < 10; i++) { /* Something */ }

However, a for loop is way more versatile than a counter such as that. Each one of the statements in the for loops 'arguements' (Not what it's called, but I'm unaware of the actual naming), is a statement, each executed at a different time:

Code: [Select]
for ( A; B; C)

A - Executed before the loop. The 'initialization'. Variables declared here are local to the scope of the for loop.
B - Exit Condition. While this statement resolves to non-zero, the loop will continue.
C - Update. Normally used to push a condition closer to pushing out of the loop (Making B resolve false).

You could re-write that first for loop as a while loop, such as:


Code: [Select]
int i = 0;
while (i < 10)
{
 //Do something.
 i++;
}

And that has all three parts, initialization, update, and exit condition, illustrated here:


Code: [Select]
A;
while (B)
{
 //Do something.
 C;
}

Knowing the structure of how loops such as this execute lets you do a lot fancier things. Such as, people use this for an infinite loop:
Code: [Select]
while(1) {}

That can be done the same way with a for loop:
Code: [Select]
for(;;) {}
A blank statement initializes, a blank statement updates, and theres no exit condition, so it continues forever.

How does this all relate to a foreach loop?

A for each essentially takes care of doing A, B, and C for you, assumed from one statement. You could roll out any foreach loop into a for (or any other, for that matter) loop. Take the following C# foreach:
Code: [Select]
foreach (String s in lStringList)
{
  Console.WriteLine(s);
}

Now lets see what's happening, if it were written out in a traditional for loop:
Code: [Select]
for (int i = 0; i < lStringList.Count; i++)
{
   String s = lStringList[i];
   Console.WriteLine(s);
}

Both would provide the exact same output.

For the curious, here's a little sample code that prints out what I illustrated above. (A C# Console App)
Code: [Select]
using System;
using System.Collections.Generic;
using System.Text;

namespace consoleappcsharp
{
class Program
{
static void Main(string[] args)
{
//Make a new list that holds strings.
List<String> lStringList = new List<String>();

//Add three test strings.
lStringList.Add("Test String 1");
lStringList.Add("Test String 2");
lStringList.Add("Test String 3");

//A foreach loop that prints each test string.
Console.WriteLine("foreach (String s in lStringList)\n");
foreach (String s in lStringList)
{
Console.WriteLine(s);
}

//A for loop that mimics the same thing as the above foreach
Console.WriteLine("\n\nfor(int i = 0; i < lStringList.Count; i++)\n");
for (int i = 0; i < lStringList.Count; i++ )
{
String s = lStringList[i];
Console.WriteLine(s);
}
}
}
}

Sorry if I got a bit long winded, I just enjoy this stuff :)

Contact Us | Legal | Advertise Here
2013 © ZFGC, All Rights Reserved