Welcome, Guest. Please login or register.
Did you miss your activation email?
Pages: [1]   Go Down
  Print  
Author Topic: Basics of Loops! [Tutorial]  (Read 555 times)
0 Members and 1 Guest are viewing this topic.
_ikram
Member
*

Reputation: 3
Offline Offline
Posts: 65
Referrals: 0

Awards
« on: July 31, 2009, 08:25:13 AM »

Note, this is pretty basic guide.

Loops in Java are very useful for those who want to implement things that repeat them self.  For instance, we would not use a loop in this situation:

Code:
badExample += 1;
badExample += 1;

Alright, I have to admit this is a bad example but hey.  Anyways, lets say that we wanted this integer to grow 10 times.  Now this is where the loop comes in handy.  Every loop must begin with something like the following.

Code:
for (int i = 0; i < 10; i++) {

Please note that the number 10 can be changed to match certain situations.  Anyhow, this basically means for the loop, it will then set the integer to 0 and then it'll count to 10 because of i++.  Hopefully, you understood what that meant. 

Now, here is one last example to clear most things up with you.  This is a better example then the other one because you will actually see this in some servers.

Code:
       
        client.stream.writeString("");
        client.stream.writeString("");
        client.stream.writeString("");
        client.stream.writeString("");
        client.stream.writeString("");
        client.stream.writeString("");
        client.stream.writeString("");
        client.stream.writeString("");

What this does is that it replaces the line of text with nothing.  But, it's pointless to write out the same thing over again.  So what you would use is a loop to make things smaller and easier to edit.

Code:
for (int i = 0; i < 8; i++) {
        client.stream.writeString("");
}

What this does it that it'll basically replace the line of text 8 times with nothing given it the same effect as the code a bit above.
Logged

RuneScape Developers community: www.rspsnetwork.com
Hextion Private Server: www.hextion.com
two0426
Junior Member
*

Reputation: 0
Offline Offline
Posts: 6
Referrals: 0

Awards
« Reply #1 on: May 31, 2010, 01:45:31 AM »

spam
« Last Edit: May 31, 2010, 02:15:52 AM by Arkie » Logged
Pages: [1]   Go Up
  Print  
 
Jump to:  

Your Ad Here