๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ java โ€บ loops in java (for, while, do-while)
Loops in Java (for, while, do-while) - Scaler Topics
April 20, 2024 - Looping Constructs in Java are statements that allow a set of instructions to be performed repeatedly as long as a specified condition remains true. Java has three types of loops i.e. the for loop, the while loop, and the do-while loop.
๐ŸŒ
W3Schools
w3schools.com โ€บ java โ€บ java_for_loop.asp
Java For Loop
Java Examples Java Videos Java Compiler Java Exercises Java Quiz Java Code Challenges Java Server Java Syllabus Java Study Plan Java Interview Q&A Java Certificate ... When you know exactly how many times you want to loop through a block of code, use the for loop instead of a while loop:
Discussions

Java understanding For loops
I just learnt about Looping, Pre and Post increment, but I think I didnโ€™t understand it. Can someone explain this code loop by loop. Thanks in advance. class Multi { public static void main(String[] args) { for(int x = 0; x 2; y--) { System.out.println(x + " " + y); } if(x == 1) { x++; } ... More on forum.freecodecamp.org
๐ŸŒ forum.freecodecamp.org
0
0
December 16, 2019
ELI5: Understanding For Loops in Java
we have 3 loop types: whie (condition) { // do this code } do { // run this code at least once } while (condition); // determines whether the loop should be ran more than once for (declaration; condition; iteration_step) { // do this code } people will often use i for a short lived loop variable - think of it as meaning iteration or something like that for (int i = 0; i < 5; i++) { // some code here } in this for loop we are declaring an int called i, saying to run the loop whilst i is less than 5 and the i++ is done once the body of the loop has been ran More on reddit.com
๐ŸŒ r/learnprogramming
5
0
April 14, 2021
Java: For loop and If algorithm - Stack Overflow
You could introduce state and remember whether you have found the book or not. If you're not using Java 1.4 or earlier, you could also use the foreach loop syntax: More on stackoverflow.com
๐ŸŒ stackoverflow.com
java - How does a for loop work, specifically for(;;)? - Stack Overflow
The initialization, condition, and increment statements are all optional, so without any of them, this will always loop again (unless a break is hit or some other construct interacts with it). Although I'm unsure about Java, this question explains how in .Net your empty for and a while (true) ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ difference-between-for-loop-and-enhanced-for-loop-in-java
Java for loop vs Enhanced for loop - GeeksforGeeks
July 23, 2025 - The traditional for loop is a versatile structure used to iterate over a range of values, arrays, or other data structures. It provides complete control over the iteration process for any tasks which need to do multiple times.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ java-for-loop-with-examples
Java For Loop - GeeksforGeeks
1 week ago - The for loop in Java is a control flow statement used to execute a block of code repeatedly based on a condition.
๐ŸŒ
ScholarHat
scholarhat.com โ€บ home
Understanding for loop in Java (Different Types and Examples)
September 5, 2025 - Let's try to understand their usage with Examples in Java Online Editor: The simple for loop in Java repeats the execution of block of code for a known and fixed number of times.
๐ŸŒ
Medium
medium.com โ€บ @AlexanderObregon โ€บ demystifying-the-for-loop-in-java-a-beginners-guide-eaaff1ad6434
Exploring the โ€œforโ€ Loop in Java โ€” A Brief Beginner's Guide
April 20, 2024 - Before we wrap up, letโ€™s take a quick look at nested โ€œforโ€ loops. A nested loop is a loop within a loop. In Java, you can nest any type of loop inside any other type. For the โ€œforโ€ loop, this can be particularly useful when working with multi-dimensional arrays.
๐ŸŒ
Zero To Mastery
zerotomastery.io โ€บ blog โ€บ enhanced-for-loop-java
How To Use Enhanced For Loops In Java (aka 'foreach') | Zero To Mastery
January 26, 2024 - No prior programming knowledge is required. Just a willingness and enthusiasm to learn Java. With that out of the way, letโ€™s get into this guide! The enhanced foreach loop is a way of iterating through elements in arrays and collections in Java.
Find elsewhere
๐ŸŒ
DataFlair
data-flair.training โ€บ blogs โ€บ java-for-loop
Java For Loop Syntax and Example - DataFlair
May 8, 2024 - We will be learning about for loops Currently in Loop. value of i = 0 Currently in Loop. value of i = 1 Currently in Loop. value of i = 2 Currently in Loop. value of i = 3 Currently in Loop. value of i = 4 Currently outside Loop. value of i = 5 ยท package com.dataflair.javaforloop; import java.io.*; public class ArrayTraverse{ public static void main(String []args) { int arrp[] = {1,2,3,4,5,6}; for(int i=0;i<arrp.length;i++) { System.out.println("The "+(i+1)+" element is "+arrp[i]); } } }
๐ŸŒ
DataCamp
datacamp.com โ€บ doc โ€บ java โ€บ java-for-loop
Java For Loop
Java keywordsIntroduction To JavaJava File HandlingJava Language BasicsJava ArraysJava Object-Oriented Programming ... The for loop in Java is a control flow statement that allows code to be executed repeatedly based on a given boolean condition.
๐ŸŒ
freeCodeCamp
forum.freecodecamp.org โ€บ t โ€บ java-understanding-for-loops โ€บ 333711
Java understanding For loops - The freeCodeCamp Forum
December 16, 2019 - I just learnt about Looping, Pre and Post increment, but I think I didnโ€™t understand it. Can someone explain this code loop by loop. Thanks in advance. class Multi { public static void main(String[] args) { for(int x = 0; x 2; y--) { System.out.println(x + " " + y); } if(x == 1) { x++; } } } } the output for the above code is : 0 4 0 3 1 4 1 3 3 4 3 3 When I tried to find the output I got confused in th...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ loops-in-java
Java Loops - GeeksforGeeks
Loops in programming allow a set of instructions to run multiple times based on a condition. In Java, there are three types of Loops, which are explained below: The for loop is used when we know the number of iterations (we know how many times ...
Published ย  August 10, 2025
๐ŸŒ
Oracle
docs.oracle.com โ€บ javase โ€บ tutorial โ€บ java โ€บ nutsandbolts โ€บ for.html
The for Statement (The Javaโ„ข Tutorials > Learning the Java Language > Language Basics)
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases. The for statement provides a compact way to iterate over a range of values. Programmers often refer to it as the "for loop" because of the way in which it repeatedly ...
๐ŸŒ
W3Schools
w3schools.com โ€บ java โ€บ java_for_loop_nested.asp
Java Nested Loops
Java Examples Java Videos Java Compiler Java Exercises Java Quiz Java Code Challenges Java Server Java Syllabus Java Study Plan Java Interview Q&A Java Certificate ... It is also possible to place a loop inside another loop. This is called a nested loop. The "inner loop" will be executed one time for each iteration of the "outer loop":
๐ŸŒ
TechVidvan
techvidvan.com โ€บ tutorials โ€บ java-for-loop
Java For Loop - An Ultimate Guide to Master the Concept - TechVidvan
March 28, 2020 - The for-each loop works on the basis of elements, not the index of the element. It one by one returns the elements in the defined variable. Enhance your knowledge by exploring Arrays in Java in detail with Techvidvan.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnprogramming โ€บ eli5: understanding for loops in java
r/learnprogramming on Reddit: ELI5: Understanding For Loops in Java
April 14, 2021 -

As the title said, can you guys help me understand the for loops in java like I'm five. I've been trying to learn java but when it comes to for loop, I'm having a hard time understanding it. Especially when my professor is using single letters like for int i, x, y, and such something like that. Whenever I'm coding, I always use proper names so I don't get confused with the variables. But I still get confused using for loops;-; so I mostly prefer using the do.. while or while loops. But I want to learn to understand it because I noticed my prof always uses it and as well as ytber programmers.

๐ŸŒ
Simplilearn
simplilearn.com โ€บ home โ€บ resources โ€บ software development โ€บ understanding for loop in java with examples and syntax
Understanding For Loop in Java With Examples and Syntax
July 31, 2025 - Java provides three types of loops, i.e., โœ“ for loop โœ“ while loop โœ“ do-while loop. In this tutorial, you will learn all about for loop in Java. Start now!
Address ย  5851 Legacy Circle, 6th Floor, Plano, TX 75024 United States
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ java-for-loop-example
Java For Loop Example
February 7, 2023 - You can use loops in programming to carry out a set of instructions repeatedly until a certain condition is met. There are three types of loops in Java: for loop. while loop. do...while loop. In this article, we'll focus on the for loop, its synt...
Top answer
1 of 6
6

You should loop through the array and use an index / boolean flag to store whether or not the book is found. Then print the message in the end, based on the index / flag value.

int foundAtIndex = -1;
for(int i = 0; i < bookObj.length; i++) {
    if(bookObj[i].getName().equals(input)) {
        foundAtIndex = i;  // store the actual index for later use
        break;             // no need to search further
    }
}
if(foundAtIndex >= 0)
    System.out.println("Book Found!");
else
    System.out.println("Book not Found!");

Alternatively (unless your assignment specifically requires using an array) you should prefer a Set, which can do the search for you with a single call to contains().

How should I think of it in Object Oriented way?

When looking at a single method, there is not much difference between procedural and OO style. The differences start to appear at a higher level, when trying to organize a bunch of conceptually related data and methods that operate on these.

The OO paradigm is to tie the methods to the data they operate on, and encapsulate both into coherent objects and classes. These classes are preferably representations of important domain concepts. So for your book store, you may want to put all book related code into your Book class. However, the above search method (and the collection of books it operates on) is not related to any particular book instance, so you have different choices:

  • put both the collection of books and the search method into Store (probably as regular members), or
  • put them into Book as static members.

The first choice is more natural, so I normally would prefer that. However, under specific circumstances the second option might be preferable. In (OO) design, there are hardly ever clean "yes/no" answers - rather tradeoffs between different options, each having their own strengths and weaknesses.

2 of 6
2

You could introduce state and remember whether you have found the book or not.

If you're not using Java 1.4 or earlier, you could also use the foreach loop syntax:

boolean bookFound = false;
for(Book currentBook : bookObj) {
     if(currentBook.getName().equals(input))
     //TODO: see above
}

Also, I would suggest looking into the Collections library, and replace your array with a list or set:

Set<Book> books = new HashSet<Book>();
books.put(new Book("Game Over"));
books.put(new Book("Shrek")); 
books.put(new Book("Ghost"));

And, while were at it, you could also think about when two books are equal and override equals() and hashCode() accordingly. If equal() would be changed to check the title, you could simply use books.contains(new Book(input)); and have the libraries do the work for you.

๐ŸŒ
CodeWithHarry
codewithharry.com โ€บ tutorial โ€บ java-for-loop
for Loop | Java Tutorial | CodeWithHarry
testCondition: It checks the testing condition of the loop after each iteration and returns a Boolean value, which determines if the loop should be terminated or not. Increment/decrement: It is used to increment or decrement the variable after each iteration. ... public class ForLoop1 { public static void main(String[] args) { for(int i = 1; i <= 10; i++) { System.out.println("2 * " + i + " = " + 2 * i); } } }
Top answer
1 of 6
103

A for loop in java has the following structure -

for (initialization statement; condition check; update)
    loop body;

As you can see, there are four statements here -

  1. Initialization statement: This statement is executed only once, when the loop is entered for the first time. This is an optional statement, meaning you can choose keep this field blank. It is generally used for some initialization purpose.
  2. Conditional check: This statement is probably the most important one. It checks to verify whether or not certain expression evaluates to true. If it is, then the loop execution continues. You can choose to keep this field empty, which will be evaluated to true.
  3. Update: This statement list is executed from left to right, typically used to increment/decrement some variable.
  4. loop body: The body of the loop, which will be executed again and again based on the conditional check's truth value.

Basically this is how the execution follows - first, when the loop is entered for the first time, the initialization statement is executed once. Then the conditional check is executed to see if it evaluated to true. If it is, then the the loop body is executed, otherwise the loop execution is finished. After that, the Update statement(s) is(are) executed. Next, the conditional check is executed again, and if it evaluates to true, then again the loop body is executed, then update statement is executed, then again the conditional check....you get the picture.

Now about your for( ; ; ) syntax. It has no initialization statement, so nothing will be executed. Its conditional check statement is also empty, which means it evaluates to true after that the loop body is executed. Next, since the update statement is empty, nothing is executed. Then the conditional check is performed again which will again evaluates to true and then this whole process will again repeat.

So you see, this is basically an infinite loop which has no initialization statement, whose conditional check will always evaluates to true, and which has no update statement. This is equivalent to -

while(true)
{
    .....
}

which is another popular loop construct in java.

When you use an infinite loop like this, it's important pay attention to the breaking condition as in most cases you can't let a loop to run indefinitely. To break out of these kinds of loops, you can use the break statement. The structure is as follows -

if(some_condition_is_true)
    break;        // This will cause execution to break out of its nearest loop

or

if(some_condition_is_false)
    break;
2 of 6
41

This is the same as:

while(true) {
  //Some Stuff
}

Basically, an alternate syntax for an infinite loop.