My question is about looping skill overall.
I am having problems with loops. Is there any book/tutorial/website about this particular skill in java programming to help me learn how to loop in correct/effective way ? I know how to write simple loops, I know structures of loops too. When I need to write program from the book I cannot figure it out how to write a loop which is working well. It is always like that...Im stuck always in loops.

Does effective looping come with experience ?

Any help ?

Thanks

Recommended Answers

All 5 Replies

I don't think effective looping comes with experience. I've been programming only for a while but I've got a hang of looping. http://www.youtube.com/watch?v=1njHZGefOiQ check out this guy. He helped me loads with my programming especially when it came to loops, I also bought a book called java made easy on amazon, its really cheap but extremely useful...lol.

hi bombay1982,
ya off course the effective looping come with experience
for my knowledge roseindia.net is good one. try to watch some video tutorials in you tube

could you give us an example of one of a program you are trying to write and what your solution is? maybe you're just making minor mistakes against other parts of the logic that make you think your way of iterating over data is incorrect or inefficiënt

Here I have a simple program.
Exercise 4.7 "Introduction to programming" Daniel Liang
(Finding the highest score) Write a program that prompts the user to enter the number of students and each student's name and score, and finally displays the name of the students with the highest score and the student with the lowest score.

import java.util.*;

public class fordot8{
  public static void main(String[] args) {
    Scanner input = new Scanner(System.in);

    // Prompt the user to enter the number of students
    System.out.print("Enter the number of students: ");
    int numOfStudents = input.nextInt();

    System.out.print("Enter a student name: ");
    String student1 = input.next();
    System.out.print("Enter a student score: ");
    double score1 = input.nextDouble();

    
    
    for (int i = 0; i < numOfStudents - 1 ; i++) {                // is there any other way to write it ???
      System.out.print("Enter a student name: ");
      String student = input.next();

      System.out.print("Enter a student score: ");
      double score = input.nextDouble();

      if (score > score1) {   // problem 
        student1 = student;
        score1 = score;
      }
    }

    System.out.println("Top student " +
      student1 + "'s score is " + score1);
  }
}

I havent finished last question yest with the lowest score. But there is no big deal to check the answer, the problem is to find solution by myself.
Can you show me step by step how you guys are planning to write the program like that? Do you split this for a smaller steps or not ? How do you trying to find out how to use that loops. Many times you need to write loop inside the loop or loop with the if statemenets, or 2 different loops, or there are many other possibilities. I think sometimes I may have problem with logical thinking. I am lets say "depressed" :)

I was suprised that author wrote

for (int i = 0; i < numOfStudents - 1 ; i++) {

I wouldnt find solution by myself with that

Next where I had a problem is if statemenet

if (score > score1) {
student1 = student;
score1 = score;

I need to add this is a simple program. I have to learn stuff like that effectively to make step forward to the next topic :methods

well, sure you can write it differently, and more efficient. for instance: why do you already read one outside of your loop?
but if your problem is in the if statement, it has nothing to do with your loop.

for (int i = 0; i < numOfStudents - 1 ; i++) {

well, you don't want to do it more than the number you've already put, so:

I would use, since you already have data for one student before you go into the loop.
looks like in the end, you'll have one student more than you asked for.

does your program give you an error message, propably in the command prompt, that can tell you a lot of why it won't work.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.