In the following example, three statements (a=10), (b=20) and (c=30) are placed in the initialization part separated by commas. The for-loop iterates over numbers. Enhanced for loop can be used to iterate through Array or collections. Statement 1 Statement 1 sets a variable before the loop starts (var i = 0). Thanks –, Very Helpful for beginners…As I am from a Non-IT background in my graduation, this site has helped me a lot…Add more sample & simple programs so that we can know more. You can right like this : Wonderful way of teaching. In that case, we can have a dummy body. The Set of elements can be an array, an ArrayList, a LinkedList or any other collection. These looping statements are also known as iterative statements. 1) Basic for loop (Old style) 2) Enhanced for loop (For-each or for-in loop) First, we will discuss basic for loop then next discuss Enhanced loop. The one-time activities associated with the loop (that too at the beginning) are done here. It’s more readable and reduces a chance to get a bug in your loop. Such variable cannot be used outside the loop. We have 3. When we create a variable at initialization part of the for loop that variable is automatically dead when the loop ends (in C, we cannot create a variable at initialization part of the for loop). Using enhanced for loop. At initialization, we can create variables but not at incrementation part. We will see the evolution of java for loop iteration techniques. In this case, the variable x gets each value of the array (one value per iteration) and it is printed. If we want to go the next iteration of a loop from the middle of the body (by skipping the remaining statements of current iteration) then we can use continue. A for loop repeats a block of code as long as some condition is true. These kinds of loops are used to travel through a set of elements strictly in an order (from beginning to ending). for Loop Example Program In Java (Sum Of Numbers): This example finds the sum of all numbers till a given input number using for Loop In Java. © 2021. Java For Loop The Java for loop is used to iterate a part of the program several times. In the above example, in the initialization part, the variable a is initialized with 1. There are three phases in the loop statement. Whereas while loops run until a condition evaluates to false, for loops run for a certain number of iterations. Java For Loop, is probably the most used one out of the three loops. In Java we have three types of basic loops: for, while and do-while. Java for Loop In this tutorial, we will learn how to use for loop in Java with the help of examples and we will also learn about the working of Loop in computer programming. We have two examples below, the first one has a set of curly braces without any code inside. Duplication or Copying Our Site Content Is Strictly Prohibited. What Are Java Loops – Definition & Explanation. For Loop in Java: In java, for-loop has two versions. To come out of a for loop from the middle of the body, we can use a, Even though it is named to be initialization, condition and incrementation parts, we can write any statement in that part. The initialization step is setting up the value of variable i to 1, since we are incrementing the value of i, it would always be greater than 1 (the Boolean expression: i>1) so it would never return false. Here is a simple Java for loop example: . The condition should always result in a boolean value. Java for loop Evolution Iteration is one of the most basic requirement in any programming language & of all, “for” is the most widely used loop in java for iteration. For loop syntax for( ; ; ){ ; } The initialization statement is executed before the loop starts. A loop is a type of control statement which encircles the flow for a whilesomething like the vo… If it results in false the control comes out of the java for loop and executes the statement immediately following the loop. … Then control comes to incr/decr part. For example if we are asked to take a dynamic collection and asked to iterate through every element, for loops would be impossible to use because we do not know the size of the collection. The name of the class is forLoopDemo. Then control moves to condition part. Infinite loops make the program run indefinitely for a long time resulting in the consumption of all resources and stopping the system. Source code in Mkyong.com is licensed under the MIT License , … Generally, the header part of the for loop decides (with initialization, condition, and incrementation) how many times the loop will be executed and the actual logic is placed in the body. Second step: Condition in for loop is evaluated on each iteration, if the condition is true then the statements inside for loop body gets executed. I think this array declaration is wrong.it can’t be like this. He declared and FIXED the size of array to 4 and if I come to your point, you can declare any number of members in your array!. When initialization ends, then that part should end with a semi-colon. Thus it is important to see the co-ordination between Boolean expression and increment/decrement operation to determine whether the loop would terminate at some point of time or not. array ={2,11,45,9}; I want a real life example. It also called: Java for each loop, for in loop, advanced loop, enhanced loop. Loops in Java are crucial when we want to execute the same block of code multiple times. It should be like this Relatively this approach (foreach) is comfortable to visit all elements of the list. Statement 3 increases a value (i++) each time the code block in the loop has been executed. i>1 is condition(Boolean expression) As a program executes, the interpreter always keeps track of which statement is about to be executed. Enhanced for loop is useful when you want to iterate Array/Collections, it is easy to write and understand. For example, the enhanced for loop for string type would look like this: String arr[]={"hi","hello","bye"}; for (String str : arr) { System.out.println(str); } Check out these java programming examples related to for loop: Java Program to find sum of natural numbers using for loop; Java Program to find factorial of a number using loops You can use for each loop in Java to iterate through array, Collections(Set, List) or Map. Most runtime in programs is spent in loops. Inside the parentheses after the for keyword, are three statements separated by semicolon (;). dot net perls. The Java for-each loop or enhanced for loop is introduced since J2SE 5.0. The initialization part can have any number of statements, but all of them should be separated by a comma. A loop statement allows us to execute a statement or group of statements multiple times and following is the general form of a loop statement in most of the programming languages − Java programming language provides the following types of loop to handle looping requirements. A for loop is a repetition control structure that allows you to efficiently write a loop that needs to be executed a specific number of times. There is no limit on the number of constructs we can place in. Statement 2 defines the condition for the loop to run (i must be less than 5). ArrayList index starts from 0, so we initialized our index variable i with 0 and looped until it reaches the ArrayList size – 1 index. Your concept is good for Multidimensional Array but for Single array.. All supposed conditions should be combined using AND or OR operators to make it a single condition (in C, we can have multiple conditions separated by commas and truth value of the last condition is considered as truth value of condition part). We have 3 types of looping constructs in Java. So it comes to loop body to print “drink more water” and then it moves to increment a value by 1 so that a becomes 2. int len=p;             is valid, as len is treated as a fresh variable. There are many problems and real examples can be created using the loop. Comments Off on Java For Loop – Tutorial With Examples | Loops. When the condition is true, the loop body is executed. At initialization, we can create, In this example, the loop is repeated for 4 times (number of, Similar to normal loop, we can come out of this loop also using a statement like, Java: Validating a Phone Number Format String | Java Programs, Java Code to Calculate Years Between Two Dates | Java Programs, Java: VAT Calculator Program In 2 Ways | Java Programs, Java Program Calculate Profit and Loss | Java Programs, Simple Java Program Internet Speed Test | Java Programs, Java: Convert Minutes To Seconds & Vice Versa | 4 Simple Ways, Java: Convert Hours To Seconds & Minutes | Vice Versa, Java Number Of Words In A String | 4 Ways, Java Program To Calculate Salary Of An Employee | 3 Ways, Java Mortgage Payment Calculator in 3 Ways | Java Programs, Java Program To Calculate Modulus | Mod Java, Java Standard Deviation in 4 Easy Ways | Java Programs, Java Distance Traveled By Vehicle Program | 4 Ways, Mean Java Program In 4 Simple Methods | Java Programs, 4 Ways To Calculate Mode In Java | Java Programs, Java Program To Calculate Median Array | 4 Methods, 4 Methods To Find Java String Length() | Str Length, Java Code For log() – 4 Simple Ways | Java Codes, Java Code to Calculate Love Percentage [FLAMES] | Programs, [GUI] Implement Simple Calculator Using JFrame/Swing In Java | Programs, Java Program to Calculate Income Tax | Java Programs, Java Code to Calculate Harmonic Value | Java Programs, Java Program to Calculate Hypotenuse Of Triangle | Programs, 4 Ways To Find Height of A Binary Tree In Java | Java Programming, Java: Volume Of Box Program | Java Programs, Java: Number of Days In A Month – 3 Ways | Java Programs, C Programs – 500+ Simple & Basic Programming Examples & Outputs, C Program Inverted Right Triangle Star Pattern – Pattern Programs, C Program To Delete An Element From An Array At Specified Position | C Programs, C Program Right Triangle Star Pattern | Pattern Programs, C Mirrored Right Triangle Star Pattern Program – Pattern Programs, C Plus Star Pattern Program – Pattern Programs | C, C Pyramid Star Pattern Program – Pattern Programs | C, C Square Star Pattern Program – C Pattern Programs | C Programs, Hollow Square Pattern Program in C | C Programs, C Program To Find Maximum & Minimum Element In Array | C Prorams, C Program To Search All Occurrences Of A Character In String | C Programs, C Program To Remove First Occurrence Of A Character From String, C Program To Count Frequency Of Each Character In String | C Programs, C Program To Reverse Words In A String | C Programs, C Program To Left Rotate An Array | C Programs, C Program To Delete Duplicate Elements From An Array | 4 Ways, C Program To Print All Unique Elements In The Array | C Programs, C Program To Count Number Of Even & Odd Elements In Array | C Programs, C Program Count Number Of Words In A String | 4 Ways, C Program Number Of Alphabets, Digits & Special Character In String | Programs, C Program To Compare Two Strings – 3 Easy Ways | C Programs, C Program To Copy One String To Another String | 4 Simple Ways, C Program To Remove Blank Spaces From String | C Programs, C Program To Find First Occurrence Of A Word In String | C Programs, C Program To Count Occurrences Of A Word In A Given String | C Programs, C Program To Search All Occurrences Of A Word In String | C Programs, C Program To Remove First Occurrence Of A Word From String | 4 Ways, C Program To Remove Repeated Characters From String | 4 Ways, C Program To Copy All Elements From An Array | C Programs, C Program To Find Last Occurrence Of A Character In A Given String, C Program To Remove Last Occurrence Of A Character From String, C Program Find Maximum Between Two Numbers | C Programs, C Program To Toggle Case Of Character Of A String | C Programs, C Program To Concatenate Two Strings | 4 Simple Ways, C Program To Find Last Occurrence Of A Word In A String | C Programs, C Program To Replace Last Occurrence Of A Character In String | C Programs, C Program To Trim White Space Characters From String | C Programs, C Program To Trim Trailing White Space Characters From String | C Programs, C Program To Trim Leading & Trailing White Space Characters From String, C Program To Remove All Occurrences Of A Character From String | C Programs, C Program Replace All Occurrences Of A Character With Another In String, C Program To Find First Occurrence Of A Character In A String, C Program Replace First Occurrence Of A Character With Another String, C Program To Find Reverse Of A string | 4 Ways, C Program To Check A String Is Palindrome Or Not | C Programs, C Program To Count Occurrences Of A Character In String | C Programs, C Program To Find Lowest Frequency Character In A String | C Programs, Highest Frequency Character In A String C Program | 4 Ways, C Program To Sort Even And Odd Elements Of Array | C Programs, C Program To Convert Lowercase String To Uppercase | 4 Ways, C Program To Convert Uppercase String To Lowercase | 4 Ways, C Program Count Number Of Vowels & Consonants In A String | 4 Ways, C Program To Count Frequency Of Each Element In Array | C Programs, Merge Two Arrays To Third Array C Program | 4 Ways, C Program Count Number of Duplicate Elements in An Array | C Programs, C Program To Right Rotate An Array | 4 Ways, C Program To Find Length Of A String | 4 Simple Ways, Rhombus Star Pattern Program In C | 4 Multiple Ways, C Program To Sort Array Elements In Ascending Order | 4 Ways, C Program To Sort Array Elements In Descending Order | 3 Ways, C Program To Count Number Of Negative Elements In Array, C Program To Insert Element In An Array At Specified Position, C Program To Read & Print Elements Of Array | C Programs, C Program To Search An Element In An Array | C Programs, C Program To Put Even And Odd Elements Of Array Into Two Separate Arrays, C Program To Print All Negative Elements In An Array, C Program To Find Sum Of All Array Elements | 4 Simple Ways, C Program Hollow Inverted Mirrored Right Triangle, Diamond Star Pattern C Program – 4 Ways | C Patterns, Hollow Inverted Pyramid Star Pattern Program in C, C Program Half Diamond Star Pattern | C Pattern Programs, C Program To Print Number Of Days In A Month | 5 Ways, C Program Hollow Inverted Right Triangle Star Pattern, C Program Hollow Mirrored Rhombus Star Pattern | C Programs, Left Arrow Star Pattern Program in C | C Programs, Right Arrow Star Pattern Program In C | 4 Ways, C Program Hollow Mirrored Right Triangle Star Pattern, 8 Star Pattern – C Program | 4 Multiple Ways, C Program To Input Week Number And Print Week Day | 2 Ways, One Dimensional Array In Java – Tutorial & Example, Two Dimensional Array In Java – JavaTutoring, Multi Dimensional Array In Java – Tutorial & Program, Java Program To Check Even Numbers | 4 Ways, Java Program To Calculate EMI – Monthly & Annum, Java Program To Calculate Exponent Value | 4 Ways, C Program Mirrored Half Diamond Star Pattern | C Patterns, C Program Inverted Mirrored Right Triangle Star Pattern, C Program Hollow Right Triangle Star Pattern, Java Program To Calculate Future Investment Value, Volume Of Cube Java Program – 2 Ways | Programs, Java Program Calculate Perimeter Of Circle | 4 Simple Ways, Java Program Perimeter Of Equilateral Triangle | Programs, Java Program Calculate Perimeter Of Parallelogram | 3 Ways, Java Program To Find Perimeter Of Rectangle | 3 Ways, Java Program Calculate Perimeter Of Square | Programs, Java Program To Calculate Perimeter Of Rhombus | 3 Ways, HCF Of Two & N Numbers Java Program | 3 Ways, LCM Of Two Numbers Java Program | 5 Ways – Programs, Java Program Convert Fahrenheit To Celsius | Vice Versa, Java Program Count Vowels In A String | Programs, Square Star Pattern Program In Java – Patterns, Java Right Arrow Star Pattern Program | Patterns, Rhombus Star Pattern Program In Java – Patterns, Reverse A Number In Java – 4 Simple Ways | Programs, Java Pyramid Star Pattern Program | Patterns, Plus Star Pattern Java Program | Patterns, Perfect Number In Java Program – 3 Ways | Programs, Palindrome Program In Java – 5 Ways | Programs, Java Mirrored Right Triangle Star Pattern Programs | Patterns, Merge Sort Java – Program 2 Ways | Sortings, Java Mirrored Half Diamond Star Pattern Programs | Patterns, Left Arrow Star Pattern Java Program – Patterns, 30+ Number & Star Pattern Programs In Java – Patterns, Java Program To Display Transpose Matrix | 3 Ways, Java Program To Subtract Two Matrices – 3 Ways, Java Program To Check Leap Year Or Not – 4 Ways, GCD Of Two Numbers In Java – Programs | 5 Ways, Prime Number Java Program – 1 to 100 & 1 to N | Programs, Java Program For Addition, Subtraction, Multiplication, Division | Programs, Java Program Sum Of digits Of A Number | Programs, Java Program To Reverse An Array | Programs, Java Program To Insert An Element In Array | Programs, Linear Search In Java Program – 2 Simple Ways | Programs, Java Program to Add Two Matrices – 4 Ways | Programs, Selection Sort Java – Algorithm 2 Ways | JavaSorting, Implement Bubble Sort Java – Algorithm | 2 Easy Ways, Java Half Diamond Star Pattern Program | Patterns, Hollow Diamond Star Pattern Java Program | Patterns, Java Inverted Right Triangle Star Pattern Program | Patterns, Java Hollow Inverted Pyramid Star Pattern Program, Java Hollow Pyramid Star Pattern Program | Patterns, Java Hollow Inverted Mirrored Right Triangle Star Pattern, Java Inverted Mirrored Right Triangle Star Pattern, QuickSort Java – Algorithm | 2 Simple Ways, Insertion Sort Java Algorithm – 2 Ways | Java Sortings, Implement Heap Sort Java Algorithm – 2 Ways | Java Sorting, Hollow Inverted Right Triangle Star Pattern Java Program, Hollow Mirrored Right Triangle Star Pattern, Fibonacci Series In Java Program – 4 Multiple Ways, C Program Find Circumference Of A Circle | 3 Ways, C Program Area Of Trapezium – 3 Ways | C Programs, C Program Area Of Rhombus – 4 Ways | C Programs, Hollow Right Triangle Star Pattern Java Program, Inverted Pyramid Star Pattern Java Program, Matrix Multiplication In Java – 4 Ways | Programs, 8 Star Pattern Java Program – 4 Ways | Programs, Reverse A String In Java – 4 Ways | Programs, X Star Pattern C Program 3 Simple Ways | C Star Patterns, C Program Hollow Diamond Star Pattern | C Programs, Mirrored Rhombus Star Pattern Program In c | Patterns, Hollow Rhombus Star Pattern Program In C | Patterns, C Program To Find Area Of Semi Circle | C Programs, C Program Area Of Parallelogram | C Programs, C Program Area Of Isosceles Triangle | C Programs, Java Program To Print Mirrored Rhombus Star Pattern | Programs, Java Program To Print Hollow Mirrored Rhombus | 4 Ways, Java Program To Print Diamond Star Pattern | Programs, Java Programs – 500+ Simple & Basic Programming With Outputs, Armstrong Number In Java Program – 5 Simple Ways, Java Program To Convert Decimal To Binary | Vice Versa, Java Program To Convert Decimal To Octal | Vice Versa, Java Program Convert Decimal To Hexadecimal | Vice Versa, Java Program Sum Of N Numbers | 4 Simple Ways, Java Program Addition Of Two Numbers – 4 Ways | Programs, Java Program To Convert Octal To Hexadecimal | Vice Versa, Java Program To Check Vowel Or Consonant | 5 Ways, C Program Check A Character Is Upper Case Or Lower Case, C Program To Count Total Number Of Notes in Given Amount, C Program To Calculate Perimeter Of Rhombus | C Programs, C Program To Calculate Perimeter Of Rectangle | C Programs, C Program To Calculate Perimeter Of Square | C Programs, C Program To Find Volume Of Cone | C Programs, C Program To Calculate Volume Of Cube | C Programs, C Program Volume Of Cylinder | C Programs, C Program Area Of Equilateral Triangle | C Programs, Curved Surface Area Of Cube : Java Program | 3 Simple Ways, Java Program Calculate Total Surface Area Of Cylinder | 3 Ways, Java Program To Calculate Average Marks | 5 Methods, Compound Interest : Java Program In 5 Simple Ways | Programs, Java Program To Calculate CGPA Percentage | 3 Simple Ways, Java Program : Calculate Batting Average Example | Programs, Java Program To Calculate Depreciation | Programs Hub, Java Program To Calculate Power Of Number | 4 Ways, Java Program To Calculate Commission Percentage | Programs, Java Program To Calculate Distance Between Two Points | 3 Ways, Java Program To Calculate Discount Of Product | Programs, Java Program To Calculate Average Of N Numbers, Java Program To Calculate Electricity Bill | Example, Factorial Program In Java – 5 Simple Ways | Java Tutoring, Total Surface Area Of Sphere Java Program | Programs, Volume Of Cone Java Program In 4 Simple Ways | Programs, Java Program Volume Of Cylinder | 3 simple ways, Java Program To Calculate Volume Of Prism | 3 Simple ways, Java Program To Calculate Volume Of Sphere – 3 Simple Ways, Java Program To Calculate Area Of Rhombus | 4 Ways, Java Program To Find Area Of Rectangle | 3 Ways, Java Program To Calculate Area Of Triangle – 5 Ways, Java Program To Find Area of Parallelogram – Programs, Java Program To Find Area Of Isosceles Triangle, Java Program To Find Area Of Equilateral Triangle, Java Program To Calculate Area Of Circle | 5 Ways, Java Program Calculate Remainder | Java programs, control comes out of the java for loop and executes the statement, Java Program Calculate Distance Between Two Points. For loop executes group of Java statements as long as the boolean condition evaluates to true. This can be made possible by Java Labels for For Loops. And then it goes to incrementation part of inner for loop and then to condition part of the same (inner) loop. By Chaitanya Singh | Filed Under: Learn Java. And, control statements provide the way to maneuver the flow of the program into different directions that are linear otherwise. The advantage of the for-each loop is that it eliminates the possibility of bugs and makes the code more readable. Java For Loop, is probably the most used one out of the three loops. Required fields are marked *, Copyright © 2012 – 2021 BeginnersBook . Java Code Editor: for(int a=5,b=6; a<=b ; a++)        is valid, for(int a=7,b=8; a+b; a++)            is not valid. Adding to the confusion, they are of various types. It is ideal … We can have a for loop inside another for loop. This (inner loop) is repeated as long as the inner condition is true. See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases. in Java Tutorials, Loops This activity takes place only once. for(int i=0; i < 10; i++) { System.out.println("i is: " + i); } . A for loop is a control structure that allows us to repeat certain operations by incrementing and evaluating a loop counter.. Before the first iteration, the loop counter gets initialized, then the condition evaluation is performed followed by the step definition (usually a simple incrementation). Mkyong.com is providing Java and Spring tutorials and code snippets since 2008. There are often times when we need to use nested loops in Java and thus need to break the outer loop instead of the inner loop. Java also includes another version of for loop introduced in Java 5. In case the header part itself has the actual logic implicitly then the body may not be required. The trouble with for. The above three different java loops are used primarily with same purpose and the difference is in their syntax. After executing the incr/decr part, the control comes to condition part. The first stumbling block when we start learning any programming language is the concept of loops. Let’s take the same example that we have written above and rewrite it using enhanced for loop. If the condition is true, the loop will start over again, if it is false, the loop will end. The traditional for loop was introduced in the first release of the Java language, and its simpler variation, for-each, was introduced in Java 5.Most developers prefer for-each for everyday iterations, but will still use for for things like iterating through a range or skipping values in a range.. We'll be focusing on iterating through the list in order, though going in reverseis simple, too. Enhanced ‘for’ Loop: The Java SE 5 extends the basic for loop to increase the readability of the loop. After executing the incr/decr part, the control comes to condition part. In computer programming, loops are used to repeat a block of code. This is where the actual logic is executed. Sometimes the initialization takes place by the time control comes to the for loop, in that case, we can write a dummy statement (just a semi-colon) at initialization part. Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available. Java Program to find sum of natural numbers using for loop, Java Program to find factorial of a number using loops, Java Program to print Fibonacci Series using for loop. If we want parts of the list then we can use the normal for loop. A quick and practical guide to Java loops. To get the same result, we can use the following notation (for each style). For. Your email address will not be published. class forLoopDemo { public static void main(String args[]) { // for loop 0begins when x=1 // and runs till x <=10 System.out.println("OUTPUT OF THE FIRST 10 N… The Java Tutorials have been written for JDK 8. First step: In for loop, initialization happens first and only one time, which means that the initialization part of for loop only executes once. The sample code is given below as well as the output. The following example gives another example of using for each notation to travel through an ArrayList. While loop in Java. Java for loop is the easiest loops in Java that has all the expressions in a single line. It is inflexible and should be used only when there is a need to iterate through the elements in sequential manner without knowing the index of currently processed element. There are three types of … int arr[4]={2,11,45,9}; Compare different loops. It is commonly used. If the condition results in true, the control enters the body. In Java, for loops are used to repeat the execution of a block of code a certain number of times. So all the following usages are valid. This process is repeated as long as condition results in true. If the condition is true, it enters the loop body and executes statementA. This will change depending on the data type of array. The execution process of the Java for loop is: Initialization: We initialize the counter variable(s) here.For example i = 1. 2. This is known as nesting. Statement 3 increases a value (i++) each time the code block in the loop has been executed. If it results true, the body is executed, then comes to incr/decr and then to condition part. Similar to normal loop, we can come out of this loop also using a statement like break; To travel through a two dimensional array we can write a code like the following. To come out of a for loop from the middle of the body, we can use a break. Even though it is named to be initialization, condition and incrementation parts, we can write any statement in that part. This part is executed only once. The condition part should result in a boolean value. Java provides a way to use the “for” loop that will iterate through each element of the array. for(a=2; a<=b && a<=c; a++)       is valid, for(a=3; a<=b, a<=c; a++)             is not valid, for(a=4; a>=b || a>=0; a–)            is valid. In the above example, the outer loop travels through an array of references (where each reference will refer an array) and the inner loop travels through each element (of the current array). Thus, it is a good practice to avoid using such loops in a program. But the condition part should not be separated by commas. Enhanced for loop provides a simpler way to iterate through the elements of a collection or array. The following code is valid. It is generally used to initialize the loop variable. In this tutorial we will learn how to use “for loop” in Java. Great going guys…keep it up :). Then it moves to condition a<=3 which results in true. Privacy Policy . It runs from 1 to 10 generating all the natural numbers in between. If we want we can create a variable a fresh with the same name outside. In the second example, just a semi-colon is the body of the loop. The importance of Boolean expression and increment/decrement operation co-ordination: This is an infinite loop as the condition would never return false. i think first one was right . Java for loop tutorial with examples and complete guide for beginners. If we write a for loop like for(;;) and there is no break or return kind of statements then that loop becomes an infinite loop. int x=len;             is not valid as len is dead. Test condition: It will check for the condition against the counter variable.If the condition is True, the compiler will execute the statements inside the for loop. Suppose we have an array and wanted to print each element of the array we can write a normal loop like the following. For loop combines three elements which we generally use: initialization statement, boolean expression and increment or decrement statement. As i find it very ease in learning java, i’ve been in touch with the same for past 1yr but never been so comfortable with it. Whatever we can do for a while we can do it with a Java for loop too (and of course with a do-while too). This is one of the best sites for learning java. i– Decrement operation. Then it comes to condition part, results in true, print the message and comes back to increment to make a value 3. It provides an alternative approach to traverse the array or collection in Java. The below article on Java for loop will cover most of the information, covering all the different methods, syntax, examples that we used in for loops.