Java For Loop with Example | Syntax | Break | Enhanced for loop

Loop condition statement in programming is used to repeat a block of code (statement) until a specific condition. Java for loop is one of the ways to do loop execution in the program, there is many.

A For Loop is break one a given condition is false.

Java For Loop with Example Syntax Break Enhanced for loop

Syntax

Here Java for loop syntax code.

How for loop works?

In mind, if you got the question of how java for loop works, then there is 3 stage of it.

  • Initialization – This step executes first and only once in for loop. Declare and initialize variables for loop and ends with a semicolon (;).
  • Test Expression – This is a boolean expression, will execute if it’s true else pass them for a loop.
  • Update – Here is a loop that comes after a complete cycle of body and updates the value of variables (Increase or decrease value, etc).

With an example, you will get more clarity.

for Loop Flowchart Diagram

how java for Loop Flowchart Diagram example

Java for loop example

Here is an example program code in java with the output. This program a print 1 to 10 number in java,

Here is i is an Initialization variable with an assigned value of 1. The loop executes until i <= 10 is true. And “i” value increased by 1 every time as using unary operator (i++)

Output: 1
2
3
4
5
6
7
8
9
10

Java for loop array

A for loop is used with the Array is accessing an element(item) one by one. Here is an example of how to iterating over an array used for loop.

Output: 1 2 3 4 5

Java for loop list

You have to use a for-each statement for a list of data structure in java.

QA: What is an Enhanced for loop in java?

A Java for-each Loop loop is an Enhanced and Advanced version of Java for a loop. If you are working with array and collection then it’s mainly using for loop condition statements.

QA: What is nested for loop in java?

A Loop inside Loop is called a nested loop. So if there is for loop is exists inside another loop is called a Nested for loop in java.

Here is a simple program example of a Nested Loop. Printing a pattern with Nested for a loop.

Output:

What is nested for loop in java example

QA: How to Java for loop break (not by condition false)?

You can break and loop by using Break Keyword in Java, let’s see this simple example for it. Using an if condition when the value matched then breaks the loop otherwise it will break in a first-time loop cycle.

Output: 20
21

Do java loop programs for practice, it’s required mostly places in different ways. So you must have practical knowledge of it. If any doubt and suggestions, do comment.

Note: This example (Project) is developed in IntelliJ IDEA 2018.2.6 (Community Edition)
JRE: 11.0.1
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.14.1

Java version 11

All Example of Java program code are in Java 11, so it may change on different from 9 or 10 or upgraded versions.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.