Iteration (Loops) (ထပ်ခါထပ်ခါ လုပ်ခြင်း)

B2.3.3 - Construct programs that utilize looping structures to perform repeated actions.
မြန်မာလို: လုပ်ဆောင်ချက်တွေကို ထပ်ခါထပ်ခါ လုပ်ဖို့ looping structure (loop ပုံစံ) တွေကို သုံးတဲ့ program တွေကို ကိုယ်တိုင် တည်ဆောက်နိုင်ရပါမယ်။


အဓိက အယူအဆများ

Iteration (ထပ်ခါထပ်ခါ လုပ်ခြင်း) က code block (code အပိုင်း) တစ်ခုကို အကြိမ်ကြိမ် ပြန်မရေးဘဲ ထပ်ခါထပ်ခါ run စေပါတယ်။ Java မှာ loop (ထပ်ခါထပ်ခါ run တဲ့ code အပိုင်း) အမျိုးအစား သုံးမျိုး ရှိပြီး ဘယ်ဟာကို ရွေးမလဲဆိုတာ အခြေအနေပေါ် မူတည်ပါတယ်။ (Python မှာတော့ while နဲ့ for နှစ်မျိုးပဲ ရှိပါတယ်။ Python မှာ do-while မရှိပါဘူး၊ အဲဒီလို ရလဒ်မျိုး ဘယ်လို ရအောင် လုပ်မလဲဆိုတာ ဥပမာ 2 မှာ ပြထားပါတယ်။)

Loop အမျိုးအစား Condition စစ်တဲ့ အချိန် အသင့်တော်ဆုံး အခြေအနေ
while Loop တစ်ပတ် မစခင် တိုင်း Condition (မှန်လား မှားလား စစ်တဲ့ အခြေအနေ) true ဖြစ်နေသရွေ့ ထပ်လုပ်တဲ့အခါ၊ တစ်ကြိမ်မှ မ run တာလည်း ဖြစ်နိုင်ပါတယ်
do-while Loop တစ်ပတ် ပြီးတိုင်း Body (loop ထဲမှာ ထပ်ခါထပ်ခါ run တဲ့ code) က အနည်းဆုံး တစ်ကြိမ် run ရမယ့်အခါ
for Loop တစ်ပတ် မစခင် တိုင်း ဘယ်နှစ်ကြိမ် ထပ်လုပ်ရမလဲ အတိအကျ သိတဲ့အခါ

Pre-condition loop (body ကို မ run ခင် condition စစ်တဲ့ loop) ဖြစ်တဲ့ while နဲ့ for က body ကို run မလုပ်ခင် condition ကို စစ်ပါတယ်၊ ဒါကြောင့် တစ်ကြိမ်မှ မ run တာ ဖြစ်နိုင်ပါတယ်။ Post-condition loop (body run ပြီးမှ condition စစ်တဲ့ loop) ဖြစ်တဲ့ do-while ကတော့ run ပြီးမှ စစ်လို့ အနည်းဆုံး တစ်ကြိမ် run တာ သေချာပါတယ်။ ဘယ် loop ကို သုံးသုံး body ထဲက တစ်ခုခုက condition ကို false ဖြစ်လာတဲ့ ဘက်ကို ရွှေ့ပေးရပါမယ်။ မဟုတ်ရင် infinite loop (ဘယ်တော့မှ မရပ်တဲ့ loop) ကို ရေးမိသွားပါလိမ့်မယ်။


အဆင့်ဆင့် ရှင်းပြထားတဲ့ ဥပမာများ

ဥပမာ 1: while loop၊ countdown (နောက်ပြန် ရေတွက်ခြင်း)

while loop က loop တစ်ပတ် မစခင် တိုင်း condition ကို စစ်ပါတယ်။ အစကတည်းက condition က false ဖြစ်နေရင် body က တစ်ကြိမ်မှ မ run ပါဘူး။

int count = 5;
while (count > 0) {
    System.out.println(count);
    count--;
}
System.out.println("Go!");
count = 5
while count > 0:
    print(count)
    count = count - 1
