Tutorialsteacher

Follow Us

Articles
  • C#
  • C# OOP
  • ASP.NET Core
  • ASP.NET MVC
  • LINQ
  • Inversion of Control (IoC)
  • Web API
  • JavaScript
  • TypeScript
  • jQuery
  • Angular 11
  • Node.js
  • D3.js
  • Sass
  • Python
  • Go lang
  • HTTPS (SSL)
  • Regex
  • SQL
  • SQL Server
  • PostgreSQL
  • MongoDB
  • TypeScript - Get Started
  • TypeScript - Overview
  • TypeScript - Installation
  • TypeScript - First Program
  • TypeScript - Type Annotation
  • TypeScript - Variable
  • Data Types - Number
  • String
  • Boolean
  • Array
  • Tuple
  • Enum
  • Union
  • Any
  • Void
  • Never
  • Type Inference
  • Type Assertion
  • if Statement
  • switch Statement
  • for Loop
  • while Loop
  • Function
  • Arrow Function
  • Function Overloading
  • Rest Parameters
  • Interface
  • Class
  • Abstract Class
  • Data Modifiers
  • ReadOnly
  • Static
  • Module
  • Compiling a Module
  • Namespace
  • Generic
  • Generic Interface
  • Generic Class
  • Compiling Project
  • Build Tools
  • Convert JavaScript to TypeScript
Entity Framework Extensions - Boost EF Core 9
  Bulk Insert
  Bulk Delete
  Bulk Update
  Bulk Merge

TypeScript - while Loop

The while loop is another type of loop that checks for a specified condition before beginning to execute the block of statements. The loop runs until the condition value is met.

Syntax:
while (condition expression) 

The condition expression checks for a specified condition before running the block of code.

Example: while loop
let i: number = 2;

while (i < 4) {
    console.log( "Block statement execution no." + i )
    i++;
}
Result:
Block statement execution no.2 Block statement execution no.3

In the above example, we declared a variable i with the value 2. The while loop checks for the value of i before executing the while block of code. If i < 4, then it executes the block. Within the block of code, we have a statement to increment the value of i by 1. This means, the while loop runs two times, for i = 2, and i = 3.

Note that, in the above example, if we hadn't incremented the value of i inside the loop, every time the loop condition was checked, i would have remained the same, i.e. with the initial value of 2. Since the condition i < 4 would have always been true, the loop would have run infinite times.

do..while loop

The do..while loop is similar to the while loop, except that the condition is given at the end of the loop. The do..while loop runs the block of code at least once before checking for the specified condition. For the rest of the iterations, it runs the block of code only if the specified condition is met.

Syntax:
do while (condition expression);
Example: do..while loop
let i: number = 2;
do {
    console.log("Block statement execution no." + i )
    i++;
} while ( i &lt; 4)
Result:
Block statement execution no.2 Block statement execution no.3

In the above example, we have a variable i initialized with value 2. The loop runs the block of code once, prints "Block statement execution no.2" and increments i to value 3. The loop then checks for the condition i < 4. Since i is less than 4, it runs the loop again, this time printing "Block statement execution no.3" and then incrementing the value of i to 4. The condition i < 4 is evaluated again. This time the condition evaluates to false and the do...while loop ends.

Let's check whether the loop runs if the value of i is initialized to 4 before the loop begins:

let i: number = 4;
do {
    console.log( "Block statement execution no." + i )
    i++;
} while ( i &lt; 4)
Result:
Block statement execution no.4

As you can see in the example above, even though the condition in the do...while loop is i < 4, the loop still runs once and prints "Block statement execution no.4". When the condition is evaluated at the end of the block, i is 4 and the condition evaluates to false, hence ending the loop!

TUTORIALSTEACHER.COM

TutorialsTeacher.com is your authoritative source for comprehensive technologies tutorials, tailored to guide you through mastering various web and other technologies through a step-by-step approach.

Our content helps you to learn technologies easily and quickly for learners of all levels. By accessing this platform, you acknowledge that you have reviewed and consented to abide by our Terms of Use and Privacy Policy, designed to safeguard your experience and privacy rights.

[email protected]

ABOUT USTERMS OF USEPRIVACY POLICY
copywrite-symbol

2024 TutorialsTeacher.com. (v 1.2) All Rights Reserved.