Java Tutorial

What is Java


Control Statements


Java Object Class


Java Inheritance


Java Polymorphism


Java Abstraction


Java Encapsulation


Java Array

Java OOPs Misc


For Loop in java

In java for loop is used to iterate a part of the program several times. If the number of iteration is fixed, it is recommended to use for loop.
1.for loop is a most commonly user loop is java.
2. If we are not use number of iteration in advance than for loop is best choise.

There are three types of for loop in java.

  1. For Loop
  2. For-each or Enhanced For Loop
  3. Labeled For Loop

Java For Loop

1- Initialization
    1. This Part will be executed only one in loop like cycle.
    2. here we can declear and the initailize local variable of for loop.
    3. Here we can declear any number of variable but should be same type. so by mistake if try to declear different data type variable then we will get compile time error.

2- Condition
    1.Here we can take any valid java expression but should be At time boolean this Part is Optional & if We are not taking anything .

3- Increment/Decrement
1. In the Increment and Decrement section we can take any valid java Statement including System.Out.print.

Syntax:


for(initialization;conditional check;increment/decrement)
{
loop boby
}


Infinitive For Loop


public class JavaBit{
public static void main(String ab[])
{
int i ;
for(i=0; true; i++)
{
System.out.println("java64bit");
}
}
}


Output: java64bit .......

Example2:


public class JavaBit{
public static void main(String ab[])
{
int n=10 ;
for(int i=0; i< n; i++)
{
System.out.println(i);
}
}
}


Output: 0,1,2,3,4,5,6,7,8,9

For-each Loop in java

In for-each loop is specifically designed to handle the elements of array or collection in java. It is easier to use than simple for loop because we don't need to increment value and use subscript notation.

It works on elements basis not index. It returns element one by one in the defined variable.

Syntax:


for(var : collection)
{
Statement;
}


Example:


public class Java64bit{
public static void main(String ab[])
{
int arr[]={200, 19, -56, 44, 99}; // declear an array with 5 elements.
for(int i : arr) // for each to retrieve elements from array.
{
System.out.println(i);
}
}
}


Output: 200, 19, -56, 44, 99

Labeled For Loop in java

We can have name of each for loop. To do so, we use label before the for loop. It is useful if we have nested for loop so that we can break/continue specific for loop.

Normally, break and continue keywords breaks/continues the inner most for loop only.

Syntax:


labelname:
for(initialization;conditional check;increment/decrement)
{
loop boby
}


Example:


package com.ashu;
public class LabeledBreakJava64bit {
public static void main(String[] args) {
int c = 0;
start: {
for (int i = 0; i <= 10; i++) {
for (int j = 0; j <= 10; j++) {
if (i == 5)
break start;
}
c++;
}
}
System.out.println(c);
}
}


Output: 5

Example:


package com.ashu;
class Java64bit
{
public static void main(String args[])
{
int i,j;
for(i=1;i<=10;i++)
{
System.out.println();
for(j=1;j<=10;j++)
{
System.out.print(j + " ");
if(j==5)
break;
}
}
}
}


Output: 1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5

Copyright ©2017 Design& Development