print("Go!")

Output (program က ထုတ်ပြတဲ့ ရလဒ်):

5
4
3
2
1
Go!

အလုပ်လုပ်ပုံ:

  • count က 5 ကနေ စပါတယ်။ count > 0 condition က true ဖြစ်လို့ body က run ပါတယ်။
  • Loop တစ်ပတ်စီမှာ count ကို print ထုတ်ပြီး decrement (တန်ဖိုးကို 1 လျှော့ခြင်း) လုပ်ပါတယ်။
  • count က 0 ရောက်တဲ့အခါ condition က false ဖြစ်ပြီး loop ပြီးသွားပါတယ်။

ဥပမာ 2: do-while loop၊ input validation (user ထည့်လိုက်တဲ့ input မှန်မမှန် စစ်ခြင်း)

do-while loop က body ကို အရင် run ပြီးမှ condition ကို စစ်ပါတယ်။ ဒါကြောင့် အနည်းဆုံး တစ်ကြိမ် run တာ သေချာပြီး menu တွေနဲ့ input validation အတွက် အသင့်တော်ဆုံးပါ။

import java.util.Scanner;

Scanner input = new Scanner(System.in);
int number;

do {
    System.out.print("Enter a number between 1 and 10: ");
    number = input.nextInt();
} while (number < 1 || number > 10);

System.out.println("You entered: " + number);
# Python has no do-while loop.
# The IB pattern: read once BEFORE the loop (priming read),
# then repeat while the value is invalid.
number = int(input("Enter a number between 1 and 10: "))
while number < 1 or number > 10:
    number = int(input("Enter a number between 1 and 10: "))

print("You entered:", number)

Python code ထဲက comment တွေက ဒီလို ပြောထားပါတယ်: Python မှာ do-while loop မရှိလို့ IB က သုံးတဲ့ ပုံစံက loop မစခင် input ကို တစ်ကြိမ် ကြိုဖတ်ပြီး (priming read: loop မစခင် ပထမ input ကို ကြိုဖတ်ခြင်း) တန်ဖိုး မမှန်သရွေ့ ထပ်ဖတ်တာပါ။

အလုပ်လုပ်ပုံ:

  • Prompt (user ကို input တောင်းတဲ့ စာ) က အနည်းဆုံး တစ်ကြိမ် run ပါတယ်။ ဘာကိုမှ မစစ်ခင် user က တစ်ခုခု အရင် ထည့်ပေးရမှာ ဖြစ်လို့ပါ။
  • number က 1-10 အပြင်ဘက်မှာ ရှိရင် condition က true ဖြစ်ပြီး loop ထပ်ပတ်ပါတယ်။
  • မှန်ကန်တဲ့ input (program ထဲ ထည့်ပေးတဲ့ ဒေတာ) ကို ထည့်လိုက်တာနဲ့ condition က false ဖြစ်ပြီး loop ပြီးသွားပါတယ်။

“အရင် မေး၊ ပြီးမှ စစ်” ဆိုတဲ့ အခြေအနေမျိုးအတွက် do-while ပုံစံက အသင့်တော်ဆုံး ရွေးချယ်မှုပါ။ while loop ကို သုံးရင် prompt ကို loop မစခင်မှာ တစ်ခါ၊ loop ထဲမှာ တစ်ခါ ထပ်ရေးရပါလိမ့်မယ်။


ဥပမာ 3: for loop၊ မြှောက်ဇယား

for loop က initialisation (စတင် တန်ဖိုး သတ်မှတ်ခြင်း)၊ condition နဲ့ update (loop တစ်ပတ်ပြီးတိုင်း တန်ဖိုး ပြောင်းခြင်း) ကို line တစ်ကြောင်းတည်းမှာ ပေါင်းရေးပါတယ်။ ဘယ်နှစ်ပတ် ပတ်ရမလဲ သိထားတဲ့အခါ အသင့်တော်ဆုံးပါ။

int tableNum = 7;
System.out.println("Multiplication table for " + tableNum + ":");

