TypeScript Tutorial

TypeScript While Loop

while repeats as long as a condition stays true. Check that the condition can become false.

Repeat while a test is true

A while loop checks a condition, runs a block, then checks again. As long as the condition is true, the block repeats. When the condition is false, TypeScript leaves the loop and continues with the next statement.

Something inside the loop must change the values the condition looks at. If nothing changes, the test never becomes false and the program does not stop.

A complete while program

This loop prints 1 through 5. n starts at 1. Each pass prints, then adds 1. Whenn becomes 6, the condition is false and the loop ends.

Example

let n: number = 1;
while (n <= 5) {
  console.log(n);
  n = n + 1;
}

Compile this in /typescript/try. Then change 5 to another small number. Keep the increment. A missing n = n + 1 is how beginners create an infinite loop.

The three parts you manage

  1. Set a starting value before the loop.
  2. Write a condition that can become false.
  3. Update that value inside the loop.

Forget step 3 and the condition stays true forever. Forget step 1 and the condition uses a value you never set. The for loop in the next chapter packs these three into one line. while leaves them visible, which is useful when the update is not a simple + 1.

Use let for the counter. const cannot be reassigned, so it cannot step.

do while runs the body first

A do / while loop runs the block once, then checks the condition. If the condition is true, it runs again. The body always executes at least once, even when the test starts false.

Example

let n: number = 1;
do {
  console.log(n);
  n = n + 1;
} while (n <= 3);

Notice the semicolon after while (n <= 3). A do / while needs that semicolon. A plain while does not.

When the body should run first

Use do / while when the first pass must happen before you know whether to continue: print a menu, then ask whether to show it again. Use plain while when you might need zero passes: process input only while there is input left.

Example

const n: number = 10;
do {
  console.log("ran once, n is " + n);
} while (n < 0);

n is 10, so n < 0 is false, but the message still prints. A plainwhile (n < 0) would print nothing.

Infinite loops

while (true) never becomes false on its own. Neither does a loop whose counter never changes. The StudyGrid editor will sit there until a time limit cuts it off. Do not submit an example that cannot finish.

If the program never prints and never returns, look for a missing update. Count in your head: after enough passes, is the condition false? If you cannot say yes, the loop is unbounded.

A later chapter covers break, which can leave a loop early. Until then, make the condition do the stopping.

while versus for

whilefor
Use whenYou wait on a conditionYou know the count
CounterYou set and update it yourselfInit, test, and step sit in one line
Zero passesYes, if the test starts falseYes, same idea

Next: the for loop, which is the usual choice for a known number of repeats.

FAQ: TypeScript While Loop

Common questions about this page.

What is the StudyGrid TypeScript tutorial?

The StudyGrid TypeScript tutorial follows the same chapter rhythm as C++: syntax, types, input, loops, functions, classes, generics, maps, and lambdas. Each chapter has copy-and-run examples.

Should I run typescript while loop examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn typescript while loop in this TypeScript TypeScript lesson (TypeScript While Loop).

Is the TypeScript editor the same as Try Python or Try C++?

No. Try TypeScript type-checks with tsc at /typescript/try and shows stdout plus compiler messages. Try Python stays at /try. Try C++ stays at /cpp/try. TypeScript lessons never open those editors.

Do I need to install a compiler to learn TypeScript?

No. Open a chapter, click Try it in TypeScript, and compile in the browser. You can also download a .ts file and compile locally with tsc.

Where should I start the TypeScript tutorial?

Start at TypeScript Intro, then Get Started and Syntax. After the first program, continue to output, variables, and if-else. After classes, open TypeScript Examples, then generics, Map, and arrow functions. Use Next at the bottom of each chapter.

Is the TypeScript tutorial free?

Yes. The TypeScript workshop on StudyGrid (studygrid.in) is free: dashboard, chapters, and the compile-and-run editor.