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 :-s ). 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,
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?
1 Attachment(s)
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.
Quote:
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.
Quote:
Modify the previous exercise to combine your code from the four separate triangles of asterisks
such that all four patterns print side by side
Attachment 1181
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).
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.
Re: How to think about LOOPS in the first place
Quote:
Originally Posted by
KevinWorkman
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.
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
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:
Code :
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