Java programming
30.6K subscribers
131 photos
12 files
20 links
Download Telegram
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.
The code written below depicts how for loop is implemented in Java Language
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.

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:
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
Moving on with next example.

Pyramid Example: Case 2
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!
What is a While Loop in Java and how to use it?

Java
language offers you to work with several loops. Loops are basically used to execute a set of statements repeatedly until a particular condition is satisfied. Here, I will tell you about the ‘while’ loop in Java. The topics included in this article are mentioned below:

What is while loop in Java?
- Syntax of while loop
- Practical Demonstration
What is an Infinite while loop?
- Syntax of infinite while loop
- Practical Demonstration

Let’s begin!
What is a while loop in Java?

The Java while loop is used to iterate a part of the program again and again. If the number of iteration is not fixed, then you can use while loop.

A pictorial representation of how a while loop works:
In the above diagram, when the execution begins and the condition returns false, then the control jumps out to the next statement after the while loop. On the other hand, if the condition returns true then the statement inside the while loop is executed.

Moving on with this article on While Loop in Java, Let’s have a look at the syntax:
Now that I have shown you the syntax, here is an example:
Share our channel
What is for-each loop in Java?

The
for-each loop in Java is generally used for iteration through the array elements in different programming languages. Java 5 introduced this for-each loop, which is also called as an enhanced for-each loop. So, in this article, I’ll help you guys get a proper gist about how for-each loop in Java works.

Importance of for-each loop
Using a for-each loop, you can traverse a given array. It starts with the keyword for followed by the condition.

This for statement is a very commonly used looping statement. It includes initialization of an expression that specifies an initial value for an index, and the following condition expression determines whether the loop is continued or not and the last iteration expression allows the index to be modified at the end of each pass. It is known as the for-each loop because it traverses each element one by one.

Also, it is recommended to use the for-each loop in Java for traversing the elements in the array and collection because it makes the code readable really good.
Now let’s take a look at the syntax of this for-each loop.