for (int i = 1; i <= 12; i++) {
    System.out.println(tableNum + " x " + i + " = " + (tableNum * i));
}
table_num = 7
print("Multiplication table for " + str(table_num) + ":")

for i in range(1, 13):    # range(1, 13) gives i = 1 to 12 - the stop value is not included
    print(table_num, "x", i, "=", table_num * i)

Output:

Multiplication table for 7:
7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
...
7 x 12 = 84

ဖွဲ့စည်းပုံ: for (initialise; condition; update)

  • Initialise: int i = 1၊ loop မစခင် တစ်ကြိမ်ပဲ run ပါတယ်
  • Condition: i <= 12၊ loop တစ်ပတ် မစခင် တိုင်း စစ်ပါတယ်၊ false ဖြစ်ရင် loop ရပ်ပါတယ်
  • Update: i++၊ loop တစ်ပတ် ပြီးတိုင်း run ပါတယ်

ဥပမာ 4: Accumulator pattern (ရလဒ်ကို တဖြည်းဖြည်း စုပေါင်းသွားတဲ့ ပုံစံ)၊ running total (တဖြည်းဖြည်း ပေါင်းသွားတဲ့ စုစုပေါင်း)

အသုံးများတဲ့ loop ပုံစံ တစ်ခုပါ: loop တစ်ပတ်ချင်းစီမှာ ရလဒ်ကို variable (တန်ဖိုး သိမ်းထားတဲ့ နာမည်ပေးထားတဲ့ နေရာ) တစ်ခုထဲ စုပေါင်းသွားပါတယ်။

import java.util.Scanner;

Scanner input = new Scanner(System.in);
int total = 0;
int count = 0;

System.out.println("Enter numbers to sum (enter -1 to stop):");

int num = input.nextInt();
while (num != -1) {
    total += num;
    count++;
    num = input.nextInt();
}

System.out.println("Sum: " + total);
System.out.println("Numbers entered: " + count);
if (count > 0) {
    System.out.println("Average: " + (double) total / count);
}
total = 0
count = 0

print("Enter numbers to sum (enter -1 to stop):")

num = int(input())
while num != -1:
    total = total + num
    count = count + 1
    num = int(input())

print("Sum:", total)
print("Numbers entered:", count)
if count > 0:
    print("Average:", total / count)    # / always gives a decimal in Python - no cast needed

အဓိက ပုံစံများ:

  • Accumulator: total += num၊ ပေါင်းလဒ်ကို တဖြည်းဖြည်း စုပေါင်းသွားပါတယ်
  • Counter (အရေအတွက် မှတ်တဲ့ variable): count++၊ တန်ဖိုး ဘယ်နှစ်ခု လုပ်ဆောင်ပြီးပြီလဲ ရေတွက်ပါတယ်
  • Sentinel value (ဒေတာ ထည့်တာ ပြီးပြီလို့ ပြတဲ့ အထူး တန်ဖိုး): -1 က “ရပ်” ဆိုတဲ့ အချက်ပြပါ၊ sentinel ကို ထည့်တဲ့အထိ loop က ဆက် run ပါတယ်

Sentinel value (ဒီမှာတော့ -1) ကို total ထဲ မပေါင်းပါဘူး။ ပထမ တန်ဖိုးကို loop မစခင် အမြဲ ဖတ်ပါ၊ နောက်ထပ် တန်ဖိုးတွေကိုတော့ loop body ရဲ့ အဆုံးမှာ ဖတ်ပါ။


ဥပမာ 5: Nested loop (loop ထဲမှာ ထပ်ပါတဲ့ loop) များ၊ ကြယ်ပုံ ဆွဲခြင်း

Loop တွေကို တခြား loop တွေထဲမှာ ထည့်ရေးလို့ ရပါတယ်။ Outer loop (အပြင်ဘက် loop) တစ်ပတ် ပတ်တိုင်း inner loop (အတွင်းဘက် loop) က အစကနေ အဆုံးအထိ ပြည့်ပြည့်စုံစုံ ပတ်ပါတယ်။

