Posts
60
Comments
36
Trackbacks
18
Basic For Loop Java

Here is a demo for anyone that wants to use for loops.  It demonstrates the for (with index) and the for each (without index).  Each has its own particular usage and this shows one way that it can be used.  In the future I may publish a more practical use of each, but for now this is a good start.  I have also attached the video and the files we created in Netbeans.

For loop with index:

for (int i = 0; i < str.length(); i++)
{ //Start for loop operations
 
    // now we can pick through this
    // string to print only e's (remember that
    // h is 0, e is 1)
    if (str.charAt(i) == 'e')
    {
        System.out.printf("e seen at char %s", i);
    }
} //End for loop operations

For each loop without index:

for (char ch : str.toCharArray())
{
    if (ch == 'e')
    {
        // This should only print out once
        System.out.println("e reached");
    } //end if
} //end for

Executable (Captivate) Video Video.zip

Netbeans Project Project.zip

Please feel free to leave comments or email me if you have any questions.

posted on Wednesday, February 25, 2009 7:05 PM Print
Comments
No comments posted yet.

Post Comment

Title *
Name *
Email
Url
Comment *  
Please add 2 and 6 and type the answer here:
News
My Developer Notebook! This also happens to be my opinion place. Thanks for coming by.