Hello. In this video, I will show you different ways of writing loops. Most algorithms involve some loops. For instance, sorting a sequence requires iterating on its elements until they are sorted. Finding the shortest path between two points in a graph requires iterating under graph nodes and edges until the destination is reached and so on. This video introduces several ways of performing loops by iterating on the standard collection, by using the control structure while you can write so-called imperative loops and by writing recursive methods, you can write so-called functional loops. You will see that depending on the situation, one style of loops can fit better your problem than the others. As an example, let's write a method that computes the factorial of a given integer. The factorial of n is the product of all the positive integers less than or equal to n. We want to write something like this, 1 times 2 times, etc times n minus 2 times n minus 1 times n. We can perform this computation by using the standard collections. We have already seen that the collections have operation that iterate on their elements. The most general one is foldLeft. To implement factorial with foldLeft, we need to generate all the numbers from one to n and to multiply every number with the previous result, starting with one. Actually, there is already an operation product in the standard collections so you could just use it like here. But I wanted to show you the general way of iterating over a collection of elements and computing something out of them by using foldLeft. By the way, the syntax I've used one to n creates a range, which is another type of Scala collection that models a range of values. Here are some examples. One to four produces a range with the elements 1, 2, 3, and 4. You can also specify the step between the element zero to 10 by two produces a range with element 0, 2, 4, 6, 8, 10. Instead of to you can use until to exclude the end value from the range. Here, zero is excluded from the range so the last value is one. The operations to and until are just regular operations under type int. We could have written one dot to of four, like this. Since ranges are standard collections, they have all the usual operations of sequences like map, filter, and so on. Here is another way to implement factorial. This time with an imperative loop. We define a first variable initialized to one, which will accumulate the result as we iterate through the loop. A second variable initialized to one, which represents the current number in every iteration of the loop. The body of the while loop will be executed until we have reached the final number n. In every iteration of the loop, we multiply the accumulated result with the current number. The keyword var introduces a variable definition. The difference between val and var is that you can reassign a new value to a var over time. For reference, the general syntax of the while statement is while condition do statement. Not that scattered too had a slightly different syntax. The condition was written between parentheses and there was no do keyword. The body of the loop was written between braces. Finally, here is the last alternative to implement factorial with a recursive method. Here, factorial of n is defined in terms of factorial of n minus 1, which is itself defined in terms of factorial of n minus 2, and so on until we reach factorial of zero, which immediately terminates the loop. Let's have a look at the evaluation steps of the expression factorial of three. Three is different from zero. Factorial of three evaluates to the else branch, which is 3 times factorial of 3 minus 1. We need to evaluate factorial of 3 minus 1, which is factorial of two. Again, two is different from zero, so it returns 2 times factorial of 2 minus 1. We need to evaluate factorial of 2 minus 1, which is 1 times factorial of 1 minus 1. Last factorial of 1 minus 1 is factorial of zero, which returns one. We are left with this multiplication, which evaluates to six. To summarize, looping can be achieved with several approaches, by iterating on collections, by using a while statement or by using a recursive function. In the next video, we will compare these approaches and more examples.