Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
While-Loop | Loops
Introduction to TypeScript
course content

Conteúdo do Curso

Introduction to TypeScript

Introduction to TypeScript

1. TypeScript Fundamentals
2. Conditional Statements
3. Arrays
4. Loops
5. Functions

bookWhile-Loop

Now, we've moved on to loops, and it's time to quickly learn how to work with large amounts of data. Loops help to repeat a specific action until a certain condition is met, which will stop the loop.

At the beginning of each loop iteration, the condition is checked, and if it evaluates to true, the loop will execute once, and the condition will be checked again until it returns false. When the condition evaluates to false, the loop will stop.

Theory is good, but let's move on to practice. Introducing the while loop. This loop will perform a specific action as long as the condition remains true. It's the simplest and the first of all loops. The parent of all other loops and my good friend. Syntax:

The syntax is very simple and easy to remember, but it's best remembered through practice. Let's look at a practical example of using the while loop, where we will create a new variable of type boolean, and it will be our condition:

123456789
let condition: boolean = true; let number_of_rabbits: number = 2; while (condition) { number_of_rabbits = number_of_rabbits * 2; if (number_of_rabbits > 50) { condition = false; } } console.log(`Total number of rabbits is ${number_of_rabbits}`)
copy

Using a loop, we multiplied the number of rabbits considering that every 2 rabbits would produce offspring of two new rabbits. However, we had a condition that there should not be more than 50 rabbits.

But why did we end up with 64 rabbits? We won't be able to feed such a large number of rabbits! The GIF below explains why this happens.

Let's edit the code so that the extra rabbits go to another farm. What happens to them on the other farm should not concern us.

12345678910
let condition: boolean = true; let number_of_rabbits: number = 2; while (condition) { number_of_rabbits = number_of_rabbits * 2; if (number_of_rabbits > 50) { condition = false; } } console.log(`Total number of rabbits is ${number_of_rabbits}`) console.log(`Extra rabbits: ${number_of_rabbits - 50}`)
copy

Now we see how many rabbits we are sending to another farm. We don't care that they are raising crocodiles on the other farm.

We can also remove the condition from the loop by incorporating it into the condition's body. This way, we will use fewer lines of code and improve the code overall.

123456
let number_of_rabbits = 2; while (number_of_rabbits < 50) { number_of_rabbits = number_of_rabbits * 2; } console.log(`Total number of rabbits is ${number_of_rabbits}`) console.log(`Extra rabbits: ${number_of_rabbits - 50}`)
copy
1. What is a loop in programming?
2. Which of the following loops executes a block of code as long as a specified condition is `true`?
What is a loop in programming?

What is a loop in programming?

Selecione a resposta correta

Which of the following loops executes a block of code as long as a specified condition is `true`?

Which of the following loops executes a block of code as long as a specified condition is true?

Selecione a resposta correta

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 4. Capítulo 1
some-alt