int rows = 5;
for (int row = 1; row <= rows; row++) {
    for (int col = 1; col <= row; col++) {
        System.out.print("* ");
    }
    System.out.println();
}
rows = 5
for row in range(1, rows + 1):
    for col in range(1, row + 1):
        print("* ", end="")    # end="" keeps printing on the same line
    print()                    # move to the next line

Output:

*
* *
* * *
* * * *
* * * * *

အလုပ်လုပ်ပုံ:

  • Outer loop က ဘယ် row (အတန်း) မှာ ရှိနေလဲ (1 ကနေ 5) ကို ထိန်းပါတယ်။
  • Inner loop က ကြယ်တွေကို print ထုတ်ပါတယ်၊ ကြယ် အရေအတွက်က လက်ရှိ row နံပါတ်နဲ့ တူပါတယ်။
  • Inner loop ပြီးတဲ့နောက် System.out.println() က နောက် line ကို ဆင်းပေးပါတယ်။

Nested loop တွေကို trace (တစ်ဆင့်ချင်း လိုက်ကြည့်ပြီး မှတ်) လုပ်တဲ့အခါ loop variable နှစ်ခုလုံးကို သီးခြားစီ မှတ်ပါ။ Outer loop တစ်ပတ် ပတ်တိုင်း inner loop variable က သူ့ရဲ့ စတင်တန်ဖိုးကို ပြန်ရောက်သွားပါတယ်။


ဥပမာ 6: စုငွေ ပန်းတိုင်၊ ပုံစံတွေကို ပေါင်းသုံးခြင်း

ကျောင်းသား တစ်ယောက်က 120 တန်တဲ့ ပစ္စည်း တစ်ခု ဝယ်ဖို့ ငွေစုနေပါတယ်။ ပထမ အပတ်မှာ 10 ထည့်ပြီး အဲဒီနောက် အပတ်တိုင်း အရင်အပတ်ထက် 5 ပိုထည့်ပါတယ်။ ပန်းတိုင် မရောက်သေးရင်တောင် 8 ပတ်ပြီးရင် ရပ်ပါတယ်။ Loop ရပ်ဖို့ အကြောင်း နှစ်ခု ရှိပါတယ်: ပန်းတိုင် ရောက်သွားတာ၊ ဒါမှမဟုတ် အပတ်တွေ ကုန်သွားတာပါ။ ဒီ code မှာ counter၊ running total၊ boolean flag (true/false နဲ့ တစ်ခုခု ဖြစ်ခဲ့လား မှတ်ထားတဲ့ variable) နဲ့ ရပ်တဲ့ condition နှစ်ခုပါတဲ့ while loop ကို ပေါင်းသုံးထားပါတယ်။

int goal = 120;        // amount to save
int maxWeeks = 8;      // stop after 8 weeks even if the goal is not met
int week = 0;
int saved = 0;
boolean reached = false;

while (!reached && week < maxWeeks) {
    week++;
    int deposit = 10 + 5 * (week - 1);   // 10 in week 1, then 5 more each week
    saved += deposit;
    System.out.println("Week " + week + ": paid in " + deposit + ", total " + saved);

    if (saved >= goal) {
        reached = true;
    }
}

if (reached) {
    System.out.println("Goal reached in week " + week + ".");
} else {
    System.out.println("Goal not reached after " + maxWeeks + " weeks. Total saved: " + saved + ".");
}
goal = 120         # amount to save
max_weeks = 8      # stop after 8 weeks even if the goal is not met
week = 0
saved = 0
reached = False

while reached == False and week < max_weeks:
    week = week + 1
    deposit = 10 + 5 * (week - 1)    # 10 in week 1, then 5 more each week
    saved = saved + deposit
    print("Week " + str(week) + ": paid in " + str(deposit) + ", total " + str(saved))

    if saved >= goal:
        reached = True

if reached == True:
    print("Goal reached in week " + str(week) + ".")
else:
    print("Goal not reached after " + str(max_weeks) + " weeks. Total saved: " + str(saved) + ".")

Output:

