Thuta Learning
C
BasicProgrammingအခြေခံ

While Loop

Cသင်ခန်းစာ 1320 မိနစ်

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • Initialisation, condition နဲ့ progress step ရှင်းရှင်းလင်းလင်း ပါတဲ့ while loop တစ်ခု ရေးနိုင်ဖို့
  • While loop တစ်ခု ဘယ်နှစ်ကြိမ် run မလဲ (သုည ကြိမ် အပါအဝင်) ကြိုခန့်မှန်းနိုင်ဖို့
  • Body ကို အနည်းဆုံး တစ်ကြိမ် run ရမယ့်အခါ do...while ကို ရွေးနိုင်ဖို့
  • Update မေ့တာ၊ semicolon အပိုပါတာကြောင့် ဖြစ်တဲ့ infinite loop တွေကို ရှာပြီး ပြင်နိုင်ဖို့

ရိုးရိုးလေး ရှင်းပြမယ်

ရိုးရိုးလေး ရှင်းရရင်

While loop ကို ဂိတ်ပေါက်မှာ ရပ်နေတဲ့ အစောင့်တစ်ယောက်လို မြင်ကြည့်ပါ။ သူက ရောက်လာတိုင်း မေးခွန်း တစ်ခုတည်းကိုပဲ မေးပါတယ်။ အဖြေ true ဖြစ်ရင် body ထဲက code ကို run ခွင့်ပေးပြီး ပြီးတာနဲ့ ဂိတ်ဆီ ပြန်လာပြီး ထပ်မေးပါတယ်။ အဖြေ false ဖြစ်သွားတဲ့အချိန်မှာတော့ program က loop ကို ကျော်ပြီး နောက် line ကို ဆက်သွားပါတယ်။

ဘယ်နှစ်ကြိမ် run ရမလဲဆိုတာ ကြိုမပြောနိုင်တဲ့အခါ while ကို သုံးပါတယ် — number တစ်ခုကို 1 ရောက်တဲ့အထိ တစ်ဝက်စီ ခွဲတာ၊ file ဆုံးတဲ့အထိ ဖတ်တာ၊ connection ရတဲ့အထိ ထပ်ကြိုးစားတာ စတာတွေပါ။ ဘယ်တော့ ရပ်မလဲဆိုတာကို သတ်မှတ်ထားတဲ့ အရေအတွက်က မဟုတ်ဘဲ data ကိုယ်တိုင်က ဆုံးဖြတ်ပါတယ်။

Problem → Why → Solution

ပြဿနာ: ဘယ်တော့မှ မပြီးတဲ့ program

While loop မှာ အဖြစ်အများဆုံး bug က program က ဘယ်တော့မှ မပြီးတော့တာပါ။ Crash လည်း မဖြစ်၊ error message လည်း မပေါ်ပါဘူး — terminal မှာ line တစ်ကြောင်းတည်း ဆက်တိုက် ထွက်နေတာ ဒါမှမဟုတ် Ctrl+C မနှိပ်မချင်း ဘာမှမပြဘဲ ငြိမ်နေတာပါ။ အောက်က ပထမ loop မှာ counter ကို print ထုတ်ပေမယ့် ဘယ်တော့မှ မပြောင်းလို့ i < 5 က အမြဲ true ဖြစ်နေပါတယ်။ ဒုတိယ loop မှာတော့ condition နောက်မှာ semicolon အပို ပါနေလို့ loop ရဲ့ body က ဗလာ statement ဖြစ်သွားပါတယ်၊ အောက်က brace တွေက loop အဆုံးမှာမှ run မှာဖြစ်ပြီး loop ကလည်း ဘယ်တော့မှ မဆုံးပါဘူး။

stuck.c
#include <stdio.h>

int main(void) {
    int i = 0;
    while (i < 5) {
        printf("%d\n", i);      /* prints 0 forever: i never changes */
    }

    int j = 0;
    while (j < 5);              /* this ; IS the loop body */
    {
        j++;                    /* never reached */
    }
    return 0;
}

