TypeScript do-while loop

TypeScript do while loop is same as that of while loop except that the loop is run atleast once.

Syntax

Following is the syntax of do-while loop :

The set of statement are enclosed in brackets after do keyword. while keyword then follows with the condition that controls the looping mechanism.

</>
Copy
 do {
     // set of statements 
 } while(condition)

The set of statements are executed and then the condition after while keyword is evaluated. If it is true, the set of statements are executed once again and the condition is checked once again, and the looping continues. The loop breaks only when the condition is evaluated to false.

Example – do-while loop

Following is an simple do-while loop example.

example.ts

</>
Copy
var N = 6
var i = 1

do {
    console.log(i)
    i++ // updating control variable
} while(i<N)

Output

1
2
3
4
5

Example – do-while loop – execute atleast once

Following is an example with condition evaluating to false for the very first evaluation.

example.ts

</>
Copy
var N = 0
var i = 1

do {
    console.log(i)
    i++ // updating control variable
} while(i<N)

Output

1

The loop has executed atleast for once even if the condition is straight away false.

Conclusion

With this TypeScript TutorialDo-While loop, that should be a wrap to looping statements in TypeScript.