While Loop

අද අපි කතා කරන්න යන්නේ While Loop එක ගැන. While Loop එකකින් සිද්ද වෙන්නේ යම් condition එකක් false වෙනකන් අපි define කරපු statement එකක් repeatedly execute   වෙන එක.

while(Boolean expression) {
statement 1;
statement 2;
..
..
Statement n
}

While Loop එකක්  වැඩ කරන්නේ පහලින් දැක්වෙන විදියට.

-මුලින්ම Boolean expression එක check කරනවා.
-එක True නම් 1 ඉදන් n දක්වා වෙන statements execute වෙනවා.
-ආයෙම Boolean expression එක check කරනවා.
-ආයෙම True නම් 1 ඉදන් n දක්වා වෙන statements execute වෙනවා.
-මේ process එක Boolean expression එක false වෙනකම්ම සිද්දවෙනවා. 


දැන් Example එකක් බලමු. 

x = 5
while x > 0:
    print x
    x = x –1

මේකේ Output එක මොකක් වෙයිද?
Try කරලා බලන්න.

අපිට While loop එකට else clause එක උනත් add කරන්න පුළුවන්. පහලින් තියෙන්නේ ඒ වගේ example එකක් .

x = 5
y = x
while y > 0:
    print y
    y = y -1
else:
    print x

මේකේ output එක වෙන්නේ. 

5
4
3
2
1
5