ဘာကြောင့် မရပ်တော့တာလဲ

While loop က သူ့ condition false ဖြစ်မှသာ ရပ်ပြီး condition ကို C က ကိုယ့်ဘာသာ ပြောင်းမပေးပါဘူး။ Body ထဲက ဘာကမှ loop variable ကို ထွက်ပေါက်ဘက် မရွှေ့ရင် test တစ်ခုတည်းက အဖြေ တစ်ခုတည်းကို အမြဲ ပေးနေပါလိမ့်မယ်။ Loop နှစ်ခုလုံးက valid C ဖြစ်လို့ compiler က လက်ခံပါတယ်။ ဒီလို layout မျိုးမှာ GCC ကို -Wall နဲ့ compile လုပ်ရင် semicolon အပိုကို -Wmisleading-indentation warning ("this 'while' clause does not guard...") နဲ့ ပြပေးနိုင်လို့ warning တွေ ဖွင့်ပြီး compile လုပ်သင့်တဲ့ အကြောင်းရင်း နောက်တစ်ခု ဖြစ်ပါတယ်။

Core concept: initialise, test, progress

မှန်မှန်ကန်ကန် ဆုံးတဲ့ while loop တိုင်းမှာ အပိုင်းသုံးပိုင်း ပါပါတယ်၊ ကွင်းထဲမှာ ပါတာက တစ်ပိုင်းပဲ ရှိပေမယ့်ပါ။ Initialisation က loop မစခင် ဖြစ်ပါတယ် — int i = 0; က condition အတွက် စစ်စရာ value တစ်ခု ပေးပါတယ်။ Condition ကို ပထမဆုံးအကြိမ် အပါအဝင် pass တိုင်းရဲ့ အရှေ့မှာ စစ်ပါတယ်။ Progress က body ထဲမှာ ဖြစ်ပါတယ် — i++, n /= 10 ဒါမှမဟုတ် နောက် item ကို ဖတ်တာက condition မှီခိုနေတဲ့ value ကို ပြောင်းပေးပါတယ်။ သုံးခုထဲက တစ်ခု မေ့သွားရင် loop က လုံးဝ မစတာ၊ ဘယ်တော့မှ မရပ်တာ ဒါမှမဟုတ် အမှိုက် value တွေနဲ့ အလုပ်လုပ်တာ ဖြစ်ပါတယ်။

Test က အရင် လာတာကြောင့် while loop က သုည ကြိမ် run နိုင်ပါတယ်။ i က 10 နဲ့ စရင် while (i < 5) က body ကို လုံးဝ ကျော်သွားပြီး ပိတ် brace နောက်က statement ဆီ တန်းသွားပါတယ်။ အများအားဖြင့် ဒါက လိုချင်တဲ့ အပြုအမူပါ — ဗလာ file ကို process လုပ်စရာ မလိုသလို 1 ဖြစ်ပြီးသား number အတွက်လည်း ဘာ step မှ မလိုပါဘူး။

C မှာ condition က scalar expression ဘယ်ဟာမဆို ဖြစ်နိုင်ပြီး သုည တစ်ခုတည်းကိုပဲ false လို့ သတ်မှတ်ပါတယ်။ while (n) က while (n != 0) ရဲ့ အတိုကောက်ဖြစ်ပြီး while (1) ကတော့ ကိုယ်ပိုင် ထွက်ပေါက် မပါတဲ့ loop ပါ — အဲဒါကို break (Break/Continue သင်ခန်းစာမှာ လေ့လာမယ်) ဒါမှမဟုတ် return နဲ့ ထွက်ရပါတယ်။ Pointer condition လည်း အတူတူပါပဲ၊ while (p) က p null pointer မဖြစ်မချင်း run ပြီး linked list တွေမှာ ဒီပုံစံကို တွေ့ရပါမယ်။

