© Khmer Angkor Academy - sophearithput168

រង្វិលជុំ (Loops)

Loops (រង្វិលជុំ) ក្នុង Java

🔁 Loops គឺជាអ្វី?

Loop គឺការធ្វើការងារម្តងហើយម្តងទៀត រហូតដល់បំពេញលក្ខខណ្ឌ។ វាជួយយើងជៀសផុតពីការសរសេរកូដដដែលៗ។

ឧទាហរណ៍ក្នុងជីវិតពិត:

  • ពេលលាងចាន: លាងចានមួយបន្ទាប់មួយ រហូតអស់
  • ការរាប់: រាប់ពី 1 ដល់ 100
  • ដើរជុំសួន: ដើរជុំសួន 5 ជុំ
  • ចាក់តន្ត្រី: ចាក់បទដដែលៗ 10 ដង

📚 ហេតុអ្វីត្រូវការ Loops?

  1. កាត់បន្ថយកូដ: មិនចាំបាច់សរសេរដដែលៗ
  2. ធ្វើការជាមួយទិន្នន័យច្រើន: ដំណើរការ array ឬ list
  3. Automation: ធ្វើការស្វ័យប្រវត្តិ
  4. Performance: កូដខ្លី ងាយ maintain

🔢 ប្រភេទ Loops ក្នុង Java

Loop Type នៅពេលណាប្រើ លក្ខណៈពិសេស
for ដឹងចំនួនជុំច្បាស់ មាន counter ច្បាស់
while មិនដឹងចំនួនជុំ ពិនិត្យមុនរត់
do-while ត្រូវរត់យ៉ាងតិច 1 ដង ពិនិត្យក្រោយរត់
for-each ដំណើរការ collection ងាយស្រួល សាមញ្ញ

🔄 for Loop

Syntax:

for (initialization; condition; increment/decrement) {
    // code to repeat
}

3 ផ្នែកសំខាន់:

  1. Initialization: កំណត់តម្លៃចាប់ផ្តើម (int i = 0)
  2. Condition: លក្ខខណ្ឌបន្ត (i < 10)
  3. Increment/Decrement: បន្ថែម/បន្ថយតម្លៃ (i++)

📖 របៀបដំណើរការ:

for (int i = 0; i < 5; i++) {
    System.out.println(i);
}

// Step by step:
// 1. i = 0 (initialization)
// 2. Check: 0 < 5? Yes → Run code → Print 0
// 3. i++ → i = 1
// 4. Check: 1 < 5? Yes → Run code → Print 1
// 5. i++ → i = 2
// ... continue
// 6. Check: 5 < 5? No → Stop

💡 ឧទាហរណ៍ការប្រើប្រាស់:

// រាប់ពី 1 ដល់ 10
for (int i = 1; i <= 10; i++) {
    System.out.println("Count: " + i);
}

// រាប់ថយក្រោយ
for (int i = 10; i >= 1; i--) {
    System.out.println(i);
}

// រំលង 2 (លេខគូ)
for (int i = 0; i <= 10; i += 2) {
    System.out.println(i);  // 0, 2, 4, 6, 8, 10
}
Java Code
Click "Run" to execute the Java code

⚡ while និង do-while Loops

🔁 while Loop

while loop រត់នៅពេល condition ពិត។ វាពិនិត្យលក្ខខណ្ឌ មុនពេលរត់

Syntax:

while (condition) {
    // code to repeat
}

ឧទាហរណ៍:

int count = 1;

while (count <= 5) {
    System.out.println("Count: " + count);
    count++;  // ⚠️ សំខាន់! បើមិនមាន infinite loop
}

🔂 do-while Loop

do-while loop រត់យ៉ាងតិច 1 ដង ព្រោះពិនិត្យលក្ខខណ្ឌ ក្រោយពេលរត់

Syntax:

do {
    // code to repeat
} while (condition);

ឧទាហរណ៍:

int num = 1;

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

📊 while vs do-while

លក្ខណៈ while do-while
ពិនិត្យលក្ខខណ្ឌ មុនរត់ ក្រោយរត់
ចំនួនរត់អប្បបរមា 0 ដង (អាចមិនរត់) 1 ដង (រត់ច្បាស់)
ប្រើនៅពេលណា មិនប្រាកដថារត់ ត្រូវរត់យ៉ាងតិច 1 ដង

💡 ឧទាហរណ៍ការប្រើ while:

// អាន input រហូតដល់ត្រឹមត្រូវ
String password;
boolean isValid = false;

while (!isValid) {
    password = getInput();
    if (password.equals("12345")) {
        isValid = true;
        System.out.println("Welcome!");
    } else {
        System.out.println("Try again");
    }
}
Java Code
Click "Run" to execute the Java code

🛑 break និង continue Statements

🚫 break Statement

break ប្រើដើម្បីចេញពី loop ភ្លាមៗ មុនពេលបញ្ចប់។

// ស្វែងរកលេខ
for (int i = 1; i <= 10; i++) {
    if (i == 5) {
        System.out.println("រកឃើញ 5!");
        break;  // ឈប់រត់
    }
    System.out.println(i);
}
// Output: 1, 2, 3, 4, រកឃើញ 5!

⏭️ continue Statement

continue ប្រើដើម្បីរំលងជុំបច្ចុប្បន្ន ហើយបន្តទៅជុំបន្ទាប់។

// បោះបង់លេខគូ
for (int i = 1; i <= 5; i++) {
    if (i % 2 == 0) {
        continue;  // រំលងលេខគូ
    }
    System.out.println(i);
}
// Output: 1, 3, 5 (គ្រាន់តែសេស)

🔍 break vs continue

Statement សកម្មភាព ប្រើនៅពេលណា
break ចេញពី loop រកឃើញអ្វីដែលត្រូវការ
continue រំលងជុំនេះ ចង់ skip លក្ខខណ្ឌមួយចំនួន

⚠️ Infinite Loop (រង្វិលជុំគ្មានទីបញ្ចប់)

Loop ដែលមិនបញ្ចប់ អាចធ្វើឱ្យកម្មវិធីគាំង។

// ❌ Infinite loop - កុំធ្វើបែបនេះ!
while (true) {
    System.out.println("រត់រហូត...");
    // គ្មាន break → infinite!
}

// ✅ ត្រឹមត្រូវ - មាន break condition
int count = 0;
while (true) {
    System.out.println(count);
    count++;
    if (count >= 10) {
        break;  // ចេញ
    }
}

🎯 Nested Loops (Loop ក្នុង Loop)

// បោះពុម្ពផ្ទាំង multiplication table
for (int i = 1; i <= 3; i++) {
    for (int j = 1; j <= 3; j++) {
        System.out.print(i + "x" + j + "=" + (i*j) + "  ");
    }
    System.out.println();  // បន្ទាត់ថ្មី
}

// Output:
// 1x1=1  1x2=2  1x3=3
// 2x1=2  2x2=4  2x3=6
// 3x1=3  3x2=6  3x3=9
Java Code
Click "Run" to execute the Java code