do...while Loop
The do...while
loop runs the block at least once, then repeats while the condition remains true.
The condition is checked after each iteration. Use break
to exit early and continue
to skip to the next check.
Syntax
do {
// body
} while (condition);
Examples
// ১) অন্তত একবার চলবে
let i = 10;
করুন {
কনসোল.লগ("i =", i); // i < 5 না হলেও একবার চলবে
i++;
} যতক্ষণ (i < 5);
// ২) অ্যারে ইটারেশন (সূচক দিয়ে)
const সংখ্যা = [2, 4, 6];
let idx = 0;
করুন {
ছাপাও(সংখ্যা[idx]);
idx++;
} যতক্ষণ (idx < সংখ্যা.length);
// ৩) continue / break সহ
let n = 0;
করুন {
n++;
যদি (n % 2 === 0) চলুন; // জোড় স্কিপ
দেখাও("odd:", n);
যদি (n >= 7) থামুন; // আগে বের হও
} যতক্ষণ (n < 10);
Notes
- The body executes before the condition check, so it always runs at least once.
- Don’t forget the semicolon (
;
) after the closingwhile (condition)
. continue
jumps to the condition check;break
exits the loop.- Prefer
while
/for
unless you specifically need “run once, then check” semantics.