Pass အရေအတွက်က data ပေါ် မူတည်တဲ့အခါ for ထက် while ကို ရွေးပါ။ Example ထဲက digit counter က number မှာ digit ၁ လုံးလား ၉ လုံးလား ကြိုမသိပါဘူး၊ ဘာမှ မကျန်တော့တဲ့အထိ 10 နဲ့ စားနေရုံပါပဲ။ "row 5 ကြောင်း print ထုတ်" လို အရေအတွက် သိပြီးသား ဆိုရင်တော့ နောက်သင်ခန်းစာက for loop က အပိုင်းသုံးပိုင်းကို line တစ်ကြောင်းတည်းမှာ စုထားလို့ ဖတ်ရ ပိုလွယ်ပါတယ်။

text
One pass of while (i < 5)
-------------------------
        +------------------+
        | int i = 0;       |   initialise (runs once)
        +--------+---------+
                 |
                 v
        +------------------+   false
   +--->|     i < 5 ?      |---------> first statement after the loop
   |    +--------+---------+
   |             | true
   |             v
   |    +------------------+
   |    | printf(...)      |   body
   |    | i++;             |   progress
   |    +--------+---------+
   |             |
   +-------------+
မှတ်ထားရမယ့် ပုံစံ: မေး၊ လုပ်၊ ရွှေ့၊ ပြန်မေး
  1. Loop variable ကို တစ်ကြိမ် set လုပ်
  2. Pass တိုင်းအရှေ့မှာ condition စစ်
  3. Body run ပြီး progress လုပ်
  4. False ဆိုရင် loop ကို ကျော်ထွက်

Body ထဲက ဘာကမှ condition ရဲ့ အဖြေကို မပြောင်းနိုင်ရင် loop က ဆုံးဖို့ မဖြစ်နိုင်ပါဘူး။

အထွေထွေ ပုံစံ

c
initialise;
while (condition) {
    statements;
    update;             /* move toward condition == false */
}

do {
    statements;
} while (condition);    /* body always runs at least once */

while က body မ run ခင် condition ကို စစ်ပြီး do...while ကတော့ body ကို အရင် run ပြီးမှ စစ်ပါတယ်။ do...while ရဲ့ ပိတ်ကွင်းနောက်မှာ semicolon လိုအပ်ပြီး while ရဲ့ ကွင်းနောက်မှာတော့ semicolon မထည့်ရပါဘူး။

Example: counter နဲ့ digit counter

ပထမ loop က 0 ကနေ 4 အထိ ရေတွက်တဲ့ ရိုးရိုး loop ဖြစ်ပြီး number တစ်ခုကို line တစ်ကြောင်းစီ print ထုတ်ပါတယ်။ ဒုတိယ loop က while ရဲ့ တကယ့် အားသာချက်ကို ပြပါတယ် — pass ဘယ်နှစ်ကြိမ်လဲဆိုတာကို 40210 ဆိုတဲ့ value ကိုယ်တိုင်က ဆုံးဖြတ်ပြီး digit တစ်လုံးကို pass တစ်ကြိမ် ဖြစ်ပါတယ်။

while_loop.c
#include <stdio.h>

