Let's learn java programming language with easy steps. This Java tutorial provides you complete knowledge about java technology.

Friday 6 January 2017

Loop

 

Q.Why use loops in java?

Ans. It is used to repeat the statements in java and less coding, time saving. When you need to execute a block of code several number of times then you need to use looping concept in java.


Advantage of loops in java

  • Reduce the length of the code
  • Saving time
  • Take less memory space


There are some loops in java

  • for loop
  • while loop
  • do while loop
  • for each loop

Loop


for loop in java

It is used to repeat a task set or fixed numbers of times, where the number of repetitions are fixed and consists of three parts: variable declaration or initialization, condition, increment/decrement  statement.

Syntax:

for(Initialization; condition; increment/decrements)
{
//Statements ;
}

Initialization: initialization step execute first and this step is execute on only once when we are entering into the loop first time. Initialization step allows to declare and initialize any loop control variables.

Condition: condition is the second step after initialization step. If condition of second steps is true, the body of the loop is executed again and again, if it is false then the body of the loop does not execute and flows of loop control goes outside of the for loop.

Increment or Decrements: when the initialization and condition steps is execute then increment or decrements step is execute. This statement allows to update any loop control variables.

For example: 

class ForLoop
{
public static void main(String...s)
{
for(int i = 1; i<=10; i++)
{
System.out.println(i);
}
}
}

output:1
             2
             3
             4
             5
             6
             7
             8
             9
            10


while loop in java

It is also used to repeat a task number of time but where  the number of repeatitions are not fixed. The Statements inside the loop are executed while certain condition is true.

Syntax:

while(condition)
{
//Statements ;
}

For example:

class  WhileLoop
{
public static void main(String...s)
{
int a =1;
while(a<=5)
{
System.out.println(i);
i++
}
}
}

output: 1
              2
              3
              4
              5

do while loop in java


Similar to the while loop except that the statements are executed at least once because condition is checked after loop body.

Syntax:

do

{
//Statements;
}
while(condition);

For example:

class DowhileLoop
{
public static void main(String...s)
{
int a = 10;
do
{
System.out.println(a);
a++;
}
while(a < =20);
}
}

output: 10
              11
              12
              13
              14
              15
              16
              17
              18
              19
              20


for-each or enhanced for loop in java

This is mainly used for Arrays or collection. It executes all elements in an array and collection . (we will learn arrays and collection in later).

Syntax:

for(type variable : array)
{
//Statements;
}

For example:


class ForEachLoop

public static void main(String...s)
{
int i[]={1,2,3,4,5};
for(int a : i)
{
System.out.println(a);
}
}
}

infinite for loop in java

There also infinite for loop in java. if you use ;; in for loop it will be infinite for loop .

Syntax:

for(;;)
{
//Statements;
}

For example:

class InfiniteForLoop
{
public static void main(String []args)
{
for(;;)
{
System.out.println("This is infinite for loop");
}
}
}

output: This is infinite for loop
              This is infinite for loop
              This is infinite for loop


Note: for Stop this loop press Ctrl + c on your command prompt.

Share:

0 comments:

Post a Comment

Facebook Page Likes

Follow javatutorial95 on twitter

Popular Posts

Translate