বিষয়বস্তুতে চলুন

প্রোগ্রামিংয়ের মৌলিক ধারণা/জাভাস্ক্রিপ্ট লুপ উদাহরণ

উইকিবই থেকে
// এই প্রোগ্রামটি ব্যবহারকারী-নির্ধারিত স্টার্ট, স্টপ এবং ইনক্রিমেন্ট মান ব্যবহার করে While, Do এবং For লুপ গণনা প্রদর্শন করে । 
// 
// তথ্যসূত্র: 
// https://en.wikibooks.org/wiki/C%2B%2B_Programming
using System;
main();

function main() {
    var start = getValue("শুরুর");
    var stop = getValue("শেষের");
    var increment = getValue("বৃদ্ধির");

    whileLoop(start, stop, increment);
    doLoop(start, stop, increment);
    forLoop(start, stop, increment);
}

function getValue(name) {
    output(name + " মান লিখুন:");
    var value = Number(input());
    return value;
}

function whileLoop(start, stop, increment) {
    output("While লুপ গণনা করছে " + start + " থেকে " + stop + 
        " পর্যন্ত, প্রতি ধাপে " + increment + " করে:");

    var count = start;
    while (count <= stop) {
        output(count);
        count = count + increment;
    }
}

function doLoop(start, stop, increment) {
    output("Do লুপ গণনা করছে " + start + " থেকে " + stop + 
        " পর্যন্ত, প্রতি ধাপে " + increment + " করে:");
    
    var count = start;
    do {
        output(count);
        count = count + increment;
    } while (count <= stop);
}

function forLoop(start, stop, increment) {
    output("For লুপ গণনা করছে " + start + " থেকে " + stop + 
        " পর্যন্ত, প্রতি ধাপে " + increment + " করে:");
    
    for (var count = start; count <= stop; count += increment) {
        output(count);
    }
}

function input(text) {
  if (typeof window === 'object') {
    return prompt(text)
  }
  else if (typeof console === 'object') {
    const rls = require('readline-sync');
    var value = rls.question(text);
    return value;
  }
  else {
    output(text);
    var isr = new java.io.InputStreamReader(java.lang.System.in); 
    var br = new java.io.BufferedReader(isr); 
    var line = br.readLine();
    return line.trim();
  }
}

function output(text) {
  if (typeof document === 'object') {
    document.write(text + "<br>");
  } 
  else if (typeof console === 'object') {
    console.log(text);
  } 
  else {
    print(text);
  }
}

আউটপুট

[সম্পাদনা]
শুরুর মান লিখুন:
1
শেষের মান লিখুন:
5
বৃদ্ধির মান লিখুন:
1

While লুপ গণনা করছে 1 থেকে 5 পর্যন্ত, প্রতি ধাপে 1 করে:
1
2
3
4
5
Do-while লুপ গণনা করছে 1 থেকে 5 পর্যন্ত, প্রতি ধাপে 1 করে:
1
2
3
4
5
For লুপ গণনা করছে 1 থেকে 5 পর্যন্ত, প্রতি ধাপে 1 করে:
1
2
3
4
5