Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: How to think about LOOPS in the first place

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to think about LOOPS in the first place

    Hello,
    I'm new to this forum. I saw some of the threads and posts today and then I decided to register as I found a lot of people who tend to provide REAL help. I am not a CS student. I just want to learn programming on my own (I don't have enough money/time to take a private class ). So I read a book, write sample examples, and try to solve the problems at the end of each chapter.

    I have a problem with loops. The syntax is pretty simple. It's easy to understand how a for or a while loop works. But when I try to solve problems I found that I cannot figure out how to write the loop to make the required task. For example, I cannot figure out the condition/s to end the loop. Of course, I am talking about some intermediate problems. Not trivial ones (to count even/odd number, print numbers from 1 - 100, get the sum of numbers that the user enters from the keyboard ...etc).

    So, I feel that I missed the main concept of how to THINK of a loop not how to write a loop (the syntax part). I'd like to hear from you guys.

    Thanks in advance,


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,606
    My Mood
    Hungover
    Thanks
    145
    Thanked 658 Times in 563 Posts

    Default Re: How to think about LOOPS in the first place

    Recommended reading: The for Statement (The Java™ Tutorials > Learning the Java Language > Language Basics)

    I'm not sure exactly what you're confused about. Could you provide an example SSCCE that demonstrates something that you're not clear on?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Apr 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to think about LOOPS in the first place

    Thanks for your reply and sorry, if I posted my thread on the wrong sub-forum. This is an example of a problem that I could not figure out how to construct loops to solve it.

    Write an application that displays the following patterns separately, one below the other. Use for loops to generate the patterns. All asterisks should be printed by a single statement of the form System.out.print( '*' ); which causes the asterisks to print side by side. A statement of the form System.out.println(); can be used to move to the next line. A statement of the form System. out.print( ' ' ); can be used to display a space for the last two patterns. There should be no other output statements in the program.
    Modify the previous exercise to combine your code from the four separate triangles of asterisks
    such that all four patterns print side by side
    image.jpg
    Last edited by searchformeaning; April 19th, 2012 at 08:49 AM.

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: How to think about LOOPS in the first place

    This intuition comes with practice. As you program more, you'll figure out where are good places to use loops and how to use them effectively. Also, loops are a lot about math (particularly for loops).

  5. #5
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,606
    My Mood
    Hungover
    Thanks
    145
    Thanked 658 Times in 563 Posts

    Default Re: How to think about LOOPS in the first place

    One way to help you think about assignments like this is by using a piece of paper and a pencil. Draw this out by hand, labeling stars and spaces with numbers. Do you see a pattern?

    Hint: you can put loops inside of loops.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    Junior Member
    Join Date
    Apr 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to think about LOOPS in the first place

    Quote Originally Posted by KevinWorkman View Post
    One way to help you think about assignments like this is by using a piece of paper and a pencil. Draw this out by hand, labeling stars and spaces with numbers. Do you see a pattern?

    Hint: you can put loops inside of loops.
    Thanks for your help. Now, I know how to do it. That's the kind of help I am searching for. How to THINK of a problem not here's the code to do what you want.

  7. #7
    Junior Member
    Join Date
    May 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to think about LOOPS in the first place

    Okay let's take a real problem: You want to write a file listing function. This means you have to recursively run through all directories and subdirectories.
    The first question you ask yourself is: What do I need? -> Files. Next: How do I interact with files? -> File Class

    Take this small piece of code
    public class Listing{
       //fields
      private File file; //You declared a File with name file
     
      //constructor
      public Listing(){
         this.file = new File("C:/text.txt");
       }
     
      public String toString(){
       System.out.println(this.file.getName());
       }
    }

    Now you got one file you can talk to. So say we want to get all the files in a directory now:
    public class Listing{
       //fields
      private File file; //You declared a File with name file
      private File folder; //A folder is also a file
      //constructor
      public Listing(){
         this.folder = new File("C:/test);
       }
     
      public void getFiles(){
        if(folder.isDirectory()){
          File[] files = folder.listFiles(); //put all the files in an array
          //Now what you have to do is iterate through the list till you reach the end.
         /* Important to note is that arrays, collections, etc. all have one thing in common... 
             They have a size. It's important to know the size, because this will be the max of your loop (for now)
         */
          //You can use whatever kind of loop you which, I'm going to use a for loop because it's the shortest
                     for(int i=0; i<files.length(); i++){
                       //important to note is that arrays have lengths, but if I were to work with a collection it would be a size.
                          System.out.println(files[i].getName());  
                     }
          //Such a loop is still too consuming, that's why most people use a for-each loop when iterating through lists to show their contents. NEVER USE A FOR EACH LOOP      TO CHANGE THE VALUE OF AN OBJECT... It's just not good programming manners
                   for(File f:files){
                        System.out.println(f.getName());  //As you can see much less code
                   }
            }
       } 
     
      public String toString(){
       System.out.println(this.file.getName());
       }
    }
    Now this is a basic concept: When you want to create loops you have to think: What do I want to loop through? Objects? Numbers? Do I want to stop at a certain point? (If statement in your loop) Do I want to read or write/alter data? (for-each or for/while/do while)

    Note: this is pseudo code, it might give an error or two I don't know, I just came up with it on the spot
    I'm sparing real recursion because it's somewhat more advanced, but this is the basic one folder search
    Last edited by mobinni; May 1st, 2012 at 02:08 PM.

Similar Threads

  1. How to place JSF pages into OSGI bundle?
    By rcbandit in forum JavaServer Pages: JSP & JSTL
    Replies: 0
    Last Post: November 1st, 2011, 05:08 AM
  2. A place to call home
    By Tsarin in forum Member Introductions
    Replies: 1
    Last Post: October 14th, 2011, 08:56 AM
  3. I cannot get the right output in the right place. Where is my problem?
    By kl2eativ in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 26th, 2011, 08:28 AM
  4. Where to place my own library?
    By hexwind in forum Java Theory & Questions
    Replies: 3
    Last Post: June 22nd, 2011, 07:25 AM
  5. great place to start
    By flotsam in forum The Cafe
    Replies: 2
    Last Post: April 22nd, 2010, 11:52 AM