Java Loops and Control Statements
In programming, if you want to repeat the execution of a particular set of instructions or want to execute a function/set of lines repeatedly then we use loops. For example, if you want to print a number 10 times, what would you do? Probably a newbie would write a print statement 10 times, but we can do the same thing to use a loop. Let's understand the different types of loops with examples. For loop For-each loop While loop Do-While loop 1. For Loop When you know that you want to execute your code fixed no. of times say 10 times, then for loop comes into the picture. It's the most used and the simplest loop in any programming. Here is the For loop structure: for (initializing variable; condition; increment/decrement) { // Code/Statement to be iterated } It has four different elements, let's understand them: Initialization Variable - It is the starting point of the for loop. This variable gets initialized when for loop gets started.