X

Play AlgoQuiz

Rate Us :



Share with Friends :

Menu

for loop :

A for loop is a repetition control structure that allows us to efficiently write a loop that needs to execute a specific number of times. A for loop is useful when we know how many times a task is to be repeated.

It is most commonly used loop.

Syntax: for(initialization_section ; conditional_expression ; update) { //Statements }

initialization_section :

  • This section executes only once.

  • Usually we are declaring and performing initialization for variables in this section.

  • We can declare multiple variables of same type but not for different types.

int i = 0 , j = 0; // valid
int i = 0 , byte b = 0; // invalid
int i = 0 , int j = 0; // invalid

Example:

class Example { public static void main(String [ ] args) { for(int i = 0 , j = 0 ; i < 5 ; i++ , j++) { System.out.println(j); } } }

Output:

0 1 2 3 4
  • In the initialization section we can take any valid Java statement.

Example:

class Example { public static void main(String [ ] args) { int i = 0; for(System.out.println("Algo") ; i < 3 ; i++) { System.out.println("Valley"); } } }

Output:

Algo Valley Valley Valley

conditional_expression :

  • Here, we can take any Java expression but the result should be of boolean type.

  • It is optional and if we are not specifying it then compiler will always take it as 'true'.

update :

  • Usually we increment / decrement the value of variables of initialization section.

  • We can take any valid Java statement.

Example:

class Example { public static void main(String [ ] args) { int i = 0; for(System.out.println("Algo") ; i < 3 ; System.out.println("Valley")) { i++; } } }

Output:

Algo Valley Valley Valley

  • All three parts of for loop are independent of each other.

  • All three parts of for loop are optional.

for( ; ; ); // valid infinite loop
// Terminator (;) is a valid Java statement.
for(int i = 0 ; true ; i++)
{
 System.out.println("Algo");
}
System.out.println("Valley"); // Compile time error : unreachable statement
for(int i = 0 ; false ; i++)
{ // Compile time error : unreachable statement
 System.out.println("Algo");
}
System.out.println("Valley");
for(int i = 0 ; ; i++)
{
 System.out.println("Algo");
}
System.out.println("Valley"); // Compile time error : unreachable statement
int a = 10 , b = 20;
for(int i = 0 ; a < b ; i++)
{
 System.out.println("Algo");
}
System.out.println("Valley"); // valid
final int a = 10 , b = 20;
for(int i = 0 ; a < b ; i++)
{
 System.out.println("Algo");
}
System.out.println("Valley"); // Compile time error : unreachable statement

Example:

class Example { public static void main(String [ ] args) { for(int a = 1 ; a <= 5 ; a++) { System.out.println("USA"); } } }

Output:

Here, the loop will be carried on until the condition becomes false. Therefore, output will be - USA USA USA USA USA

Enhanced for loop in Java (for-each loop) :

As of version 1.5 of Java, the enhanced for loop was introduced. This is mainly used to retrieve the elements of Arrays and Collections.

X

retrieve :

It refers to find or extract (information stored in a computer).

X

compatible :

It refers to able to exist or occur together (of two things) without problem or conflict.

Syntax: for(declaration : expression) { //Statements }

Declaration :

The newly declared block variable, which is of a type compatible with the elements of the array which we are accessing. The variable will be available within the for block and its value would be the same as the current array element.


Expression :

This evaluates to the array which we need to loop through. The expression can be an array variable or method call that returns an array.

Example 1:

class Example { public static void main(String [ ] args) { int[ ] numbers = {10,20,30,40,50}; for(int x : numbers ) { System.out.print( x ); System.out.print(","); } System.out.print("\n"); String[ ] names = {"Java","PHP","ASP.NET","JSP"}; for(String name : names ) { System.out.print( name ); System.out.print(","); } } }

Output:

Here, output would be - 10,20,30,40,50, Java,PHP,ASP.NET,JSP,

Example 2:

class Example { public static void main(String [ ] args) { int[ ][ ] numbers = {{10,20},{30,40,50}}; for(int[ ] x : numbers ) { for(int y : x) { System.out.print( y ); System.out.print(","); } } } }

Output:

Here, output would be - 10,20,30,40,50,

Limitation of for-each loop :

  • It is not a general purpose loop.

  • It is applicable only for Arrays and Collections.

  • By using for-each loop we can retrieve all values of Arrays and Collections but we can't retrieve a particular set of values.


X

MiniDoll :

MiniDoll is an application to learn with fun. You can play it to solve some tricky questions related to Java Programming. Either enjoy playing it alone or discuss with your friends to solve it. It is a simple approach to learn by solving questions. This application has different topics of Java and each topic is divided into three phases. Start solving from initial topic to final topic and by the end of every topic you can boost your concept of that topic.