JavaRush /Courses /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.

Loops in Java

In this article, you will find a simple explanation of what loops are and what kinds of loops exist. And you'll also look at the implementation of several types of loops:

  • while — a loop with a precondition
  • do..while — a loop with a postcondition
  • for — a loop with a counter
  • for each — a kind for loop used to iterate over a collection of elements

How real numbers are structured

You already know what real numbers are: positive and negative numbers that include a fractional part. This is a massive topic in itself, and you can dig deep into it, but there is no need to panic. In this article, we'll talk only about the most important stuff: how real numbers are stored in memory and how to compare them. And we will also consider examples of why sometimes an error can creep in even in the simplest of comparisons.

How to use a 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
Survey/quiz
Loops, 4 level, 8 lesson
Unavailable
Loops
Loops
Comments (4)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Anonymous #3248283 Level 9 Expert
31 March 2023
There's an error: The correct answer is definitely "The code will run fine, but it will not display anything", but it is evaluated as wrong! Why? - the "System.out.println(i)" statement will never be executed, because it stays after "continue;", and the loop will go on until the "i" variable reaches a value of 10, at which point the loop will terminate. Nothing will be printed in result.
hidden #3342104 Level 19
27 August 2023
You're wrong. Java Compiler will throw an "unreachable statement" error for the "System.out.println(i)" statement. Accordingly, the response is "This code will not compile".
25 December 2023
Lol, was that mentioned somewhere? This looks like a bizarre behavior, compared to other languages.
20 October 2024
why, it is cool that compiler can analyze the code at some level and prevent us from making such mistakes ;) But yeah, I don't remember reading about it explicitly in the course.