Ad space (banner)
๐Ÿ”ทVB.NET Lessons
Lesson 8 / 68

Repeating (the While statement)

This lesson covers looping in VB.NET with a While statement, with the aim of choosing correctly between it and a For statement.

While ... End While repeats for as long as the condition holds. Where a For statement counts, a While statement keeps going while a condition is satisfied.

The sample code loops while i is 5 or less, increasing the value by one inside the loop with i += 1. The condition is evaluated at the top of each pass, and execution moves on the moment it becomes false.

A common early stumble is forgetting the finishing condition and ending in an infinite loop. Something inside the loop always has to change the condition.

Both For and While repeat, but the basic rule is to choose on whether the count is known in advance. When it is not, a While statement suits.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run VB.NET directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)