Week 1: paid in 10, total 10
Week 2: paid in 15, total 25
Week 3: paid in 20, total 45
Week 4: paid in 25, total 70
Week 5: paid in 30, total 100
Week 6: paid in 35, total 135
Goal reached in week 6.

အဓိက ပုံစံများ:

  • Flag variable: reached က ပန်းတိုင် ရောက်ပြီလား မှတ်ထားပြီး loop condition (loop ဆက်လုပ်မလား စစ်တဲ့ အခြေအနေ) က loop တစ်ပတ် မစခင် တိုင်း အဲဒါကို စစ်ပါတယ်
  • Compound condition (condition အများကို ပေါင်းထားတဲ့ condition): !reached && week < maxWeeks က loop ကို ရပ်ဖို့ အကြောင်း နှစ်ခု ပေးပါတယ်
  • Counter: week က loop ပတ်တဲ့ အကြိမ်ရေကို ကန့်သတ်ပြီး deposit (ထည့်ငွေ) ကိုလည်း သူ့ကနေ တွက်ပါတယ်
  • Accumulator: ဥပမာ 4 လိုပဲ saved က running total ကို သိမ်းထားပါတယ်

goal ကို 250 လို့ ပြောင်းပြီး ထပ် run ကြည့်ပါ။ အခု loop က 8 ပတ်လုံး run ပြီး စုစုပေါင်းက 220 ရောက်ပါတယ်။ ပြီးတော့ else branch (condition အရ run မယ့် code လမ်းကြောင်း) က Goal not reached after 8 weeks. Total saved: 220. ကို print ထုတ်ပါတယ်။


Quick Check (အမြန် စစ်ဆေးမေးခွန်း)

Q1. ဘယ် loop က body ကို အနည်းဆုံး တစ်ကြိမ် run တာ သေချာသလဲ။

Q2. for (int i = 0; i < 5; i++) { System.out.println(i); } ရဲ့ body က ဘယ်နှစ်ကြိမ် run သလဲ။

Q3. while loop ရဲ့ condition ကို ပထမဆုံး စစ်တဲ့အခါ false ဖြစ်နေရင် ဘာဖြစ်မလဲ။

Q4. for (int i = 0; i < 10; i++) မှာ i++ က ဘယ်အချိန် run သလဲ။

Q5. ဒီ code က ဘာလုပ်သလဲ။
int count = 5; while (count > 0) { System.out.println(count); }

Q6. Inner body က ဘယ်နှစ်ကြိမ် run သလဲ။
for (int row = 1; row <= 3; row++) { for (int col = 1; col <= row; col++) { System.out.print("*"); } }


Trace Exercise (code ကို တစ်ဆင့်ချင်း လိုက်ကြည့်ခြင်း)

ဒီ loop ကို trace လုပ်ပါ: loop တစ်ပတ် ပြီးတိုင်း i နဲ့ product ရဲ့ တန်ဖိုးတွေကို ဖြည့်ပါ။

int product = 1;
int n = 4;
for (int i = 1; i <= n; i++) {
    product = product * i;
}
product = 1
n = 4
for i in range(1, n + 1):
    product = product * i

နောက်ဆုံး မေးခွန်းရဲ့ အဖြေကို English စကားလုံး တစ်လုံးနဲ့ ရိုက်ထည့်ပါ၊ ဥပမာ factorial (1 ကနေ n အထိ ကိန်းတွေရဲ့ မြှောက်လဒ်) ဒါမှမဟုတ် sum (ပေါင်းလဒ်)။

Loop အကြိမ်i (body အစမှာ)body ပြီးနောက် product
1
2
3
4

Loop ပြီးသွားတဲ့အခါ product က ဘယ်လောက်လဲ။

ဒီ program က ဘာကို တွက်ထုတ်တာလဲ။


Trace Exercise 2: Nested Loop (loop ထဲမှာ ထပ်ပါတဲ့ loop)

Nested loop ကို trace လုပ်ပါ: အဆင့်တိုင်းမှာ ထွက်လာမယ့် output ကို ဖြည့်ပါ။

