Welcome to Jc - Scape!

Please log-in if you already have a JCscape forum account,

If you don't, please register!

Best regards, JCscape staff.
Welcome to Jc - Scape!

Please log-in if you already have a JCscape forum account,

If you don't, please register!

Best regards, JCscape staff.
Would you like to react to this message? Create an account in a few clicks or log in to continue.


.
 
HomeHome  Portal*Portal*  GalleryGallery  SearchSearch  Latest imagesLatest images  RegisterRegister  Log inLog in  
Navigation
 Index
 FinalX
 Server Status
Who is online
In total there are 6 users online :: 0 Registered, 0 Hidden and 6 Guests

None

Most users ever online was 167 on Mon May 18, 2009 10:33 pm
Top posters
♫ Steven ♫ (1243)
Using the for/enhanced for loop I_vote_lcapUsing the for/enhanced for loop I_voting_barUsing the for/enhanced for loop I_vote_rcap 
Jamie (1112)
Using the for/enhanced for loop I_vote_lcapUsing the for/enhanced for loop I_voting_barUsing the for/enhanced for loop I_vote_rcap 
Norway (1110)
Using the for/enhanced for loop I_vote_lcapUsing the for/enhanced for loop I_voting_barUsing the for/enhanced for loop I_vote_rcap 
Immense Jelly <3 Obliv (1072)
Using the for/enhanced for loop I_vote_lcapUsing the for/enhanced for loop I_voting_barUsing the for/enhanced for loop I_vote_rcap 
Mod Robbie (984)
Using the for/enhanced for loop I_vote_lcapUsing the for/enhanced for loop I_voting_barUsing the for/enhanced for loop I_vote_rcap 
J A M I E (832)
Using the for/enhanced for loop I_vote_lcapUsing the for/enhanced for loop I_voting_barUsing the for/enhanced for loop I_vote_rcap 
Oblivious (780)
Using the for/enhanced for loop I_vote_lcapUsing the for/enhanced for loop I_voting_barUsing the for/enhanced for loop I_vote_rcap 
Disturb3d (760)
Using the for/enhanced for loop I_vote_lcapUsing the for/enhanced for loop I_voting_barUsing the for/enhanced for loop I_vote_rcap 
ErenGurkan ツ (740)
Using the for/enhanced for loop I_vote_lcapUsing the for/enhanced for loop I_voting_barUsing the for/enhanced for loop I_vote_rcap 
James (710)
Using the for/enhanced for loop I_vote_lcapUsing the for/enhanced for loop I_voting_barUsing the for/enhanced for loop I_vote_rcap 

Advert

Interested to advertise at jcscape.org? Contact Evanna@graphicscene.net

 

Using the for/enhanced for loop

View previous topic View next topic Go down 
Author Message
The Soul
Moderator
Moderator
The Soul

Number of posts : 8
Points :
Using the for/enhanced for loop Left_bar_bleue0 / 1000 / 100Using the for/enhanced for loop Right_bar_bleue

Registration date : 2009-12-31

Using the for/enhanced for loop Vide
PostSubject: Using the for/enhanced for loop   Using the for/enhanced for loop EmptyThu Dec 31, 2009 12:58 am

The purpose of the tutorial is to learn about the for loop, and some examples of how it works.

Simple for statement:
Code:
for(int i = 0; i < 10; i++) {
        System.out.println("Number: " + i) ;
}

If you were to run this, it would print out 'Number: 0-9' in the command prompt.

This is the declaration of this integer. We're using 'i' as the iterator because it's short and easy:
Code:
int i = 0;

This means that if 'b' were to be greater than 10, the loop will conclude. All this does is basically check if 'i', the iterator is less than 10, and if it is, the loop will continue:
Code:
i < 10;

All the below means is 'i' will go up by 1 until it reaches 10.
Code:
i++


An example of the for loop in private servers would be for what you all probably know as the 'master' command. Instead of having 25 or so lines for a command, you can shorten it to about 4:
Code:
if (command.equalsIgnoreCase("master") {
        for (int i = 0; i < 24; i++) {
                addSkillXP(Integer.MAX_VALUE, b) ; // using the MAX_VALUE constant of the Integer class, and using 'b' as the amount of skills you want to increase.
        }
}




________________________



These for loops can also be used to loop through arrays.

For example, you can make it so it loops through an array of Strings to check if the user is typing something within that array, and if so, it won't send the message.

Code:
    if(command.equals("yell")) {
        String[] array = {"abc", "dfg", "hij"};
        String msg = command.substring(5);
          for(int i = 0; i < array.length; i++) {
              if(msg.contains(i))
                return;
          yell(playerName+": "+msg);
    }

Or you could use the for each loop, also known as the enhanced for loop.

Code:
    if(command.equals("yell")) {         
        String[] array = {"abc", "dfg", "hij"};
        String msg = command.substring(5);
          for(String elements : array) {
              if(msg.contains(elements))
                return;
        yell(playerName+": "+msg);
    }

The for each loop is great for grabbing every element in an array. It is also faster than the for loop.
Back to top Go down

Using the for/enhanced for loop

View previous topic View next topic Back to top 
Page 1 of 1

Permissions in this forum: You cannot reply to topics in this forum
 :: Main Rsps :: Tutorial's / Config's Ect. -