Hello Guest, please login or register.
Did you miss your activation email?
Login with username, password and session length.

Pages: [1]   Go Down

Author Topic: For each loop...  (Read 2078 times)

0 Members and 1 Guest are viewing this topic.
For each loop...
« on: March 11, 2008, 08:28:07 am »
  • *
  • Reputation: +1/-0
  • Offline Offline
  • Gender: Male
  • Posts: 4588
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.
Logged
the a o d c
Re: For each loop...
« Reply #1 on: March 11, 2008, 09:10:19 am »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Posts: 531
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?
Logged
So give me you're code and add mine:
Code: 4296-2644-3642

Sorry guys.  I just don't have the time anymore for the CP.  Working two jobs and just having a couple hours to play my Wii and continue learning programming just is not leaving me free time to focus on the CP.  I feel that it's my obligation to step aside as Level Director.  It's just a time issue.  Since I am also a global mod that also takes some of my focus from the CP.  So, it's nothing against anybody or what not, I'm just out of time anymore it seems.  Sorry again.
wtf, i remember posts that used to have content explaining reasons rather than ways to get rises out of users with a youtube video and no reason as to why!


How to post maturely:
epic intolerance, I caught aids from the stupidity in your post

why not issue everyone 5 opinion tokens for each year, if they voice their opinion without presenting a token- they can be legally murdered
Make sure every post you make is mature like these two (and there's many more). You just can't beat ZFGC's friendliness and maturity!
Re: For each loop...
« Reply #2 on: March 11, 2008, 09:16:03 am »
  • *
  • Reputation: +1/-0
  • Offline Offline
  • Gender: Male
  • Posts: 4588
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.
Logged
the a o d c
Re: For each loop...
« Reply #3 on: March 11, 2008, 09:32:50 am »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Posts: 531
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?
Logged
So give me you're code and add mine:
Code: 4296-2644-3642

Sorry guys.  I just don't have the time anymore for the CP.  Working two jobs and just having a couple hours to play my Wii and continue learning programming just is not leaving me free time to focus on the CP.  I feel that it's my obligation to step aside as Level Director.  It's just a time issue.  Since I am also a global mod that also takes some of my focus from the CP.  So, it's nothing against anybody or what not, I'm just out of time anymore it seems.  Sorry again.
wtf, i remember posts that used to have content explaining reasons rather than ways to get rises out of users with a youtube video and no reason as to why!


How to post maturely:
epic intolerance, I caught aids from the stupidity in your post

why not issue everyone 5 opinion tokens for each year, if they voice their opinion without presenting a token- they can be legally murdered
Make sure every post you make is mature like these two (and there's many more). You just can't beat ZFGC's friendliness and maturity!
Re: For each loop...
« Reply #4 on: March 11, 2008, 09:40:37 am »
  • *
  • Reputation: +1/-0
  • Offline Offline
  • Gender: Male
  • Posts: 4588
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
Logged
the a o d c
Re: For each loop...
« Reply #5 on: March 11, 2008, 09:54:41 am »
  • *
  • Reputation: +3/-0
  • Offline Offline
  • Gender: Male
  • Posts: 6629
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.
Logged
Re: For each loop...
« Reply #6 on: March 11, 2008, 11:37:09 am »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Posts: 531
Seems like it would fix the off by one error that happens a lot, but can you use a variable in that? Like object?
Logged
So give me you're code and add mine:
Code: 4296-2644-3642

Sorry guys.  I just don't have the time anymore for the CP.  Working two jobs and just having a couple hours to play my Wii and continue learning programming just is not leaving me free time to focus on the CP.  I feel that it's my obligation to step aside as Level Director.  It's just a time issue.  Since I am also a global mod that also takes some of my focus from the CP.  So, it's nothing against anybody or what not, I'm just out of time anymore it seems.  Sorry again.
wtf, i remember posts that used to have content explaining reasons rather than ways to get rises out of users with a youtube video and no reason as to why!


How to post maturely:
epic intolerance, I caught aids from the stupidity in your post

why not issue everyone 5 opinion tokens for each year, if they voice their opinion without presenting a token- they can be legally murdered
Make sure every post you make is mature like these two (and there's many more). You just can't beat ZFGC's friendliness and maturity!
Re: For each loop...
« Reply #7 on: March 12, 2008, 05:03:35 am »
  • *
  • Reputation: +1/-0
  • Offline Offline
  • Gender: Male
  • Posts: 4588
Started using it more today I understand how it works now. Thanks helios. And yea C# and Java use for each differently ;)
Logged
the a o d c
Re: For each loop...
« Reply #8 on: March 15, 2008, 12:10:31 am »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Posts: 256
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 :)
Logged
  • My Myspace?
Pages: [1]   Go Up

 


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



Page created in 0.036 seconds with 52 queries.

anything