for (int i = 1; i <= 3; i++) {
    for (int j = 1; j <= 2; j++) {
        System.out.println(i + "," + j);
    }
}
for i in range(1, 4):
    for j in range(1, 3):
        print(str(i) + "," + str(j))
အဆင့်ijOutput
1
2
3
4
5
6

Output စုစုပေါင်း ဘယ်နှစ်ကြောင်း ထွက်သလဲ။


Code Completion (code ကွက်လပ် ဖြည့်ခြင်း)

ဒီ program က 2 ကနေ 20 အထိ စုံကိန်း အားလုံးကို print ထုတ်ဖို့ step value (တစ်ခါ တိုးတဲ့ ပမာဏ) 2 နဲ့ while loop ကို သုံးပါတယ်။ မှန်ကန်တဲ့ စတင်တန်ဖိုး၊ ရပ်မယ့် condition နဲ့ တိုးမယ့် ပမာဏကို ဖြည့်ပါ။

int num = ;
while (num <= ) {
    System.out.println(num);
    num += ;
}

ဒီ program က 1 ကနေ n အထိ integer (ကိန်းပြည့်) အားလုံးရဲ့ ပေါင်းလဒ်ကို တွက်ဖို့ for loop ထဲမှာ accumulator pattern ကို သုံးပါတယ်။ Accumulator ကို initialise လုပ်တာ (စတင်တန်ဖိုး ပေးတာ)၊ loop ရဲ့ စတင်တန်ဖိုး သတ်မှတ်တာနဲ့ running total ကို update လုပ်တာကို ဖြည့်ပါ။

int n = 100;
int total = ;
for (int i = ; i <= n; i++) {
    total ;
}
System.out.println("Sum: " + total);

Spot the Error (အမှား ရှာခြင်း)

ဒီ code က while loop သုံးပြီး 1 ကနေ 5 အထိ ကိန်းတွေကို print ထုတ်ရမှာပါ။ ဒါပေမယ့် 5 ပြီးရင် မရပ်ဘဲ ကိန်းတွေကို ဆက် print ထုတ်နေပါတယ်။ အမှားပါတဲ့ line ကို ရွေးပါ၊ ပြီးရင် မှန်ကန်တဲ့ ပြင်နည်းကို ရွေးပါ။

int i = 1;
while (i <= 5) {
    System.out.println(i);
    i--;
}

ဘာမှားနေလဲ၊ ဘယ်လို ပြင်မလဲ။


ဒီ code က for loop သုံးပြီး 1 ကနေ 10 အထိ integer တွေရဲ့ ပေါင်းလဒ်ကို တွက်ရမှာပါ။ ဒါပေမယ့် loop condition မှာ off-by-one error (တစ်ကြိမ် ပိုသွား ဒါမှမဟုတ် လိုသွားတဲ့ အမှား) ရှိလို့ တန်ဖိုး တစ်ခု လွတ်သွားပါတယ်။ မှန်ကန်တဲ့ အဖြေက 55 ဖြစ်ပေမယ့် ဒီ code က 45 ကို print ထုတ်ပါတယ်။ Bug (code ထဲက အမှား) ကို ရှာပါ။

int sum = 0;
for (int i = 1; i < 10; i++) {
    sum += i;
}
System.out.println("Sum of 1 to 10: " + sum);

ဘာမှားနေလဲ၊ ဘယ်လို ပြင်မလဲ။


Predict the Output (output ကို ကြိုခန့်မှန်းခြင်း)

ဒီ code က ပုံမှန် မဟုတ်တဲ့ step value 3 နဲ့ 10 ကနေ နောက်ပြန် ရေတွက်တဲ့ while loop ကို ပြပါတယ်။ x ရဲ့ ဘယ်တန်ဖိုးတွေက loop condition ကို ပြည့်မီလဲ၊ loop က ဘယ်အချိန် ရပ်လဲဆိုတာကို သတိထားကြည့်ပါ။

ဒီ code က ဘာကို print ထုတ်မလဲ။ Output ကို အတိအကျ ရိုက်ထည့်ပါ။

int x = 10;
while (x > 0) {
    System.out.print(x + " ");
    x -= 3;
}
x = 10
while x > 0:
    print(x, end=" ")
    x = x - 3

