JAVA For Loop
For loop in java is one of the most common type of loop. There are two types of for loop:
- Basic For loop
- For Each loop (Introduced in Java 5)
Basic For loop:
For loop declaration has there parts : initialization, termination condition, iteration expression. These parts are separated by semicolons.
Syntax of for loop:-
for(initialization; condition; iteration){ |
Initialization part: The initialization part of for loop is executed only for the first when the loop begins. It lets you declare and initialize one or more variables of the same types. One thing to note when the declaring a variable in for loop is the scope of variable. If the variable is declared in the for loop the variable's scope is limited to the for loop.
Condition part: The condition part must be an Boolean expression. In condition part there cannot be multiple test condition but we can use logical operators to make a logical expression that evaluates into a Boolean result. The body of the for loop is executed only if the condition is true else the loop is terminated.
Iteration part: After every iteration of for loop's body, the iteration part is executed. This is the part where you can set the change in loop control variable. The given example will give a better understanding of working of for loop.
Example of for loop:- This is a very simple example of for loop which will take an integer input form user and print the table of that number.
import java.util.Scanner; public class ForLoop { |
In this program, a Scanner class is used to take input from user. Next, is a for loop which is declare to run10 times. This for loop starts with initialization int num=1, and executes the body part till the condition num<=10 becomes false, after executing the body of for loop for each and every time num value will be incremented by 1.
Output of Program:-
Different ways of using For Loop:- For Loop can be used in different ways and sometimes it can be very complex. We will discuses this in the examples below. In For loop there is one thing to notice that all three declaration part of for loop can be left blank.
for ( ; ; ) { |
As we now know that all three part of For loop are optional and can be left blank but it is very interesting to know that what kind of code you can write in these parts. The condition part is the only part of for loop declaration for which there is a rule defined that it must be an Boolean expression. In the remaining two, Initialization and Iteration part we can actually write any peace of code.
public class ForLoop { System.out.println("Iteration Part") ) { System.out.println("Body part"); } |
Output of Program:-
Use of comma and logical operators in For loop:- In For loop we can use multiple statements in initialization and iteration parts using comma. In initialization part we can define multiple variables by separating the variables with comma but the variables must be of same type.
As we know, condition part of the for loop must be an boolean expression therefor use of comma is not allowed but we can use logical operators to make a valid boolean expression.
public class ForLoop { |
Output of the program:-
In the next post we will learn about For each loop or Enhanced For Loop.