while Loop
The while loop repeats a block as long as its condition evaluates to true.
The condition is checked before each iteration. Use break to exit early and continue to skip the current iteration.
Syntax
while (condition) {
// body
}
Examples
// ১) যতক্ষণ দিয়ে গণনা
let i = 0;
যতক্ষণ (i < 5) {
কনসোল.লগ("i =", i);
i++; // স্টেট আপডেট করতে ভুলবেন না
}
// ২) জোড় স্কিপ, নির্দিষ্ট সীমায় থামা
let n = 0;
যতক্ষণ (n < 10) {
n++;
যদি (n % 2 === 0) চলুন; // জোড় বাদ
যদি (n > 7) থামুন; // আগে বের হও
ছাপাও("বিজোড়:", n);
}
// ৩) কিউ ফাঁকা হওয়া পর্যন্ত প্রসেস
const কিউ = ["A", "B", "C"];
যতক্ষণ (কিউ.length > 0) {
const আইটেম = কিউ.shift();
কনসোল.তথ্য("প্রসেসিং", আইটেম);
}
Notes
- Ensure the loop state changes so the condition eventually becomes false; otherwise you’ll create an infinite loop.
- Prefer
forwhen you know the iteration count, andfor...offor arrays/iterables. - Pattern:
while (true) { ... if (stop) break; }is useful for event loops and readers. - If you need the body to run at least once, consider
do ... while.