for loop statement in Java

In Previous post we have discussed the Condition Statement. This Article (And next two Articles) will discuss about the Looping statement in Java Language.

Looping statement can be of 3 types.

  1. For Loop
  2. While Loop
  3. Do-While Loop

For Loop

For(for) loop is used when you want to iterate over a range of value.

Scenario : When you want to print a string for 4 times.

Syntax of for loop

 

initialization : used to initialize the loop. This part will be executed once only.

termination : This loop will continue unless this part has value false.

increment : This expression will be invoked after each iteration can be used to increment or decrements the value.

Example of for loop

 

Output of the above programme would be

As you can see this loop will continue till termination expression evaluates to FALSE.

Note*:  All three expression in for loop are optional. Means below for loop is also valid.

 

 

Enhanced for Loop

Starting for Java 5, Java has special form of for loop which is mainly useful in case of Collection and Array traversal. Any Class which has implemented iterator interface can use this enhanced for loop.

Syntax of Enhance for loop

 

Output of the above code would be

Here we have learned the for loop. In next article we will learn about other looping statement and that is WHILE loop.

1 Comment for loop statement in Java

  1. Pingback: Java Beginners Tutorial While loop statement in Java for beginners

Leave A Comment

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