top of page

For loops

A for loop is the most basic and common form of loop and repetition. Code in the for loop will run a fixed amount of times. The amount of times the code will loop through is set using a variable which counts up or down until it reaches a certain number. 

for.PNG
for exe.PNG

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. 

  1.  int i = 0: this part is the starting point for the counter variable i

  2. 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.

  3. 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

image.png
image.png

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. 

while.PNG
while exe.PNG

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.

do while.PNG
do while exe.PNG

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.

i.PNG
i exe.PNG

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

break.PNG
break exe.PNG

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. 

image.png
image.png

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.

© 2024 by Samuel Wong.
Powered and secured by Wix

Call

(1) + 437-216-9455

Contact

@Brebeuf STEAM

  • Twitter
  • Instagram
bottom of page