C++ (Chapter- 3)

Loop and Decisions အသေးစိတ်ရှင်းပြချက်များ

Relational Operator

Operator Meaning
______________________________________________
> Greater than
< Less than
== Equal to
!= Not equal to
>= Greater than or equal to
<= Less than or equal to

Logical Operator

Operator Effect
—————————————————————-
&& And
|| Or
! Not

Loops and Decisions

Loops

  • for loop
  • while loop
  • do while loop

Decision

  • if
  • if else
  • if else if else

Loops

Program ရဲ့ section အပိုင်းလေးတစ်ပိုင်းကို တစ်ကြိမ်ထပ်ပိုပြီးအလုပ်လုပ်စေချင်တဲ့အခါ Loop ကိုသုံးပါတယ်။ Loop မှာ for loop , while loop, do while loop ရယ်ဆိုပြီး ၃ မျိုးရှိပါတယ်။ Looping တိုင်းမှာ condition တစ်မျိုးပါရပါတယ်။ Condition မှန်တိုင်း looping ပတ်နေမှာဖြစ်ပါတယ်။

  • Function Body မှာ statement တစ်ကြောင်းထဲဆိုရင် Brace မထည့်ပေးလဲရပါတယ်။ တစ်ကြောင်းထက်ပိုတဲ့ Statement အတွက်တော့ Braces အဖွင့်အပိတ်လိုအပ်ပါတယ်။

For loops

For loop က looping တွေထဲမှာ အလွယ်ဆုံးပါ။ for loop မှာ for ရဲ့နောက်က ကွင်းအဖွင့်အပိတ်ထဲမှာ looping ပတ်ရမယ့် အကြိမ်အရေအတွက်ကို ထည့်ပေးရပါမယ်။ ကွင်းထဲမှာရှိတဲ့ Condition မှန်နေသမျှ တွန့်ကွင်းထဲမှာရှိတဲ့ Statement တွေအလုပ်လုပ်ပြီး looping ပတ်နေမှာ ဖြစ်ပါတယ်။
Example

int main( )
{
for (int j=0; j<15; j++)
{
cout<<"j* j"<<" ";
}
cout<<endl;
return 0;
}

Example program အရ main function ထဲမှာ for loop တစ်ခုရှိပါတယ်။ ကွင်းအဖွင့်အပိတ်ထဲမှာ Condition ရှိပါတယ်။ Condition မှန်မှ တွန့်ကွင်းထဲက cout အလုပ်လုပ်မှာပါ။ Condition က integer variable name ( j ) ကို integer value 0 assign လုပ်လိုက်ပါတယ်။ အဲ့တော့ j သည် 0 ဖြစ်သွားပါတယ်။ j < 15 က j သည် 15 အောက်ငယ်နေသ၍ Condition မှန်နေပါမယ်။ j သည် 15 နှင့် 15 ထက်ကြီးလျှင် Condition မှားသွားတဲ့ အတွက် တွန့်ကွင်းထဲက Statement တွေအလုပ်မလုပ်တော့ပဲ တွန့်ကွင်းအပြင်က Statement cout<<endl; ကိုကျော်သွားမှာ ဖြစ်ပါတယ်။


အဲ့တော့ j သည် 0 ; 0 က 15 အောက်ငယ်တယ်; j++ က တွန့်ကွင်းထဲက အလုပ်တွေအကုန်ပြီးမှ အလုပ်လုပ်ပါတယ်။ ကဲ…….. တွန့်ကွင်းထဲသွားမယ်။ cout<<“j* j ” <<” “; Statement ကိုတွေ့ပါတယ်။ cout က output ထုတ်ပြပါမယ်။ “ဘာကိုလဲ ?! ” j * j ပါ Star က အမြှောက်သဘောပါ။ j နဲ့ j ကိုမြှောက်ပါမယ်။ j က 0 ဆိုတော့ 0 * 0 အဖြေက 0 ပါပဲ။ : ) အနောက်က double code ထဲမှာ ဘာမှမရှိတဲ့အတွက် blank ပြမယ်။ ကဲ ……. Output က ” 0 space” ပါ။ တွန့်ကွင်းထဲက အလုပ်တွေပီးရင် Condition ထဲက j++ ကိုသွားမယ်။ j++ ဆိုတာ j ကို 1 တိုးတာပါ။ အစက j က 0, အခု j က j +1 ဆိုတော့ j က 1 ပါ။ Condition မှန်ပါတယ်။ j က 15 အောက်ငယ်ပါတယ်။ cout<<“j* j ” <<” “; j က 1 ; 1 * 1 က 1 ပါ။ Loop နှစ်ကြိမ်ပတ်လိုက်တဲ့အခါမှာတော့ Output က ” 0 1 ” ပါ။ Condition မှန်နေသမျှ looping ပတ်နေ ပြီး တွန့်ကွင်းထဲက Statement တွေအလုပ်လုပ်နေမှာပါ။ အထက်ပါ program အရ Looping ပတ်နေသမျှ j နဲ့ j မြှောက်ပြီး Space ခြားနေမှာ ဖြစ်ပါတယ်။ j = 15 မှာ looping ရပ်ပြီး cout<<endl; ကိုရောက်မှာဖြစ်ပါတယ်။

Output

0 1  4  9  16  25  36  49  64  81  100  121  144  169  196
Design a site like this with WordPress.com
Get started