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

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

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

public class Loops
{
    public static void Main(string[] args)
    {
        int start = GetValue("শুরুর");
        int stop = GetValue("শেষের");
        int increment = GetValue("বৃদ্ধির");
        
        WhileLoop(start, stop, increment);
        DoLoop(start, stop, increment);
        ForLoop(start, stop, increment);
    }
    
    public static int GetValue(string name)
    {
        Console.WriteLine(name + " মান লিখুন:");
        string input = Console.ReadLine();
        int value = Convert.ToInt32(input);
        
        return value;
    }
    
    public static void WhileLoop(int start, int stop, int increment)
    {
        Console.WriteLine("While লুপ গণনা করছে " + start + " থেকে " +
            stop + " পর্যন্ত, প্রতি ধাপে " + increment + " করে:");

        int count = start;
        while (count <= stop)
        {
            Console.WriteLine(count);
            count = count + increment;
        }
    }
    
    public static void DoLoop(int start, int stop, int increment)
    {
        Console.WriteLine("Do লুপ গণনা করছে " + start + " থেকে " +
            stop + " পর্যন্ত, প্রতি ধাপে " + increment + " করে:");

        int count = start;
        do
        {
            Console.WriteLine(count);
            count = count + increment;
        }
        while (count <= stop);
    }
    
    public static void ForLoop(int start, int stop, int increment)
    {
        Console.WriteLine("For লুপ গণনা করছে " + start + " থেকে " +
            stop + " পর্যন্ত, প্রতি ধাপে " + increment + " করে:");

        for (int count = start; count <= stop; count += increment)
        {
            Console.WriteLine(count);
        }
    }
}

আউটপুট

[সম্পাদনা]
শুরুর মান লিখুন:
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