Java For Loop and Types of For loops and For-each loop with the Examples

Java For Loop:
The for statement is the most commonly used looping statement.  The statement includes initialization expression that specifies an initial value for an index, and the condition expression determines whether the loop is continued or not and last iteration expression allows the index to be modified at the end of each pass.
Types of for loops: There are two types of for loops. They are:
1)    for() loop.
2)    for-each loop (enhanced for loop).
Syntax of for() loop:
for(initialization; condition; iteration)

{
//codes to be Executed//
}


Example:
class Forexample
{
 public static void main(String[] args)
 {         
  int i;
  for(i = 1; i<=10; i++)
  {
   System.out.println("value of i = " +i);
  }
  System.out.println("Example of For Loop");
 }
}

Output:
value of i = 1
value of i = 2
value of i = 3
value of i = 4
value of i = 5
value of i = 6
value of i = 7
value of i = 8
value of i = 9
value of i = 10
Example of For Loop

Explanation of above program:
1)    Variable of type int (integer) is declared.
2)    Now in for loop first it is initialized as 1.
3)    Now condition is checked 1 is less than 10. So the value is printed.
4)    The loop continues until the value of “i” becomes “11” the condition (11<=10) is false so the control jump out of for loop.
 In the above we explained using Increment operator. Using Decrement operator we use for loop.
Example:
class Forexample1
{
 public static void main(String[] args)
 {
  int i;
  for(i = 4; i>=1; i--)
  {
   System.out.println("value of i = " +i);
  }
 }
}
Output:
value of i = 4
value of i = 3
value of i = 2
value of i = 1

for-each loop(enhanced for loop): It is a specially designed loop to iterate the elements of arrays and collections.
Syntax:
for(Type variable : array)
{
 //code to be executed//
}
Example:
class ForEachExample
{
 public static void main(String[] args)
 {
  int i[] = {1,2,3,4,5};
  for(int j : i)
  {
   System.out.println("j = " +j);
  }                    
 }
}
Output:
j = 1
j = 2
j = 3
j = 4
j = 5

Nested for loops: Nested for loops allow loops to be nested. That is one for loop present inside another for a loop.
Example:
class NesExample
{
 public static void main(String[] args)
 {
  int i,j;
  for(i = 0; i < 5; i++)
  {
   for(j = i; j < 5; j++)
   {
    System.out.print("*");
   }
   System.out.println();
  }
 }
}

Output:
*****
****
***
**
*

For loop using comma:
class Forcamma
{
 public static void main(String[] args)
 {
  int i,j;
  for( i = 1, j = 5; i<j; i++, j--)
  {
  System.out.println("i = " + i);
  System.out.println("j = " + j);
  }
 }
}
Output:
i = 1
j = 5
i = 2
j = 4


for loop using if and break:
class IfBreakExample
{
 public static void main(String[] args)
 {
  for(int i = 0; i < 20; i++)
  {
   if(i == 10)
   break;
  System.out.println("i = " + i);
  }
 }
}

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

Java for loop using continue:
Example:
class ContinueExample
{
 public static void main(String[] args)
 {
  for(int i = 0; i < 10; i++)
  {
   System.out.print(i + " ");
   if(i % 2 == 0)
   continue;
   System.out.println(" ");
  }
 }
}

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

Search This Blog

  • ()

Our Services

Powered by Blogger.

FSU Webmail and login into the FSU Webmail

FSU Webmail FSU Webmail (Florida State University) Webmail is email administrations to the customer to get to messages anyplace through a...