Java For Loop | Syntax & Example Code – Letstacle

Java For Loop | Syntax & Example Code – Letstacle 0 HomeLive Programming HelpComputer Science Homework HelpPython Homework HelpPython Homework SolvedJava Homework HelpJava Homework solvedC++ Homework HelpR Homework HelpPHP Homework HelpHTML Homework HelpJavaScript Homework HelpSQL Homework HelpDo My Programming HomeworkAndroid Assignment HelpAboutHow it WorksCareersArticlesJavaJavaScriptPythonC++hackerrank solutionsHTMLMathsDatabase AcademyPython Tutor ReviewsRecent ReviewsLeave a Review PaymentFAQs Contact / Get Help Java For Loop | Syntax & Example Code July 25, 2021 0 Comments Loops are used by every programmer whether he is a beginner or an expert. Like all other languages, java also has for-loops.Why use a loop?Loops are used when we want to iterate over some statements multiple times based on some condition.There are mainly four types of loops in Java and they are as below-For-LoopWhile LoopDo-while LoopEnhanced for-LoopIn this tutorial, we are going to learn and understand for-loop in Java.When to use for-loop in Java?For-loop in java has wide uses.The java for-loop is a control flow statement. It helps to iterate over some statements until a given condition matches.For-loops are recommended if the number of iteration is known or fixed.Let us see the syntax of for-loop.Java for-loop Syntaxfor(initialization ; condition ; increament/decreament) { //loop body statement 1; statement 2; … statement n; }First, let us get familiar with these terms.initialization – We initialize the loop variable here or use some pre-initialized variable. This is the loop variable. This statement executes only once when the loop starts.condition –  The condition statement is checked every time the loop executes. If the condition statement is false control does not enter into the loop.increment/decrement- This statement is checked every time except the very first time the loop executes. Moreover, the statement is used to increase or decrease the loop variable.For your better understanding, a diagram is provided below.Java for-loop working flow diagramFinally. Let’s see how to code a for loop in Java. Above all, coding is very important.Example: Find a multiplication table for a given number using Java for loop import java.util.Scanner; public class ForLoopExample { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println(“Enter a number whose multiplication table you want”); int num=sc.nextInt(); int table=0; System.out.println(“Multiplication table for “+ num); for(int i=1;i<=10;i++) { table=num*i; System.out.println(num+" * "+i+ " = " +table); } } } OutputEnter a number whose multiplication table you want 8 Multiplication table for 8 8 * 1 = 8 8 * 2 = 16 8 * 3 = 24 8 * 4 = 32 8 * 5 = 40 8 * 6 = 48 8 * 7 = 56 8 * 8 = 64 8 * 9 = 72 8 * 10 = 80 Nested for-loop in JavaFor loop inside a for loop is called nested for-loop. The inner for loop execute if and only if control enters into the outer for loop.It sounds a bit confusing so let’s understand it with an example.Example: Print the following  triangle star pattern using For Loop*pattern Java code To print the triangle pattern we need a nested for loop, one loop(outer loop) for row and another(inner loop) for the column. public class NestedForLoop { public static void main(String[] args) { for(int i=1;i<=5;i++) { for(int j=1;j<=i;j++) { System.out.print("* "); } System.out.println(); } } } OutputBe careful with Infinite for-loopA loop becomes infinite when its condition is always true. For-loops can be infinite too. Therefore it does not terminate.Even though in most of the cases we avoid creating infinite loops but there could be some cases where we need to create it. In such cases, the loop will terminate when the application exits.Or if by mistake you land into infinite for loop then use ctrl+c in terminal to terminate it. In eclipse, we use the red dot in the top right corner of the console to terminate the infinite loop.Example of infinite for-looppublic class InfiniteForLoop { public static void main(String[] args) { for(;;) { System.out.println("Letstacle"); } } } Or something like this for (int i = 1; i < 10;) { System.out.println("Letstacle"); } OutputLetstacle Letstacle Letstacle Letstacle Letstacle Letstacle Letstacle Letstacle Letstacle ... Here, the condition is always true because we have not updated the value of “i”. Thus “i” always remains 1 and the loop becomes infinite. Like this article? Follow us on Facebook and LinkedIn. You can also subscribe to our weekly FeedShare Article: July 25, 2021Java Switch December 11, 2021Trigonometry problems with real-life examples Leave a Reply Cancel replyLog In Related Articles September 13, 2021Math pow Java | The Math pow() method in Java September 8, 2021Fibonacci series in Java March 16, 20222 Ways to Convert Object to Int in Java? September 3, 2021Recursion Java | Java Recursion August 29, 2021Rainfall Java Program Array Program | Assignment Solution July 25, 2021Inheritance in Java | Types of Inheritance in Java CategoriesBlog C++ Commerce Help Database Do my Economics Help hackerrank solutions HTML Java JavaScript Java solved Maths Maths Help Node JS Online Tutor Programming Help Python Python solved Quiz Uncategorized Recent Comments On LetstacleJimenez on Computer Science Homework Help | Computer Science HelpLetstacle Team on Python Homework Help | Help with Python HomeworkLetstacle Team on Python Homework Help | Help with Python HomeworkLetstacle Team on Python Homework Help | Help with Python HomeworkLetstacle Team on Python Homework Help | Help with Python HomeworkImler Miller on Python Homework Help | Help with Python HomeworkFeatured posts April 28, 2022Visual Basic Homework Help | VB.net Coding Help April 28, 2022Computer Science Homework Help | Computer Science Help February 15, 2022Do my Java Homework | Get Java Homework Help OnlineCategoriesBlog C++ Commerce Help Database Do my Economics Help hackerrank solutions HTML Java JavaScript Java solved Maths Maths Help Node JS Online Tutor Programming Help Python Python solved Quiz UncategorizedQuick LinksMake PaymentLeave a ReviewStoreTop Rated Rated 4.9 out of 5 Made with <3 creative team @ 2021 Letstacle.comTerms of Use | Disclaimer | Privacy Policy

Leave a comment

Your email address will not be published.