FAQ: Learn Java: Loops - Using For Loops

This community-built FAQ covers the “Using For Loops” exercise from the lesson “Learn Java: Loops”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn Java

FAQs on the exercise Using For Loops

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in Language Help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to Language Help and Tips and Resources. If you are wanting feedback or inspiration for a project, check out Projects.

Looking for motivation to keep learning? Join our wider discussions in Community

Learn more about how to use this guide.

Found a bug? Report it online, or post in Bug Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

class Coffee {
  
  public static void main(String[] args) {
    /*
    int cupsOfCoffee = 1;
    
    while (cupsOfCoffee <= 100) {
      
      System.out.println("Fry drinks cup of coffee #" + cupsOfCoffee);
      cupsOfCoffee++;
      
    } end of while loop
    */
    for (int i = 1; i <= 100; i++) {
      System.out.println("Fry drinks cup of coffee #" + i);
    } // end of for loop
    
  }
  
}

I commented out the while loop using /* */
I wanted to leave it there for comparison and to help with the creation of the for loop

5 Likes

Why does this fail the test yet the output is correct?

class Coffee {

  public static void main(String[] args) {

    // initialize cupsOfCoffee
    int cupsOfCoffee = 1;
    // add while loop with counter
    for (; cupsOfCoffee <= 100; cupsOfCoffee++) {
      System.out.println("Fry drinks cup of coffee #" + cupsOfCoffee);
    }
  }

}
1 Like

In all likelihood, the SCT for the lesson is using some form of regex test to see if your for loop looks like this:

for (int cupsOfCoffee = 1; cupsOfCoffee <= 100; cupsOfCoffee++)
2 Likes

I just tried to solve the exercise in 2 different ways (with the for-Loop) and it was incredibly frustrating seeing the Result being right, but me not being able to continue, because I used i instead of cupsOfCoffee and <101 instead of <= 100…

3 Likes

I agree. From a subjective viewpoint, the while loop begins at the “while” statement, not the initialization of the cupsOfCoffee integer variable.

2 Likes

this question has trash answer validation. check the output, not the actual code. this for loop could be written in so many different ways. just look for “for,” not the specific regex.

1 Like

I wanted to mention that I wrote my code as follows, which passed, although the output read “Fry drinks cup of coffee #1” 100 times. My initial reaction was, “WTF is going on here?” until I figured out my mistake. Can you find it? Hint: it’s in the for loop header.

class Coffee {

public static void main(String args) {

int cupsOfCoffee = 1;

for (int i = 1; i <= 100; i++) {
  System.out.println("Fry drinks cup of coffee #" + cupsOfCoffee);
}

}

}

This exercise did a good job of introducing for loop headers (whereas–I believe?–the concept was more swiftly glossed over in the introduction to javascript series).

1 Like

I wish this task / lesson had better answer validation!

There are several different ways to write the code for this exercise. This is how my solution looked…

public static void main(String[] args) {
    
    int cupsOfCoffee = 1;

    for (int i = 1; i <= 100; i++) {
       cupsOfCoffee = i;
       System.out.println("Fry drinks cup of coffee #" + cupsOfCoffee);
    }

Hi Michael,

I found the below method worked for me .

int cupsOfCoffee = 1;

for (int i = 1; i <= 100; i++) {
  cupsOfCoffee = i;
  System.out.println("Fry drinks cup of coffee #" + cupsOfCoffee);
}

That way, you are able to check for cupsOfCoffee which is 100 .

In the code, if you replace cupsOfCoffee with i , then cupsOfCoffee will still be 1 .

for (int i = 1; i <= 100; i++) {
  System.out.println("Fry drinks cup of coffee #" + i);
}

Cheers

the keyword is:

for (int i = 1; i <= 100; i++)

Man this lesson made me furious lol.
It can be written correctly so many ways but for some reason they want it like this:

class Coffee {
public static void main(String args) {

int cupsOfCoffee = 1;

for (int i = 1; i <= 100; i++) {
   cupsOfCoffee = i;
   System.out.println("Fry drinks cup of coffee #" + cupsOfCoffee);
}

}
}

1 Like

This should have better answer/code validation because there are multiple ways the code can be refactored and produce the correct results.

Came here just to write this as well!

Hi there, in the explanation of the for-loop course there is said that an off-by-one error occurs with this syntax:
int sum = 0;
for (int i = 0; i < 10; i++) {
sum += i
}
This code would produce an incorrect value of 45 .

I don’t get why this code produces a value of 45??? Can anybody help me explain? Thank you so much

sum += i     is shorthand for     sum = sum + i

In each iteration of the loop, i is added to the existing value of sum and after the addition, the result is assigned to the sum variable.

  • sum is initialized as 0 before the loop.
  • In the first iteration, sum is 0 and i is 0. Hence,    sum + i    evaluates to     0 + 0.     The result 0 is assigned to sum.
  • In the second iteration, sum is 0 and i is 1. Hence,    sum + i    evaluates to     0 + 1.     The result 1 is assigned to sum.
  • In the third iteration, sum is 1 and i is 2. Hence,    sum + i    evaluates to     1 + 2.     The result 3 is assigned to sum.
  • In the fourth iteration, sum is 3 and i is 3. Hence,    sum + i    evaluates to     3 + 3.     The result 6 is assigned to sum.
  • and so on …
  • In the last iteration, sum is 36 and i is 9. Hence,    sum + i    evaluates to     36 + 9.     The result 45 is assigned to sum.
// As a reminder,
// 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 ---equals--- 45

As explained in the exercise, the goal was to add the first ten numbers but because of the loop condition    i < 10   , the number 10 is missed and we end up with a sum of 45 instead of the expected sum of 55. To rectify this, the loop condition can be changed to either    i <= 10    or     i < 11

2 Likes

Great! I had the impression that sum = 0 stayed fixed each iteration as again the input value of the following iteration. Now it is completely clear to me! Enjoy coding

1 Like