Java/J2EE and SOA (349 Blogs) Become a Certified Professional
AWS Global Infrastructure

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (16 Blogs)
  • Comprehensive Java Course (4 Blogs)
  • Java/J2EE and SOA (346 Blogs)
  • Spring Framework (8 Blogs)
SEE MORE

What is for loop in java and how to implement it?

Last updated on Aug 28,2024 14.1K Views

1 / 5 Blog from Control Statements

While programming, if a situation arises where you specifically know how many times you want to iterate a particular block of statements in your code, go for a “for” loop. In this article let’s learn about how to implement for loop in Java Programming Language.

The topics covered in this article are as follows:

Let’s begin!

What is for loop?

Programmers usually use loops to execute a set of statements. For loop is used when they need to iterate a part of the programs multiple times. It is particularly used in cases where the number of iterations is fixed!

For a better understanding, let me give you a pictorial representation!

Flow diagram

For-In Loop - Swift Tutorial - Edureka

Here, after initialization, the condition that you have assigned in the code is scanned, in case the condition is true, it would increment/decrement (according to your code) the value, and again iterate the code according to the condition that you have assigned. But, if your condition is false, it will exit the loop.

After this theoretical explanation, let me show you the syntax of the for loop!

Syntax


for (statement 1; statement 2; statement 3) {
// code block to be executed
}

The syntax is pretty simple. It goes as follows
Statement 1: condition before the code block is executed
Statement 2: specifies the condition for execution of the code
Statement 3: condition once the code has been executed

To make things clearer, let us implement the above-explained syntax in a Java code.

Example of for loop

The code written below depicts how for loop is implemented in Java Language

public class MyClass {
{
public static void main(String[] args) {
{for (int i = 0; i < 5; i++) {
System.out.println(i);
}
}
}}

Output:
0
1
2
3
4

I have taken a simple code to get you all acquainted with the concept of for loop. Inside the for loop, there are three statements that I have talked about in the previous segment. I hope you can now relate to them easily!

  • Firstly, Int i=0, is the initialization of an integer variable whose value has been assigned to 0.
  • Secondly, i<5 is the condition that I have applied in my code
  • Thirdly, i++, means that I want the value of my variable to be incremented.

Related Learning: Top Java Interview Questions

After understanding the working of for loop, let me take you to another concept, that is Java nested for loop!

Java nested for loop

If you have a for loop inside a for loop, you have encountered a Java nested for loop. The inner loop executes completely when the outer loop executes.

I am presenting an example to show you the working of a Java nested for loop.

Example

A Java code for a nested for loop:

public class Example{
public static void main(String[] args) {
for(int i=1;i<=3;i++){
for(int j=1;j<=3;j++){
System.out.println(i+" "+j);
}
}
}
}

Output:
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3

Now that you have understood the concept of a nested for loop, let me show you a very famous example that you might have heard of! The pyramid examples!

Pyramid Example: Case 1

public class PyramidExample {
public static void main(String[] args) {
for(int i=1;i<=5;i++){
for(int j=1;j<=i;j++){
System.out.print("* ");
}
System.out.println();//new line
}
}
}

Output:

*
* *
* * *
* * * *
* * * * *

Moving on with next example.

Pyramid Example: Case 2

package MyPackage;
public class Demo {
public static void main(String[] args) {
int term=6;
for(int i=1;i<=term;i++){ for(int j=term;j>=i;j--){
System.out.print("* ");
}
System.out.println();//new line
}
}
}

Output:

* * * * * 
* * * * 
* * * 
* * 
*

I am sure that you would be familiar with these two patterns.

This brings us to the end of this ‘For Loop in Java’ article. I hope the concept of “for loop in Java” is clear to you now. We will keep digging the Java world together. Stay tuned!

Make sure you practice as much as possible and revert your experience.  

Check out Java Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners worldwide. We are here to help you with every step on your journey to becoming a successful programmer, and we have developed a curriculum designed for students and professionals who want to become Java developers. 

Got a question for us? Please mention it in the comments section and we will get back to you as soon as possible.

Upcoming Batches For Java Course Online
Course NameDateDetails
Java Course Online

Class Starts on 28th September,2024

28th September

SAT&SUN (Weekend Batch)
View Details
Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

What is for loop in java and how to implement it?

edureka.co