Loops in java

Loops in JAVA

  • Loops are used to execute a set of statements repeatedly until a particular condition is satisfied.
  • Loop is used in programming to repeat a specific block of code.

In Java we have three types of basic loops-
1). FOR Loop
2). WHILE Loop
3). DO-WHILE Loop

For Loop

  • 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 loops until a particular condition is satisfied.
Syntax

1
2
3
4
<span class="hljs-keyword">for</span>(initialization; condition ; increment/decrement)
{
   statement(s);
}

Flow of Execution of the for Loop

As a program executes, the interpreter always keeps track of which statement is about to be executed. We call this the control flow, or the flow of execution.

First step:

In for loop, initialization happens first and only one time, which means that the initialization part of for loop only executes once.

Second step:

Condition in for loop is evaluated on each iteration, if the condition is true then the statements inside for loop body gets executed. Once the condition returns false, the statements in for loop does not execute and the control gets transferred to the next statement in the program after for loop.

Third step:

After every execution of for loop’s body, the increment/decrement part of for loop executes that updates the loop counter.

Fourth step:

After third step, the control jumps to second step and condition is re-evaluated.

Example:

1
2
3
4
5
6
7
class ForLoopExample {
    public static void main(String[] args){
         for(int i=1; i&lt;=10; i++){
              System.out.println("Count is: " + i);
         }
    }
}
Output:

Count is: 1
Count is: 2
Count is: 3
Count is: 4
Count is: 5
Count is: 6
Count is: 7
Count is: 8
Count is: 9
Count is: 10

For loop example to iterate an array:

Here we are iterating and displaying array elements using the for loop.


1
2
3
4
5
6
7
8
9
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ForLoopExample1</span> </span>{
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String args[])</span></span>{
         <span class="hljs-keyword">int</span> arr[]={<span class="hljs-number">2</span>,<span class="hljs-number">11</span>,<span class="hljs-number">45</span>,<span class="hljs-number">9</span>};
         <span class="hljs-comment">//i starts with 0 as array index starts with 0 too</span>
         <span class="hljs-keyword">for</span>(<span class="hljs-keyword">int</span> i=<span class="hljs-number">0</span>; i&lt;arr.length; i++){
              System.out.println(arr[i]);
         }
    }
}
Output:

2
11
45
9

While Loop

In while loop, condition is evaluated first and if it returns true then the statements inside while loop execute. When condition returns false, the control comes out of loop and jumps to the next statement after while loop.

Syntax:

1
2
3
4
<span class="hljs-keyword">while</span>(condition)
{
   statement(s);
}
Example:

1
2
3
4
5
6
7
8
9
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">WhileLoopExample</span> </span>{
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String args[])</span></span>{
         <span class="hljs-keyword">int</span> i=<span class="hljs-number">10</span>;
         <span class="hljs-keyword">while</span>(i&gt;<span class="hljs-number">1</span>){
              System.out.println(i);
              i--;
         }
    }
}
Output:

10
9
8
7
6
5
4
3
2

Note:

The important point to note when using while loop is that we need to use increment or decrement statement inside while loop so that the loop variable gets changed on each iteration, and at some point condition returns false. This way we can end the execution of while loop otherwise the loop would execute indefinitely.

While Loop example to iterate an array:

Here we are iterating and displaying array elements using while loop.


1
2
3
4
5
6
7
8
9
10
11
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">WhileLoopExample3</span> </span>{
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String args[])</span></span>{
         <span class="hljs-keyword">int</span> arr[]={<span class="hljs-number">2</span>,<span class="hljs-number">11</span>,<span class="hljs-number">45</span>,<span class="hljs-number">9</span>};
         <span class="hljs-comment">//i starts with 0 as array index starts with 0 too</span>
         <span class="hljs-keyword">int</span> i=<span class="hljs-number">0</span>;
         <span class="hljs-keyword">while</span>(i&lt;<span class="hljs-number">4</span>){
              System.out.println(arr[i]);
              i++;
         }
    }
}
Output

2
11
45
9

do-While Loop

  • The do-while loop executes a block of code while a boolean expression evaluates to true. It checks the boolean expression after each iteration. It terminates as soon as the expression evaluates to false.
  • First, the statements inside loop execute and then the condition gets evaluated, if the condition returns true then the control gets transferred to the “do” else it jumps to the next statement after do-while.
Syntax:

1
2
3
4
do
{
   statement(s);
} <span class="hljs-keyword">while</span>(condition);
Example:

1
2
3
4
5
6
7
8
9
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">DoWhileLoopExample</span> </span>{
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String args[])</span></span>{
         <span class="hljs-keyword">int</span> i=<span class="hljs-number">10</span>;
         do{
              System.out.println(i);
              i--;
         }<span class="hljs-keyword">while</span>(i&gt;<span class="hljs-number">1</span>);
    }
}
Output:

10
9
8
7
6
5
4
3
2

Do-While loop example to iterate an array:

Here we are iterating and displaying array elements using do-while loop.


1
2
3
4
5
6
7
8
9
10
11
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">DoWhileLoopExample2</span> </span>{
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String args[])</span></span>{
         <span class="hljs-keyword">int</span> arr[]={<span class="hljs-number">2</span>,<span class="hljs-number">11</span>,<span class="hljs-number">45</span>,<span class="hljs-number">9</span>};
         <span class="hljs-comment">//i starts with 0 as array index starts with 0</span>
         <span class="hljs-keyword">int</span> i=<span class="hljs-number">0</span>;
         do{
              System.out.println(arr[i]);
              i++;
         }<span class="hljs-keyword">while</span>(i&lt;<span class="hljs-number">4</span>);
    }
}
Output:

2
11
45
9

Related Post

How to install flutter | Flutter tutorial Flutter is the google’s mobile app SDK for develop the high-quality native app for iOS and android. We can develop the app by use of DART and javascri...
Dart Libraries | Dart Tutorial Dart Libraries Dart has the following libraries, which are included in all Dart platforms: dart:core This library is automatically imported into ev...
Getting started with C Getting started with C : If we want to communicate with our computer it is very much needed that our computer understands what we want to say to it. ...
Dart Introduction | Dart programming language Dart Introduction Dart is a scalable language that you can use to write simple scripts or full featured apps. Whether you’re creating a mobile app, w...
Create UITableView (Table View) By the use of stor... How to use UITableView in Above IOS 8 and Swift (UITableView) In this tutorial we are study how to use the TableView in Swift. One of the most common...
Asset Color Catalog | Swift Tutorial Asset Color Catalog is the feature of XCode 9. BY the use of this feature we can maintain the colors easily and avoid the mistakes. We can create the ...

Leave a Reply

Your email address will not be published. Required fields are marked *

Get more stuff like this
in your inbox

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.