CodeGym /Java Course /New Java Syntax /Additional lessons for Level

Additional lessons for Level

New Java Syntax
Level 4 , Lesson 8
Available

If the topics we've covered aren't clear... Repeat them over and over again until they are :) But we hope that the lessons in this level have given you a good understanding of how to use loops in Java. You also learned what real numbers are and about some of the nuances of working with them. To sort out all the new information in your brain and help you understand how the programming theory is used in practice, we prepared a few additional materials for you.

For loop in Java

They say that the best programmer is the lazy programmer. Instead of repeating the same operations several times, the smart programmer will come up with an algorithm to do the necessary work for him or her. And do it so well so that it doesn't need to be redone. In some cases, a for loop will help you write the smallest required number of lines of code. In this article, we dive into its operating principles and examples of using them to solve various problems.

The while statement

Our very first programs were a sequence of instructions executed one after another, but programming work very often involves problems that require a completely different approach. This construct puts multiple actions into a concise and understandable structure. And that's exactly what we'll talk about.


4
Опрос
Loops,  4 уровень,  8 лекция
недоступен
Loops
Loops
Comments (23)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Anonymous #11549069 Level 5, Katowice, Poland Expert
10 September 2024
I had problem with this :


        for(int i = 0; i < 10; i++) {
            continue;
            System.out.println(i);
        }
the error is :

src\Main.java:16:13
java: unreachable statement
it show me red error so i mark thi answer but proper is probably with complier word. But you did not tell before whats difference between not compliered code and error . Looks same in IntellJ output
Luke Level 5, Saint Paul, United States
19 February 2024
I don't think I quite understand what the continue statement does. In this problem

for (i = 0; i < 10; i++){
     continue;
     System.out.println(i);
}
Why does it fail to compile? As I understand it, the continue statement simply skips whatever remains after it in the loop body before going into the next iteration. In this case that would be System.out.println(i); If that's the only thing skipped the loop should still run, but not display anything to the screen. Can someone explain what I'm missing here?
tri bui Level 6, Saigon, Viet Nam
26 May 2024
Your understanding of the `continue` statement is correct: it indeed causes the loop to immediately jump to the next iteration, skipping any code that follows it within the loop body. However, there is a specific reason your code fails to compile. In Java, the `continue` statement transfers control to the next iteration of the enclosing loop, bypassing any remaining statements in the current iteration. Consequently, any statements placed after `continue` are considered unreachable code because they will never be executed. This is against Java's rules, as the Java compiler flags unreachable code as an error. Let's break down what happens in your code: 1. The `for` loop starts with `i = 0`. 2. The loop condition `i < 10` is checked and found to be true. 3. The `continue` statement is executed, which causes the loop to immediately jump to the next iteration. 4. The `System.out.println(i);` statement is never reached because the `continue` statement has already passed control to the next iteration of the loop. Since the `System.out.println(i);` statement is never reachable due to the `continue` statement, the Java compiler generates an error indicating that there is unreachable code. To fix this issue, you need to remove the unreachable statement after the `continue`. For example:

for (i = 0; i < 10; i++){
    continue;
}
This code will compile and run without errors. The loop will iterate ten times, but nothing will be printed because the `continue` statement causes the loop to skip the `System.out.println(i);` statement (which has been removed). In summary, the problem in your original code is that the `continue` statement makes the subsequent `System.out.println(i);` statement unreachable, which causes a compilation error. Removing the unreachable statement or the `continue` statement will resolve the issue.
SvenskKung Level 10, United States of America, United States
13 October 2023
How are we supposed to learn from this with no feedback on what the right answers are at the end of the test?
Zdravcov Andrei Level 15, Romania
20 February 2023
Please provide an explanation to what the correct answer is or at least the correct answer
abhishe_kira Level 18, India Expert
26 June 2023
Bro in next level(level5) you will learn to use intellij Idea where you will have another option to download the solution so keep it up. oh! sorry you just hung up.
Mindaugas Level 23, Lithuania
6 July 2022
in first question all answers are wrong actually...
what??? Level 20, Taiwan, Province of China
19 August 2022
agree
23 July 2023
You are absolutely right
Anonymous #11576107 Level 6, -, Czechia
16 October 2024
🤯
Miles118 Level 13, Brindas, France
26 April 2022
What does the methode round does ? I know it rounds the number but to what value ?
Ali_Alshehri Level 6, Saudi Arabia
19 May 2022
round 3.9--> 4 if the number after coma above 5 will be round up if not round down ceil 3.1-->4 always up floor 3.2-->3 always down floor 3.9--> 3
tri bui Level 6, Saigon, Viet Nam
26 May 2024
Please read the example belows and you could understand immediately.

public class Main {
    public static void main(String[] args){
        int i = (int) Math.round(-9.9);
        int x = (int) Math.round(9.9);
        System.out.println(i); // -5
        System.out.println(x); // 5

        int y = (int) Math.ceil(-9.9);
        int j = (int) Math.ceil(9.9);
        System.out.println(y); // -4
        System.out.println(j); // 5
    }
}
Anonymous #10960894 Level 19, Arnold, United Kingdom
17 March 2022
I would love to be able to look back at the question and answers when I get something wrong, so I can see where I went wrong.
Saravanan H Level 15, San Jose, United States
16 March 2022
Please share the wrong answer explanation
Kiran Nyayapati Level 47, Hyderabad, India
25 February 2022
Can anyone tell why the answer to the below code (Qtn 8 from the Quiz) is "This code will not compile" ? for(int i = 0; i < 10; i++) { continue; System.out.println(i); }
Miles118 Level 13, Brindas, France
26 April 2022
Because it always skip an iteration before "print", so it will never compile.
Anonymous #11149518 Level 22, United States of America, United States
10 November 2022
I ran this in intellij and the code would not compile. After compiling I got an error saying 'java: unreachable statement'. The unreachable statement is 'System.out.println(i);'. If you get rid of 'System.out.println(i);' the code will run fine. This will not create an endless loop and will still iterate through the for statement as shown below: public class Solution { public static void main(String[] args) throws Exception { for(int i = 0; i < 10; i++) { continue; } System.out.println("hi"); } } This will print out 'hi'.
tri bui Level 6, Saigon, Viet Nam
26 May 2024
In Java, the `continue` statement transfers control to the next iteration of the enclosing loop, bypassing any remaining statements in the current iteration. Consequently, any statements placed after `continue` are considered unreachable code because they will never be executed. This is against Java's rules, as the Java compiler flags unreachable code as an error. Let's break down what happens in your code: 1. The `for` loop starts with `i = 0`. 2. The loop condition `i < 10` is checked and found to be true. 3. The `continue` statement is executed, which causes the loop to immediately jump to the next iteration. 4. The `System.out.println(i);` statement is never reached because the `continue` statement has already passed control to the next iteration of the loop. Since the `System.out.println(i);` statement is never reachable due to the `continue` statement, the Java compiler generates an error indicating that there is unreachable code. To fix this issue, you need to remove the unreachable statement after the `continue`. For example:

for (i = 0; i < 10; i++){
    continue;
}
This code will compile and run without errors. The loop will iterate ten times, but nothing will be printed because the `continue` statement causes the loop to skip the `System.out.println(i);` statement (which has been removed). In summary, the problem in your original code is that the `continue` statement makes the subsequent `System.out.println(i);` statement unreachable, which causes a compilation error. Removing the unreachable statement or the `continue` statement will resolve the issue.
Igor Level 5, Ukraine, Europe
20 February 2022
I just don't understand, what was wrong with my answer.