

In the code example above, we use a for loop to print the phrase "are we there yet?" exactly 5 times. In the four loop there are 3 parts in the syntax to be aware of.
-
int i = 0: this part is the starting point for the counter variable i
-
i<5: this part is the end point for the counter, this states that so long as the counter variable i is below 5, the loop will continue.
-
i++: this part says after each run of the loop, increase the counter variable i by 1, you can change this to increment the variable by any integer number x by saying (i += x)
Extended For Loops


The code snippet above shows something called an extended for loop or for-each loop. The first part (int num: ) is a temporary variable.
The second part (: numbers) is an array of numbers. The for each loop basically states, for each integer in this array.
The loop iterates for each element in the array and at the same time stores each element for each loop. In this case we use the loop to print the number array
While loops
While loops are loops that execute code within them continuosly until the boolean expression associated with it is false. As long as the boolean expression is true, code within the loop will keep executing. There are two types of while loops, normal while loops and a do-while loop.
While loops check the boolean statement before running the code within them during each loop, therefore if the statement is false the code will not run at all.


Do-while loops check the boolean expression after having run the code within them during each loop. Therefore the code in the do-while loop will always execute at least once even if the boolean expression is not true to begin with.


Using variable in for-loops
When creating a for-loop, you may realise that we initialize a variable which is used to tell Java how many times you want the code in the loop to execute. Within the loop we can actually use this variable (most commonly called i or j) as a normal variable.


Breaking out of a Loop
If your code repeats infinitely because the boolean expression is wrong, it is called an infinite loop and will crash your computer since it continues to take up memory. To break out of any loop regardless of whether it's infinite or not, we use the break command


Recursion
Recursion is a way to code in place of using loops where the method used calls upon itself repeatedly, breaking the problem down into its simplest form, called the base case.


In the code example above, we have a method to calculate an factorial of a number using recursion. Factorial of a number is having the number multiply by every integer decrement between it and 1.
For example factorial of 6 (!6) is 6 x 5 x 4 x 3 x 2 x1 = 720.
The first if statement of this recursive method is the base case, where when the number in the argument is 1 then return 1 (since 1 x 1 = 1 and 1 times anything is trivial).
The following return statement states that the number passed in as the argument gets multiplied by what returned when calling itself with the number passed in decremented by 1.
Essentially it states that !6 is !5 times !4 times !3 times !2 times !1, and 1 returns 1.
​
Almost all methods or algorithms that use loop and be done using recursion and vise versa.