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 () 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 () below!
You can also find further discussion and get answers to your questions over in Language Help.
Agree with a comment or answer? Like () to up-vote the contribution!
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
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);
}
}
}
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…
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.
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).
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 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
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
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