int main(void) {
    int i = 0;                  /* 1. start value */

    while (i < 5) {             /* 2. tested before every pass */
        printf("%d\n", i);
        i++;                    /* 3. progress toward i < 5 being false */
    }

    int n = 40210;
    int digits = 0;

    while (n > 0) {             /* how many passes? the data decides */
        n /= 10;                /* drop the last digit */
        digits++;
    }
    printf("40210 has %d digits\n", digits);
    return 0;
}

Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်

  1. Initialiseint i = 0; /* 1. start value */

    Loop မစခင် i ကို 0 ပေးထားပါတယ်။ ဒီ line မပါရင် i ထဲမှာ ဘာ value ရှိမလဲ မသိနိုင်တော့ (indeterminate) loop ဘယ်နှစ်ကြိမ် run မလဲ ခန့်မှန်းလို့ မရပါဘူး။

  2. Testwhile (i < 5) { /* 2. tested before every pass */

    Condition ကို pass တိုင်းရဲ့ အရှေ့မှာ စစ်ပါတယ်။ ငါးကြိမ်မြောက် pass ပြီးတဲ့အခါ i က 5 ဖြစ်ပြီး 5 < 5 false ဖြစ်လို့ loop ရပ်ပါတယ် — ဒါကြောင့် 5 ကို print မထုတ်ပါဘူး။

  3. Progressi++; /* 3. progress toward i < 5 being false */

    ဒီ line က i ကို 5 ဆီ တစ်လှမ်းစီ ရွှေ့ပေးတဲ့ တစ်ခုတည်းသော နေရာပါ။ ဖျက်လိုက်ရင် program က 0 ကို အဆုံးမရှိ print ထုတ်နေပါလိမ့်မယ်။

  4. Data-driven conditionwhile (n > 0) { /* how many passes? the data decides */

    ဒီ loop က ငါးကြိမ် run ရမယ်လို့ code ထဲမှာ ဘယ်နေရာမှ မရေးထားပါဘူး။ n မှာ digit ကျန်သေးသရွေ့ ဆက် run ပြီး 0 ရောက်တာနဲ့ ရပ်ပါတယ်။

  5. Integer division as progressn /= 10; /* drop the last digit */

    int ချင်း စားရင် ဒသမ ပိုင်းကို ဖြတ်ပစ်လို့ n က 40210 → 4021 → 402 → 40 → 4 → 0 ဖြစ်သွားပါတယ်။ အဆုံးက 0 digit ကိုလည်း ရေတွက်ပါတယ်၊ digit ရဲ့ value ကို မကြည့်ဘဲ စားတာပဲ လုပ်လို့ပါ။

You should see
0
1
2
3
4
40210 has 5 digits

do...while: အရင် run ပြီးမှ စစ်

Example ထဲက digit counter မှာ ဖုံးကွယ်နေတဲ့ edge case တစ်ခု ရှိပါတယ်။ n = 0 နဲ့ run ရင် 0 > 0 က ချက်ချင်း false ဖြစ်လို့ "0 has 0 digits" လို့ ပြောပါလိမ့်မယ်၊ ဒါပေမယ့် 0 ကို ရေးရင် digit တစ်လုံး ရှိပါတယ်။ do...while က body ကို test မလုပ်ခင် အရင် run လို့ ဒီလို "အနည်းဆုံး တစ်ကြိမ်" ကိစ္စတွေအတွက် တိုက်ရိုက် ကိုက်ညီပါတယ်။

do_while.c
#include <stdio.h>

int main(void) {
    int n = 0;
    int digits = 0;

    do {
        n /= 10;
        digits++;
    } while (n > 0);            /* tested AFTER the body */

    printf("0 has %d digit\n", digits);
    return 0;
}

do...while ကို menu ပြတာ (ပထမဆုံး တစ်ကြိမ် အမြဲ ပြရမယ်) နဲ့ "တစ်ကြိမ် ကြိုးစားပြီး မအောင်မြင်ရင် ထပ်ကြိုးစား" ပုံစံတွေမှာ တွေ့ရပါတယ်။ Body ကို run ဖို့ မသင့်တော်တဲ့ အခြေအနေ ရှိနိုင်ရင် (ဥပမာ ဗလာ list) plain while ကိုပဲ သုံးပါ။

Input ကုန်တဲ့အထိ ဖတ်တဲ့ loop တွေ

Keyboard ဒါမှမဟုတ် file ကနေ ဖတ်တဲ့ loop တွေမှာ ထွက်ပေါက် နှစ်ခု လိုပါတယ်။ တစ်ခုက sentinel ပါ — 0 ဒါမှမဟုတ် "quit" လို "ပြီးပြီ" လို့ သဘောတူထားတဲ့ value ပါ။ နောက်တစ်ခုက end-of-file (EOF) ပါ — input stream ကိုယ်တိုင် ကုန်သွားတာပါ။ scanf() က အောင်မြင်စွာ ဖတ်ခဲ့တဲ့ item အရေအတွက်ကို return ပြန်ပြီး input ကုန်သွားရင် EOF ကို ပြန်ပါတယ်။ ဒါကြောင့် return value ကို loop condition ထဲမှာ တိုက်ရိုက် စစ်တာက ကျင့်သုံးရမယ့် ပုံစံပါ။ User input အကြောင်းကို User Input သင်ခန်းစာမှာ အသေးစိတ် လေ့လာရပါမယ်။

sum_until_zero.c
#include <stdio.h>

int main(void) {
    int value;
    int total = 0;

    /* stops on the sentinel 0, on EOF, and on input that is not a number */
    while (scanf("%d", &value) == 1 && value != 0) {
        total += value;
    }
    printf("total: %d\n", total);
    return 0;
}

လက်တွေ့ project တွေမှာ

Real project တွေမှာ ဘယ်လို သုံးလဲ

Data ပေါ် မူတည်ပြီး အရှည် ပြောင်းတဲ့ loop တွေကို C program တိုင်းလိုလိုမှာ တွေ့ရပါတယ်။ fgetc() က EOF မပြန်မချင်း file ကို ဖတ်တာ၊ linked list ကို while (node != NULL) နဲ့ လိုက်ကြည့်တာ၊ server တစ်ခုက shutdown signal က flag ကို မပြောင်းမချင်း while (running) နဲ့ request အသစ်ကို စောင့်နေတာ၊ game တစ်ခုက while (!quit) နဲ့ screen ကို တစ်စက္ကန့် အကြိမ် ၆၀ ပြန်ဆွဲတာ စတာတွေပါ။

Production code မှာ အပြင်ကမ္ဘာ (network, hardware, user) ကို စောင့်နေတဲ့ loop တိုင်းကို ဒုတိယ ထွက်ပေါက်တစ်ခု ထပ်ပေးလေ့ ရှိပါတယ် — attempts < max_attempts လို အကြိမ်အရေအတွက် ကန့်သတ်ချက်၊ timeout ဒါမှမဟုတ် error check တစ်ခုပါ။ ဒါမှ cable ပြတ်သွားတာမျိုးက program ကို ထာဝရ ပိတ်မိမနေစေပါဘူး။ Microcontroller firmware တွေမှာတော့ main() ထဲက while (1) က ရည်ရွယ်ချက်ရှိရှိ ရေးထားတာပါ — device က ဓာတ်အား မပြတ်မချင်း run နေရမှာမို့ အဆုံးမရှိ loop က bug မဟုတ်ဘဲ design ဖြစ်တဲ့ နေရာ တစ်ခုပါ။

အမှားများ

အဖြစ်များတဲ့ အမှားတွေ

1. Update ကို မေ့တာ ဒါမှမဟုတ် if ထဲမှာ ဖွက်ထားမိတာ

i++ လုံးဝ မပါတာ၊ ဒါမှမဟုတ် if (i % 2 == 0) { i++; } လို branch တစ်ခု ထဲမှာပဲ ရှိနေတာက odd value တစ်ခု ရောက်တာနဲ့ loop ကို ပိတ်မိစေပါတယ်။ Loop တစ်ခု ရေးပြီးတိုင်း "condition ထဲက variable ကို ဘယ် line က ပြောင်းလဲ၊ pass တိုင်းမှာ အဲဒီ line ရောက်လား" လို့ ကိုယ့်ကိုယ်ကို မေးပါ။

2. while (...) နောက်က semicolon

while (i < 5); က loop statement ကို အဲဒီမှာတင် အဆုံးသတ်ပြီး ဗလာ body တစ်ခုကို ထပ်ခါထပ်ခါ run ပါတယ်။ အောက်က brace block က loop နဲ့ မဆိုင်တော့ပါဘူး။ Condition ရဲ့ ကွင်းပိတ်နဲ့ { ကို line တစ်ကြောင်းတည်းမှာ ကပ်ရေးတဲ့ အလေ့အကျင့်က ဒီအမှားကို မျက်စိနဲ့ မြင်ရ လွယ်စေပါတယ်။

3. မှားနေတဲ့ input ကို မဖယ်ဘဲ ထပ်ဖတ်တာ

while (scanf("%d", &age) != 1) { printf("Try again\n"); } က user က စာလုံးတွေ ရိုက်လိုက်ရင် အဆုံးမရှိ လည်နေပါတယ်။ scanf က လက်မခံတဲ့ စာလုံးတွေကို input buffer ထဲမှာ ချန်ထားလို့ နောက်တစ်ကြိမ် ခေါ်တဲ့အခါ အဲဒီ စာလုံးတွေမှာပဲ ထပ်ကျရှုံးပါတယ်။ Input ကုန်ရင်လည်း scanf က EOF ကို ပြန်ပြီး EOF က 1 မဟုတ်လို့ ဆက်လည်နေပါတယ်။ မှားတဲ့ line ကျန်တာကို ဖယ်ပစ်ပြီး EOF ရောက်ရင် ရပ်ပါ။

4. < နေရာမှာ <= သုံးမိတာ

while (i <= 5) က i = 0 ကနေ စရင် ငါးကြိမ် မဟုတ်ဘဲ ခြောက်ကြိမ် run ပါတယ်။ Loop ပြီးသွားတဲ့အခါ i ရဲ့ နောက်ဆုံး value ကို စာရွက်ပေါ်မှာ trace လုပ်ကြည့်ပါ၊ ရေတွက်တဲ့ အမှားတွေ အများစုကို ဒီနည်းနဲ့ ဖမ်းမိပါတယ်။

Best Practices

အကောင်းဆုံး အလေ့အကျင့်များ

  • Body ကို မရေးခင် initialise, condition, progress သုံးခုလုံးကို အရင် ရေးထားပါ။
  • Input, network, hardware ကို စောင့်တဲ့ loop တိုင်းကို ကန့်သတ်ချက် (အကြိမ်ရေ ဒါမှမဟုတ် timeout) တစ်ခု ပေးပါ။
  • gcc -Wall -Wextra နဲ့ compile လုပ်ပါ၊ semicolon အပိုလို layout အမှားတွေကို warning အဖြစ် ပြပေးနိုင်ပါတယ်။
  • Pass အရေအတွက်ကို ကြိုသိရင် for loop ကို ရွေးပါ၊ while ကို data က ရပ်ချိန် ဆုံးဖြတ်တဲ့ အခါအတွက် ချန်ထားပါ။

Mini challenge

Mini challenge: 3n + 1 လမ်းကြောင်း

n = 7 ကနေ စပြီး n က 1 မရောက်မချင်း 3n + 1 rule ကို apply လုပ်ပါ — even number ကို တစ်ဝက် ခွဲပြီး odd number ကို 3n + 1 ပြောင်းပါ။ Value တိုင်းကို line တစ်ကြောင်းတည်းမှာ print ထုတ်ပြီး step အရေအတွက်ကို ရေတွက်ပါ။ Starting number တစ်ခုအတွက် step ဘယ်နှစ်ခု လိုမလဲ ဘယ်သူမှ ကြိုမသိနိုင်လို့ while အတွက် အကိုက်ညီဆုံး အလုပ်ပါ။

  • Condition က n != 1 ဖြစ်တဲ့ while loop တစ်ခု သုံးရမယ်
  • Body ထဲမှာ if/else နဲ့ even rule ဒါမှမဟုတ် odd rule ကို apply လုပ်ရမယ်
  • 7 ကနေ စပြီး value တိုင်းကို space ခြားပြီး line တစ်ကြောင်းတည်းမှာ print ထုတ်ရမယ်
  • Step တစ်ခုစီကို ရေတွက်ပြီး loop ပြီးမှ စုစုပေါင်းကို print ထုတ်ရမယ်
collatz.c
#include <stdio.h>

int main(void) {
    int n = 7;
    // TODO: declare a step counter

    printf("%d", n);
    // TODO: loop while n is not 1
    //   even n -> n / 2, odd n -> 3 * n + 1
    //   print " %d" for the new value and count the step

    // TODO: print "Reached 1 after <steps> steps"
    return 0;
}

ရလဒ်: Program က ဒီ line နှစ်ကြောင်းကို print ထုတ်ရပါမယ်: 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1 Reached 1 after 16 steps

အရိပ်အမြွက် ကြည့်ရန်

n % 2 == 0 က even ဖြစ်မဖြစ် စစ်ပါတယ်။ Starting value ကို loop မစခင် print ထုတ်ပြီး value အသစ်တွေကို loop ထဲမှာ print ထုတ်ပါ — ဒါဆို loop က သူ အခုလေးတင် တွက်ထားတာကိုပဲ print ထုတ်ရုံပါပဲ။

အဖြေ ကြည့်ရန်
collatz.c
#include <stdio.h>

int main(void) {
    int n = 7;
    int steps = 0;

    printf("%d", n);
    while (n != 1) {
        if (n % 2 == 0) {
            n = n / 2;
        } else {
            n = 3 * n + 1;
        }
        printf(" %d", n);
        steps++;
    }
    printf("\nReached 1 after %d steps\n", steps);
    return 0;
}

Quick Quiz 1 / 3

ဒီ code က ဘာ print ထုတ်မလဲ။ int n = 10; while (n > 0) { n -= 3; } printf("%d", n);

Quick Quiz 2 / 3

int i = 0; while (i < 3); { printf("%d", i); i++; } — ဘာဖြစ်မလဲ။

Quick Quiz 3 / 3

int k = 10; while (k < 5) { k++; } — body ဘယ်နှစ်ကြိမ် run မလဲ။

မှတ်ထားရမယ့် အချက်များ

  • While loop က pass တိုင်းရဲ့ အရှေ့မှာ condition ကို စစ်လို့ သုည ကြိမ် run နိုင်ပါတယ်။
  • Loop မှန်ကန်ဖို့ initialise, test, progress သုံးခုလုံး လိုပါတယ် — progress မပါရင် infinite loop ဖြစ်ပါတယ်။
  • do...while က body ကို အရင် run ပြီးမှ စစ်လို့ အနည်းဆုံး တစ်ကြိမ် run ရမယ့် အလုပ်တွေအတွက် သင့်တော်ပါတယ်။
  • Input ကို စောင့်တဲ့ loop တွေမှာ sentinel, EOF နဲ့ မှားတဲ့ input သုံးမျိုးလုံးကို ထွက်ပေါက် ပေးပါ။

GNU C compiler diagnostics — GNU Project

နောက်ဆုံးပြင်ဆင်ရက်

Progress ကို ဤ browser (ဤစက်) တွင်သာ သိမ်းထားပါသည်။ အခြားစက်များနှင့် sync မလုပ်ပါ။

ဤသင်ခန်းစာတွင် အမှားတွေ့ပါက အကြောင်းကြားရန်သင့် email app ကို ဤသင်ခန်းစာလိပ်စာ ဖြည့်ပြီးသားဖြင့် ဖွင့်ပေးပါမည် — သင်ကိုယ်တိုင် ပို့မှသာ ပို့မည်ဖြစ်သည်။

While Loop | Thuta Learning