Title: For loop Java Example (with video) - Java Code Geeks
Open Graph Title: For loop Java Example (with video) - Java Code Geeks
Description: Interested to learn more about Java? Then check out our detailed example on Java for loop and enhanced for loop java provides!
Open Graph Description: Interested to learn more about Java? Then check out our detailed example on Java for loop and enhanced for loop java provides!
Mail addresses
support@javacodegeeks.com
Opengraph URL: https://examples.javacodegeeks.com/for-loop-java-example/
Generator: WordPress 6.9.4
Domain: examples.javacodegeeks.com
{"@context":"https://schema.org","@graph":[{"@type":"Article","@id":"https://examples.javacodegeeks.com/for-loop-java-example/#article","isPartOf":{"@id":"https://examples.javacodegeeks.com/for-loop-java-example/"},"author":{"name":"Konstantina Dimtsa","@id":"https://examples.javacodegeeks.com/#/schema/person/5fc06d4d50814931c15af68106832a56"},"headline":"For loop Java Example (with video)","datePublished":"2014-01-09T10:06:53+00:00","dateModified":"2022-07-06T10:56:47+00:00","mainEntityOfPage":{"@id":"https://examples.javacodegeeks.com/for-loop-java-example/"},"wordCount":721,"commentCount":0,"publisher":{"@id":"https://examples.javacodegeeks.com/#organization"},"image":{"@id":"https://examples.javacodegeeks.com/for-loop-java-example/#primaryimage"},"thumbnailUrl":"https://examples.javacodegeeks.com/wp-content/uploads/2012/12/java-logo.jpg","articleSection":["for loop"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https://examples.javacodegeeks.com/for-loop-java-example/#respond"]}]},{"@type":"WebPage","@id":"https://examples.javacodegeeks.com/for-loop-java-example/","url":"https://examples.javacodegeeks.com/for-loop-java-example/","name":"For loop Java Example (with video) - Java Code Geeks","isPartOf":{"@id":"https://examples.javacodegeeks.com/#website"},"primaryImageOfPage":{"@id":"https://examples.javacodegeeks.com/for-loop-java-example/#primaryimage"},"image":{"@id":"https://examples.javacodegeeks.com/for-loop-java-example/#primaryimage"},"thumbnailUrl":"https://examples.javacodegeeks.com/wp-content/uploads/2012/12/java-logo.jpg","datePublished":"2014-01-09T10:06:53+00:00","dateModified":"2022-07-06T10:56:47+00:00","description":"Interested to learn more about Java? Then check out our detailed example on Java for loop and enhanced for loop java provides!","breadcrumb":{"@id":"https://examples.javacodegeeks.com/for-loop-java-example/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https://examples.javacodegeeks.com/for-loop-java-example/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https://examples.javacodegeeks.com/for-loop-java-example/#primaryimage","url":"https://examples.javacodegeeks.com/wp-content/uploads/2012/12/java-logo.jpg","contentUrl":"https://examples.javacodegeeks.com/wp-content/uploads/2012/12/java-logo.jpg","width":150,"height":150,"caption":"Bipartite Graph"},{"@type":"BreadcrumbList","@id":"https://examples.javacodegeeks.com/for-loop-java-example/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://examples.javacodegeeks.com/"},{"@type":"ListItem","position":2,"name":"Java Development","item":"https://examples.javacodegeeks.com/category/java-development/"},{"@type":"ListItem","position":3,"name":"Java Basics","item":"https://examples.javacodegeeks.com/category/java-development/java-basics/"},{"@type":"ListItem","position":4,"name":"for loop","item":"https://examples.javacodegeeks.com/category/java-development/java-basics/for-loop/"},{"@type":"ListItem","position":5,"name":"For loop Java Example (with video)"}]},{"@type":"WebSite","@id":"https://examples.javacodegeeks.com/#website","url":"https://examples.javacodegeeks.com/","name":"Java Code Geeks","description":"Java Examples and Code Snippets","publisher":{"@id":"https://examples.javacodegeeks.com/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://examples.javacodegeeks.com/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https://examples.javacodegeeks.com/#organization","name":"Exelixis Media P.C.","url":"https://examples.javacodegeeks.com/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https://examples.javacodegeeks.com/#/schema/logo/image/","url":"https://examples.javacodegeeks.com/wp-content/uploads/2022/06/exelixis-logo.png","contentUrl":"https://examples.javacodegeeks.com/wp-content/uploads/2022/06/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https://examples.javacodegeeks.com/#/schema/logo/image/"},"sameAs":["https://www.facebook.com/javacodegeeks","https://x.com/javacodegeeks"]},{"@type":"Person","@id":"https://examples.javacodegeeks.com/#/schema/person/5fc06d4d50814931c15af68106832a56","name":"Konstantina Dimtsa","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https://examples.javacodegeeks.com/#/schema/person/image/","url":"https://examples.javacodegeeks.com/wp-content/uploads/2013/10/Konstantina-Dimtsa-96x96.jpg","contentUrl":"https://examples.javacodegeeks.com/wp-content/uploads/2013/10/Konstantina-Dimtsa-96x96.jpg","caption":"Konstantina Dimtsa"},"description":"Konstantina has graduated from the Department of Informatics and Telecommunications in National and Kapodistrian University of Athens (NKUA) and she is currently pursuing M.Sc studies in Advanced Information Systems at the same department. She is also working as a research associate for NKUA in the field of telecommunications. Her main interests lie in software engineering, web applications, databases and telecommunications.","sameAs":["http://www.javacodegeeks.com"],"url":"https://examples.javacodegeeks.com/author/konstantina-dimtsa/"}]}
{"@context":"http:\/\/schema.org","@type":"BreadcrumbList","@id":"#Breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"item":{"name":"Home","@id":"https:\/\/examples.javacodegeeks.com\/"}},{"@type":"ListItem","position":2,"item":{"name":"Java Development","@id":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/"}},{"@type":"ListItem","position":3,"item":{"name":"Java Basics","@id":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/java-basics\/"}},{"@type":"ListItem","position":4,"item":{"name":"for loop","@id":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/java-basics\/for-loop\/"}}]}
{"@context":"http:\/\/schema.org","@type":"Article","dateCreated":"2014-01-09T12:06:53+02:00","datePublished":"2014-01-09T12:06:53+02:00","dateModified":"2022-07-06T13:56:47+03:00","headline":"For loop Java Example (with video)","name":"For loop Java Example (with video)","keywords":[],"url":"https:\/\/examples.javacodegeeks.com\/for-loop-java-example\/","description":"In this post, we feature a comprehensive For loop Java example. If you need to execute a block of code many times, you will have to use the for loop or the enhanced for loop java provides.Java provide","copyrightYear":"2014","articleSection":"for loop","articleBody":"\nIn this post, we feature a comprehensive For loop Java example. If you need to execute a block of code many times, you will have to use the for loop or the enhanced for loop java provides.Java provides three looping mechanisms, which are the following:\n\n\n\nIf you need to execute a block of code many times, then you will definitely have to use this mechanism.Java provides three ways to iterate:\n\n\n\nwhile Loopdo-while Loopfor Loop\n\n\n\nIn this example, we will show how to use the for loop mechanism. Also, we will show the enhanced for loop, which was introduced in Java 5 and it is mainly used for Arrays.\n\n\n\nYou can also check Loops in Java in the following video:\n\n\n\n For loop, while loop and do while loop in Java - Video\n\n\n\n\n\n\n\n1. Syntax of For loop\n\n\n\nThe for statement provides a compact way to iterate over a range of values until a particular condition is satisfied. The general form of the for statement is the one following:\n\n\n\nfor (initializations; condition; update expressions) {\n \/\/statement expressions\n}\n\n\n\n\ninitializations: This expression declares and initializes the loop control variable. Note that you can have as many declarations and initialization as you like. They are executed only once when the loop begins.condition: If the condition evaluates to true, the statement expressions are executed. If the condition evaluates to false, the loop terminates without the execution of the body of the loop.update expressions: The update expression is invoked after each execution of the body of the loop and updates the loop control variable. You can have multiple update expressions.\n\n\n\n2. For loop Java Example\n\n\n\nLet's see an example of the for loop mechanism. Create a java class named ForLoopExample.java with the following code:\n\n\n\nForLoopExample.java\n\n\n\npackage com.javacodegeeks.javabasics.forloop;\n\npublic class ForLoopExample {\n public static void main(String args[]) {\n\n for (int i = 0; i < 5; i++) {\n System.out.println(\"i is: \" + i);\n }\n\n System.out.println(\"Print again the i values, with different condition\");\n\n for (int i = 0; i <= 5; i++) {\n System.out.println(\"i is: \" + i);\n }\n\n System.out.println(\"Print the array Cities\");\n String[] cities = { \"Athens\", \"Thessaloniki\", \"Chania\", \"Patra\",\n \"Larissa\" };\n\n for (int i = 0; i < cities.length; i++) {\n System.out.println(cities[i]);\n }\n\n System.out.println(\"Example with multiple declarations,initialiazations and update expressions\");\n\n for(int i=0,j=1,k=2;i<=10&&j<=11&&k<=12;i++,j=j+2,k=j+3){\n System.out.println(\"i is: \" + i); \n System.out.println(\"j is: \" + j);\n System.out.println(\"k is: \" + k); \n }\n }\n}\n\n\n\n\nIn the above code, we can see two loops that seem to be the same but they have a basic difference in their conditions. The first loop evaluates to true as long as the i value is strictly less than 5 but the second loop evaluates to true even when the i value is equal to 5. Afterward, we have another loop printing an array named \"cities\". The condition evaluates to false when the counter i is greater than or equal to the array's length. The last loop shows how to write a loop with multiple declarations, initializations, and update expressions. We initialize i, j and k (The type must be the same), then we have the condition and then we add some numbers to the variables. If we run the above code, we will get the following results:\n\n\nOutput\n\n\ni is: 0\ni is: 1\ni is: 2\ni is: 3\ni is: 4\nPrint again the i values, with different condition\ni is: 0\ni is: 1\ni is: 2\ni is: 3\ni is: 4\ni is: 5\nPrint the array Cities\nAthens\nThessaloniki\nChania\nPatra\nLarissa\nExample with multiple declarations,initialiazations and update expressions\ni is: 0\nj is: 1\nk is: 2\ni is: 1\nj is: 3\nk is: 6\ni is: 2\nj is: 5\nk is: 8\ni is: 3\nj is: 7\nk is: 10\ni is: 4\nj is: 9\nk is: 12\n\n\n\n\nAs we see in the output, the first loop does not print the i value when the i is equal to 5, while the second loop does. It may seem that this is too much detail, but actually, one more or less execution of the loop may be critical for each program.\n\n\n\n3. Syntax of the \"enhanced\" For loop\n\n\n\nAs we mentioned in the introduction, Java 5 introduced the enhanced for loop as a simpler way to iterate through all the elements of a Collection. This for loop is commonly used by arrays, when you wish to step through the elements of the array in first-to-last order, and you do not need to know the index of the current element. The syntax of the enhanced for loop java provides is the following:\n\n\n\nfor (declaration : expression) {\n \/\/Statement expressions\n}\n\n\n\ndeclaration: A new variable is declared, which is of the same type as the type of the elements of the array. This variable is used in the body of the loop and its value is the same as the current array element. The colon in the syntax can be read as \"in.\"expression: This evaluates to the array you need to loop through. The expression can be an array variable or method call that returns an array.\n\n\n\n4. Example of the \"enhanced\" For loop\n\n\n\nLet's see an example of the enhanced for loop mechanism. Create a java class named EnhancedForLoopExample.java with the following code:\n\n\n\nEnhancedForLoopExample.java\n\n\n\npackage com.javacodegeeks.javabasics.forloop;\n\npublic class EnhancedForLoopExample {\n\n public static void main(String args[]) {\n String[] cities = { \"Athens\", \"Thessaloniki\", \"Chania\", \"Patra\",\n \"Larissa\" };\n \/\/Same can be done with a List such as:\n \/\/List<String> cities = new ArrayList<>();\n \/\/cities.add(\"Athens\");\n \/\/cities.add( \"Thessaloniki\" ); \n \/\/cities.add( \"Chania\" ); \n \/\/cities.add (\"Patra\" ); \n \/\/cities.add (\"Larissa\" ); \n for (String cityname : cities) {\n System.out.println(cityname);\n }\n\n }\n}\n\n\n\n\nIn the above code, we use the array from the previous example and print it with the enhanced for loop java provides. The declaration expression is a variable cityname which is of type String, just as the type of the elements of the array cities.\n\n\n\nOutput\n\n\n\nAthens\nThessaloniki\nChania\nPatra\nLarissa\n\n\n\n\n5. More articles\n\n\n\nFor Each Loop Java 8 ExampleSimple while loop Java ExampleJava Switch Case Example\n\n\n\n6. Download the Source Code\n\n\n\nThis was a For loop Java example. \n\n\n\nDownload\nYou can download the source code from here: Java For loop Example\n\n\n\nLast updated on March 2nd, 2022\n","publisher":{"@id":"#Publisher","@type":"Organization","name":"Examples Java Code Geeks","logo":{"@type":"ImageObject","url":"\/wp-content\/uploads\/2013\/05\/JavaCodeGeeks-logo.png"},"sameAs":["https:\/\/feeds.feedburner.com\/examplesjavacodegeeks","https:\/\/www.facebook.com\/javacodegeeks","https:\/\/twitter.com\/javacodegeeks","https:\/\/www.linkedin.com\/groups\/3810709\/","https:\/\/www.youtube.com\/channel\/UCxoUc7Rar2q90Gu0nT2ffuQ","https:\/\/github.com\/javacodegeeks\/"]},"sourceOrganization":{"@id":"#Publisher"},"copyrightHolder":{"@id":"#Publisher"},"mainEntityOfPage":{"@type":"WebPage","@id":"https:\/\/examples.javacodegeeks.com\/for-loop-java-example\/","breadcrumb":{"@id":"#Breadcrumb"}},"author":{"@type":"Person","name":"Konstantina Dimtsa","url":"https:\/\/examples.javacodegeeks.com\/author\/konstantina-dimtsa\/"},"image":{"@type":"ImageObject","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg","width":1200,"height":150}}
| og:locale | en_US |
| og:type | article |
| og:site_name | Examples Java Code Geeks |
| article:publisher | https://www.facebook.com/javacodegeeks |
| article:published_time | 2014-01-09T10:06:53+00:00 |
| article:modified_time | 2022-07-06T10:56:47+00:00 |
| og:image | https://examples.javacodegeeks.com/wp-content/uploads/2012/12/java-logo.jpg |
| og:image:width | 150 |
| og:image:height | 150 |
| og:image:type | image/jpeg |
| author | Konstantina Dimtsa |
| twitter:card | summary_large_image |
| twitter:creator | @javacodegeeks |
| twitter:label1 | Written by |
| twitter:data1 | Konstantina Dimtsa |
| twitter:label2 | Est. reading time |
| twitter:data2 | 5 minutes |
| None | IE=edge |
| msapplication-TileImage | https://examples.javacodegeeks.com/wp-content/uploads/2013/05/JavaCodeGeeks-favIcon.png |
Links:
Viewport: width=device-width, initial-scale=1.0
Robots: index, follow, max-image-preview:large, max-snippet:-1, max-video-preview:-1