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

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...
Array An array is a collection of similar kind of data elements stored in adjacent memory locations and are referred to by a single array-name. The first el...
Compile C/C++ program (Dev-C/C++) what is Dev-C/C++? Dev-C/C++, developed by Bloodshed Software, is a fully featured graphical IDE(Integrated Development Environment),which ...
Introduction to JVM Introduction to JVM JVM stands for Java Virtual Machine. It works as an engine that derives the Java Code. The JVM does not understand Java ty...
Control Flow Statements | Swift tutorial Control Flow : Swift tutorial Swift Provides diffrent type of controll flow statements like: Loops Statements : Loops are used for perform the t...
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...

Leave a Reply

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