ဒီ code က nested for loop တွေကို သုံးပြီး row တစ်ခုချင်း တိုလာတဲ့ ကိန်းပုံစံ တစ်ခုကို print ထုတ်ပါတယ်။ Inner loop ရဲ့ စတင်တန်ဖိုးက outer loop variable ပေါ် မူတည်လို့ row တစ်ခုချင်းက မတူတဲ့ ကိန်းကနေ စပါတယ်။ Loop variable နှစ်ခုလုံးကို သေသေချာချာ trace လုပ်ပါ။

ဒီ code က ဘာကို print ထုတ်မလဲ။

for (int i = 1; i <= 4; i++) {
    for (int j = i; j <= 4; j++) {
        System.out.print(j + " ");
    }
    System.out.println();
}
for i in range(1, 5):
    for j in range(i, 5):
        print(j, end=" ")
    print()
ရရမယ့် output:
1 2 3 4
2 3 4
3 4
4

Practice Exercises (လေ့ကျင့်ခန်းများ)

Core (အခြေခံ အဆင့်)

  1. နောက်ပြန် ရေတွက်ခြင်း။ User ကို စတင်မယ့် ကိန်း တစ်ခု မေးပြီး 1 အထိ နောက်ပြန် ရေတွက်ပြပါ၊ ပြီးရင် “Go!” ကို print ထုတ်ပေးတဲ့ program ကို ရေးပါ။ while loop ကို သုံးပါ။

  2. အပိုင်းအခြား တစ်ခုရဲ့ ပေါင်းလဒ်။ for loop ကို သုံးပြီး 1 ကနေ 100 အထိ integer အားလုံးရဲ့ ပေါင်းလဒ်ကို တွက်ပြီး print ထုတ်ပါ။

  3. စုံကိန်းတွေကို print ထုတ်ခြင်း။ for loop ကို သုံးပြီး 2 ကနေ 50 အထိ စုံကိန်း အားလုံးကို space (ကွက်လပ်) ခြားပြီး print ထုတ်ပါ။

  4. Input validation။ do-while loop ကို သုံးပြီး user ကို test score (0-100) တစ်ခု မေးပါ။ မှန်ကန်တဲ့ တန်ဖိုး မထည့်မချင်း ဆက်မေးပါ၊ ပြီးရင် score ကို print ထုတ်ပါ။ (Python မှာတော့ ဥပမာ 2 လို priming read နဲ့ while loop ကို သုံးပါ။)

Extension (တိုးချဲ့ အဆင့်)

  1. FizzBuzz။ 1 ကနေ 30 အထိ ကိန်းတွေကို print ထုတ်ပါ။ 3 ရဲ့ ဆတိုးကိန်း (multiple) တွေအတွက် ကိန်းအစား “Fizz” ကို print ထုတ်ပါ။ 5 ရဲ့ ဆတိုးကိန်းတွေအတွက် “Buzz” ကို print ထုတ်ပါ။ နှစ်ခုလုံးရဲ့ ဆတိုးကိန်းတွေအတွက် “FizzBuzz” ကို print ထုတ်ပါ။

  2. ထောင့်မှန် တြိဂံ။ Nested loop တွေကို သုံးပြီး user ထည့်တဲ့ အမြင့်ရှိတဲ့ * character တွေနဲ့ ထောင့်မှန် တြိဂံ တစ်ခုကို print ထုတ်ပါ:

    *
    * *
    * * *
    * * * *
    
  3. ပျမ်းမျှ တွက်စက်။ User က -1 (sentinel) ကို ထည့်တဲ့အထိ ကိန်းတွေကို ဖတ်ပါ။ အရေအတွက်၊ ပေါင်းလဒ်နဲ့ ပျမ်းမျှကို print ထုတ်ပါ။ ကိန်း တစ်ခုမှ မထည့်တဲ့ အခြေအနေကိုလည်း ကိုင်တွယ်ပါ။

