Updated April 5, 2023
Introduction to TypeScript while loop
In typescript, while loop is defined as a type of indefinite loops where the number of iterations is infinite, which means we will not know how much iteration it will take before beginning the execution of the block of statements where this while loop is mainly used for repeating the set of code based on a specified condition. In general, a while loop in typescript is defined as a loop for executing a certain set of code or instructions when the condition specified in the while loop is satisfied or true. This loop will evaluate the condition given and then executes the code.
Working of while loop in typescript with examples
This article will discuss while loop in typescript, which is used for repeating a set of instructions or set of code until a certain condition is satisfied or is true. In typescript, while loop also works the same as in other programming languages, while loop is an indefinite loop which is said to be a loop that executes an infinite number of times before the execution of the code for the condition specified in the while loop and if this condition is satisfied or true, then it executes the code, and hence this loop executes the statements repeatedly until it evaluates the condition specified is true; therefore such kind of loop can be when the developer has o idea of how many iterations are needed to execute which means when there we don’t know the proper number of iterations to be carried out we can use such indefinite loop known as while loop.
Now let us see the syntax and examples for demonstrating while loop in typescript.
Syntax
While (condition)
{
Statements that need to be executed repeatedly;
}
In the above syntax, we can see we are using the keyword “while” to define the while loop and specify the condition that the codes to be executed the number of times specified until the conditions are true. In simple words, first, when the compiler identified the while loop, it will check for the condition if the condition is false. It will again go back before the loop, and when the condition is true, the statements inside the while loop repeatedly executes until the condition fails. The loop gets terminated; therefore, we say while loop is an infinite loop where we don’t know the number of iterations to be carried out.
Example:
console.log("Demonstration of typescript while loop");
var num:number = 0;
console.log("To display set of numbers in order using while loop")
while(num <=7) {
console.log("The number is " +num);
num++;
}
console.log("The set of given number is "+num);
Output:
In the above program, we can see first to declare any variable we are using var. In the above program, we are printing the set of numbers starting from 0 as we have variable “num: number”, which is of number type is initialized with value as 0, and then we are displaying the numbers less than or equal to 8 as this is a condition provided in the while loop. Therefore, until the condition fails, it will repetitively execute the code within the while loop, where it repeats 8 times in the above code. And the variable acts as a counter, and we are incrementing the value of the variable “num” each time it executes the loop, which means the if the condition is satisfied. As we can see in the above code, we have been printing each number to be displayed as we are printing numbers from 0 to 7, which are 8 numbers to be displayed. Hence, we can say the while loop here is used to display every number using one instruction within the loop, and the condition is set, and if the condition is true, then it will execute the code within the while loop, and it will print the values until the condition fails and then the loop terminates and prints all the values.
Now let us see another practical example where we can use this while loop in displaying the array values in the below section.
Example:
console.log("Demonstration of while loop in array in Typescript");
var i : number = 0;
var arr1 : string[] = ["Educba", "Perl", "Python", "Javascript", "Java"];
console.log("The given array is " + arr1);
while (arr1[i])
{
console.log("The value in the given array is as follows:");
console.log(arr1[i]);
i++;
}
Output:
In the above program, we are trying to display the values in the given array using a while loop. In the above code, we can see; first, we are declaring the index value “i” with an initial value of 0. Then we are defining the array, and the string type within the array is of string type, and the values are declared. Then we are printing the given array using the console.log command. Then we are using a while loop to print each value in the array one at a time; here, when we use while loop, we are passing the condition as array name with specifying the index, where here the condition we specified is different as the compiler will automatically terminate the while loop when it reaches the end of the array without specifying the end index of the array.
In the above code, we can see we are printing each of the values in the given array. So the exact working is; first, it will print the value at the index 0 where i =0 then it will again go back to the loop and print the value at the index 1, and it repeats the process continues until the compiler reaches the end of the array it will print all the values. The output of the above code can be seen in the above screenshot.
Conclusion
In this article, we can conclude that in typescript, the while loop is defined as an indefinite loop for repeating the number of iterations where we are unknown about the number of iterations. Therefore in the above article, we can see we have defined a while loop as a loop that will execute the code within the loop until the condition fails, and the code is executed repeatedly whenever the condition is true. In the above, we have seen different examples for demonstrating the while loop in the typescript.
Recommended Articles
This is a guide to TypeScript while loop. Here we discuss the Working of while loop in typescript with different examples and outputs. You may also have a look at the following articles to learn more –