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

Firebase Push notification | Swift tutorial Push Notification by firebase in IOS.For Firebase Push notification we need to have paid apple developer account. You need to Enroll into the Apple D...
How to use Flutter most usable Widgets | Flutter t... We need some basic widgets for creating our first flutter app. Widgets describe what their view should look like given their current configuration and...
First Java Program First Java Program NOTE: You need to have java installed on your system. Step 1: Open a text editor, like Notepad on windows and TextEdit on Mac. W...
Introduction To C A Brief History Of C   C was invented and first implemented by Dennis Ritchie on a DEC PDP-11 that used the Unix operating system. C is the r...
E-Mail Format Formal Email / Letter Format A formal email is in general sent to someone you don’t know well or to someone who’s in authority. Examples of someone w...
Loop Control Statement Loop control statements are used when a section of code may either be executed a fixed number of times. C gives you a choice of three types of loop st...

Leave a Reply

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