TypeScript do while loop
The do-while loop is similar to the while loop except that the conditional expression is tested at the end of the loop. The do-while statement executes the block of statements within its braces as long as its conditional expression is true. When you use a do-while statement, the condition is tested at the end of the do-while loop. this means the code will always be executed at least once.
Syntax
- do {}
- while (initialize counter < condition);
The following example shows the palindrome of a given number. A palindrome number is a number that remains the same when its digits are reversed. In this example, I have a dowhile class and define a dowhile loop. Let's see how I implement the while loop in TypeScript. Let's use the following.
Step 1
Open Visual Studio 2012 and click "File" -> "New" -> "Project...". A window is opened. In this window, click HTML Application for TypeScript under Visual C#. Give the name of your application as "palindrome" and then click ok.
Step 2
After this session, the project has been created; a new window is opened on the right side. This window is called the Solution Explorer. The Solution Explorer contains the ts file, js file, css file, and html file.
Coding
palindrome.ts
- class dowhile {
- constructor() {}
- palimdrome() {
- var n: number;
- var s = 0;
- n = parseInt(prompt("Enter a Number for Palindrome or Not"));
- var temp = n;
- do {
- s = s * 10;
- s = s + temp % 10;
- temp = Math.floor(temp / 10);
- }
- while (temp != 0);
- if (s === n) {
- var span = document.createElement("span");
- span.innerText = "Number is palindrome->" + n;
- document.body.appendChild(span);
- } else {
- var span = document.createElement("span");
- span.innerText = "Number is not palindrome->" + n;
- document.body.appendChild(span);
- }
- }
- }
- window.onload = () => {
- var greeter = new dowhile();
- greeter.palimdrome();
- };
palindromexample.html
- < !DOCTYPE html >
- <
- html lang = "en"
- xmlns = "http://www.w3.org/1999/xhtml" >
- <
- head >
- <
- meta charset = "utf-8" / >
- <
- title > Palindrome example < /title>
- <
- link rel = "stylesheet"
- href = "app.css"
- type = "text/css" / >
- <
- script src = "app.js" > < /script>
- <
- /head>
- <
- body >
- <
- h1 > Palindrome Example in TypeScript HTML App < /h1>
- <
- div id = "content" / >
- <
- /body>
- <
- /html>
app.js
- var dowhile = (function() {
- function dowhile() {}
- dowhile.prototype.palimdrome = function() {
- var n;
- var s = 0;
- n = parseInt(prompt("Enter a Number for Palindrome or Not"));
- var temp = n;
- do {
- s = s * 10;
- s = s + temp % 10;
- temp = Math.floor(temp / 10);
- } while (temp != 0)
- if (s === n) {
- var span = document.createElement("span");
- span.innerText = "Number is palindrome->" + n;
- document.body.appendChild(span);
- } else {
- var span = document.createElement("span");
- span.innerText = "Number is not palindrome->" + n;
- document.body.appendChild(span);
- }
- };
- return dowhile;
- })();
- window.onload = function() {
- var greeter = new dowhile();
- greeter.palimdrome();
- };
Output 1
Click the "Ok" button.
Output 2
Reference By
http://www.typescriptlang.org/