প্রোগ্রামিংয়ের মৌলিক ধারণা/জাভা লুপ উদাহরণ
অবয়ব
গণণা
[সম্পাদনা]// এই প্রোগ্রামটি While, Do এবং For লুপ গণনা প্রদর্শন করে ।
//ব্যবহারকারী-নির্ধারিত স্টার্ট, স্টপ এবং ইনক্রিমেন্ট মান
//
// তথ্যসূত্র:
// https://en.wikibooks.org/wiki/C%2B%2B_Programming
import java.util.Scanner;
public class Main {
private static Scanner input = new Scanner(System.in);
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) {
System.out.print(name + " মান লিখুন: ");
while (!input.hasNextInt()) {
System.out.print("দয়া করে একটি পূর্ণসংখ্যা লিখুন: ");
input.next(); // discard invalid input
}
return input.nextInt();
}
public static void whileLoop(int start, int stop, int increment) {
System.out.println("While লুপ গণনা করছে " + start + " থেকে " +
stop + " পর্যন্ত, প্রতি ধাপে " + increment + " করে:");
int count = start;
while (count <= stop) {
System.out.println(count);
count += increment;
}
}
public static void doLoop(int start, int stop, int increment) {
System.out.println("Do লুপ গণনা করছে " + start + " থেকে " +
stop + " পর্যন্ত, প্রতি ধাপে " + increment + " করে:");
int count = start;
do {
System.out.println(count);
count += increment;
} while (count <= stop);
}
public static void forLoop(int start, int stop, int increment) {
System.out.println("For লুপ গণনা করছে " + start + " থেকে " +
stop + " পর্যন্ত, প্রতি ধাপে " + increment + " করে:");
for (int count = start; count <= stop; count += increment) {
System.out.println(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