Challenge (စိန်ခေါ်မှု အဆင့်)

  1. ကိန်း ပိရမစ်။ Nested loop တွေကို သုံးပြီး အလယ်ညှိထားတဲ့ ကိန်း ပိရမစ် တစ်ခုကို print ထုတ်ပါ:
        1
       2 3
      4 5 6
     7 8 9 10
    
  2. ရိုးရှင်းတဲ့ menu စနစ်။ do-while menu loop (Python: priming read + while) ပါတဲ့ calculator တစ်ခုကို ဖန်တီးပါ။ ရွေးစရာများ: (1) Add (ပေါင်း)၊ (2) Subtract (နုတ်)၊ (3) Multiply (မြှောက်)၊ (4) Quit (ထွက်)။ Operation (တွက်ချက်မှု) တစ်ခု ပြီးတိုင်း user က ထွက်တဲ့အထိ menu ကို ပြန်သွားပါ။

GitHub Classroom (အလိုအလျောက် စစ်ဆေးတဲ့ test တွေပါတဲ့ programming assignment များ)

Iteration (Java)
Practice while loops, do-while loops, for loops, and nested loop patterns.

A class assignment on GitHub Classroom: use it when your teacher has set it. It needs a free GitHub account.

Working in the Codespace

  1. Accept and open. Accept the assignment above, open the link in the email from GitHub, then press Open in Codespace. In Java, change the status bar from Lightweight to Standard mode.
  2. Write and check. Open src/Iteration.java and complete each TODO. Press Run and compare the output with the comments, or run bash run-tests.sh in the Terminal.
  3. Submit. In Source Control, write a message, press Commit, then Sync Changes. Your teacher’s feedback arrives as a pull request called Feedback.

The full Codespace guide, with pictures


ဆက်စပ် ခေါင်းစဉ်များ

အရင် လေ့လာထားရမယ့် ခေါင်းစဉ်များ:

  • Selection (if/else) (condition ပေါ်မူတည်ပြီး လမ်းကြောင်း ရွေးခြင်း): loop header တွေမှာ သုံးတဲ့ condition တွေ
  • Variables & Data Types (variable နဲ့ ဒေတာ အမျိုးအစားများ): loop variable တွေကို ကြေညာပြီး update လုပ်ခြင်း

ဆက်စပ် ခေါင်းစဉ်များ:

  • 1D Arrays (တန်းတစ်တန်းတည်းမှာ စီထားတဲ့ ဒေတာအစု): array (အရွယ်အစား ပုံသေ၊ အမျိုးအစားတူ ဒေတာစု) ကို traverse (element တိုင်းကို တစ်ခုချင်း ဖြတ်သွားခြင်း) လုပ်ဖို့ loop တွေ မရှိမဖြစ် လိုပါတယ်
  • 2D Arrays (row နဲ့ column ပါတဲ့ ဇယားကွက် array): nested loop တွေက row တွေနဲ့ column တွေကို ဖြတ်သွားပါတယ်

နောက်ထပ် လေ့လာမယ့် ခေါင်းစဉ်များ:

  • 1D Arrays: တန်ဖိုး အများကြီးကို သိမ်းပြီး loop နဲ့ တစ်ခုချင်း လုပ်ဆောင်ပါ
  • Algorithms (အယ်လ်ဂိုရီသမ်၊ ပြဿနာကို အဆင့်ဆင့် ဖြေရှင်းတဲ့ နည်းလမ်း): searching (ရှာဖွေခြင်း) နဲ့ sorting (အစဉ်တကျ စီခြင်း) ကို loop တွေနဲ့ လုပ်ပါတယ်

Loop တွေကို ကျွမ်းကျင်လာရင် 1D array တွေက သဘာဝကျကျ လွယ်လာပါလိမ့်မယ်။ Array operation (array ပေါ်မှာ လုပ်တဲ့ လုပ်ဆောင်ချက်) အားလုံးနီးပါးက element (အစုထဲက အချက်) တွေကို traverse လုပ်ဖို့၊ ရှာဖို့ ဒါမှမဟုတ် ပြင်ဖို့ loop ကို သုံးပါတယ်။