Home > Java > javaTutorial > body text

Loops in Java Programming

WBOY
Release: 2024-08-30 15:24:31
Original
1173 people have browsed it

‘Coding’ in simple definitions means a way to tell the computer what to do; however, it is not that easy the way it might seem, but as of now, we will not focus on the later (means easy or tough) part. In this topic, we are going to learn about Loops in Java Programming.

ADVERTISEMENT Popular Course in this category PROGRAMMING LANGUAGES - Specialization | 54 Course Series | 4 Mock Tests

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Computers can only understand ‘ON’ and ‘OFF’ types of data, which are commonly known as ‘Binary’ values. Binary codes consist of (0’s and 1’s) and are understood by computers worldwide. But the problem is we cannot write trillions of 0’s and 1’s and that to in order just to make the computer understand what we ask them to compute. This is where programming languages or coding comes into the picture.

With this, we have successfully filtered out our understanding to the coding level, and now, since we know what ‘Coding’ does and why we code, we have to further tap down to the ‘Loops’ level, which is what the title of the discussion is?

There are several PL across us; many of them are for web development, others for desktop application development, some are known as high, and some are known as low-level PL. All these programming languages have something in common, i.e. “LOOPS”.

Moving deeper into the discussion, Loops are present in almost all the programming languages; let us see what advantages they hold for the developers –

  • These are ‘REUSABLE’
  • They reduce the size of the ‘CODING.’
  • They make an easy flow of the ‘CONTROL.’
  • They tend to reduce the ‘COMPLEXITY.’

Loops in JAVA Programming are meant to solve code complexity, are usable and are meant to be used by the developers to reuse the codes as per the requirement.

Types of Loops in Java

The types of loops in java are as follows:

In JAVA, loops are iterative statements. These statements help the developers (or the users) to iterate the program codes, or a group of codes runs multiple times (as per the need).

In JAVA, there are mainly 3 main categories of loops, namely

  • FOR LOOP
  • WHILE LOOP
  • DO-WHILE LOOP

Before we dig deep into these LOOPS, we want our readers to understand one thing (this holds value for all three loops). Whether it is a FOR, WHILE or DO WHILE, all have Starting, Body and lastly, the Destination. Without a do let us see them one by one –

1. For Loop

If you are a developer and want to execute or run a part of your program to a specific number of times before you get the final outcome (or result), you will go with the FOR LOOP. Remember, you will use the ‘For loop’ only when you know clearly the number of times you want to execute the statements. The FOR LOOP will repeat itself until it has a value equals to “TRUE’.

Let us see the flowchart of it for better and clear understanding –

Loops in Java Programming

Syntax 

for(initialization condition; testing condition; increment/decrement)
{
statement(s)
}
Copy after login

Explanation 

All the 3 parameters (i.e. initialize, condition and increment/decrement) stays in one single statement in FOR loop. Initialization means to provide the initial values of the Variable. The condition means the value we want to check in the program.Increment/Decrement means what you want to have in the loop; the value will either be increased or decreased accordingly. The body of the loops starts with the curly brackets and ends with curly brackets (}) and contains the statements which will be executed using the loop.

Example

Our intention is to print all the even numbers between 1 to 100.

class Test
{
public Static Void Main (String ar [])
{
int no; for (no = 2; no <=100; no = no+2)
{
System.out. print ln(no);
}
}
}
Copy after login

Output for this program will be 2,4,6,8,10,12…………………..96,98,100

2. While Loop

Only when a certain amount of statements are needed to get executed repeatedly until the condition gets fulfilled, we need the? Here, unlike FOR loop, the condition is checked first before the execution of the statement.

Let us see the flowchart of it for better and clear understanding –

Loops in Java Programming

Syntax 

while (boolean condition)
{
loop statements
}
Copy after login

Explanation 

While loop starts with the applying conditions statements placed inside the brackets, these also hold the loop statements within the curly brackets. As we have said that the WHILE loop runs until the value holds true value.

Example 

We want to print all the odd numbers between 1 to 100.

class Test
{
public static void main (String ar[])
{
int no = 1;
while (no<100)
{
System.out.print (no);
No = no +2;
} } }
Copy after login

Output for this program will be 1,3,5,7,9,11………………………………………..97,99

3. Do While

There is not much difference between the WHILE and DO WHILE loops; the difference lies in their statement validation. In DO WHILE, the condition is checked after execution of the block of statements; hence we can say in DO WHILE, the statements are at least executed once.

Let us see the flowchart of it for better and clear understanding –

Loops in Java Programming

Syntax 

do
{
statements..
}
while (condition);
Copy after login

Explanation 

There is no condition checking in the DO WHILE loop for the first time; later, the condition is checked for TRUE or FALSE. If it is TRUE, then the next iteration of loops start; otherwise, the loop terminates itself.

Example

class Test
{
public Static Void Main (String ar[])
{
int no = 2;
do
{
System.out.print (no);
No = no + 2;
}
while (no<=100);
}}}
Copy after login

Output for this program will be – 2,4,6,8,10…………….98,100

Comparison Between Different Types of Loops

The comparison between different types of loops are as follows:

1) Declaration

For Loop

for (initialization; condition; iteration){
//body of 'for' loop
}
Copy after login

While Loop

Statements; //body of loop
}
Copy after login

Do While

do
{
Statements
}
while (condition);
Copy after login

2) We use FOR loop if the user knows the time of iteration, whereas the WHILE and DO WHILE loop is preferred when the number of iteration is not known.

3) Regarding the conditional statement in FOR loop, it will operate infinite time, whereas for WHILE and DO WHILE the absence of conditional statement will give ‘Compilation Error’.

Conclusion

The loops concept is crucial for any users who belong to development; if they are freshers and prepare for exams or interviews, they must be well-rehearsed with the Loops concepts. We have covered all the crucial aspects of Loops, and this is how things work in Loops. These 3 Loops are the most crucial ones, and the rest are improvised on them. If you have a good grip over these, then the rest will be quite easier to understand.

The above is the detailed content of Loops in Java Programming. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template