.... အားလံုးအတြက္ မဟုတ္ခဲ့ရင္ေတာင္ တစ္စံုတစ္ရာ တစ္စံုတစ္ေယာက္အတြက္ ေႏြးေထြးတဲ့ ေကာင္းကင္ငယ္ေလး ျဖစ္ပါရေစ ....

Saturday, February 26, 2011

If and If else Statement

ဒါကေတာ့ အေျခခံေတြထဲက တစ္ခုအပါ၀င္ျဖစ္တဲ့ If (choice process) တစ္ခုကို ကၽြန္ေတာ္ နားလည္မိသေလာက္ေလးေျပာျပေပးပါ့မယ္ .. အေျခခံအက်ဆံုးကေန စေနတာျဖစ္လို သိျပိးသား နားလည္ျပိးသား စာဖတ္သူမ်ားအေနနဲေတာ့ ေက်ာ္ဖတ္သြားေစခ်င္ပါတယ္ . ကၽြန္ေတာ္ ရည္ရြယ္ခ်က္ေလးကလညး္ တစ္ေန႔တာ ကၽြန္ေတာ္ ဖတ္မိစုမိသမွ်ကို  ကၽြန္ေတာ္ လို စလွမ္းခါစ လူတစ္ေယာက္အေနနဲ႔ စာဖတ္သူမ်ားကို သိေစခ်င္မိတဲ့ ေစတနာေလးတစ္ခုပါပဲ … စလိုက္ရေအာင္ ..
If ရဲ ပံုစံကေတာ့ ေအာက္ကအတိုင္းျဖစ္ပါတယ္.. 


သူအတြက္ သံုးမ်ိဳးကို တစ္မ်ိဳးစီရွင္းျပပါ့မယ္ ..
ပထမတစ္မ်ိဳးကေတာ့ .. ေအာက္ကအတိုင္းရိုးရွင္းပါတယ္…

…. If Statement …..

if(condition) statement;

(0r)

if (condition)
{
      <Block of statement>;
}    

ကၽြန္ေတာ္တို ယူဆာဆီကေန input တစ္ခုေတာင္းျပီး အဲဒီရလာတဲ့ ေဒတာကို (မွန္းမွား) မိမိလိုခ်င္တဲ့ ပံုစံဟုတ္မဟုတ္ If ကိုသံုးျပီးစစ္ၾကည့္ပါတယ္ .. အဲဒီအခါ စစ္တဲ့အတိုင္းမွန္ရင္ ေအာက္ကိုဆက္သြားျပီး မမွန္ရင္ေတာ့ အဲဒီေနရာမွာတင္ ပရိုဂရမ္ရပ္တန္ျပီး ျဖစ္ေနပါလိမ့္မယ္ .. ေအာက္မွာ ဂဏန္း တစ္လ့ုးကို စံု မ ခြဲျပထားပါတယ္ ..

/Test positive or negative

#include <iostream>
using namespace std;
int main()
{
      int num;
      cout << "Please enter any number (+ or -) : ";
      cin >> num;
      if (num >=0)
            cout << "\nThe number is positive \n\n";
      if( num <0)
            cout <<"\nThe number is negative \n\n";
      return 0;
}
num ကို ယူဆာဆီက ေတာင္းျပီး ရိုက္ခိုင္းလုိက္ပါတယ္ ..
cin >> num မွာ ယူဆာဆီက ရလိုက္ပါျပီ …
if (num > =0) ဒီမွာ တစ္ၾကိမ္စစ္လုိက္ပါတယ္ .. num က ၀ ထက္ၾကိးလား ညီလား ဆိုျပိးေတာ့ ..
ညီမယ္ဆိုရင္ ၾကီးမယ္ဆိုရင္ ေသခ်ာတယ္ ဒီဂဏန္းက အေပါင္းေပါ့ ..ဒါဆို ေအာက္ကိုဆက္ဆင္းျပီး cout ထုတ္ျပပါလိမ့္မယ္ .. ျပီးရင္ေတာ့ return 0; ကေန ပရိုဂရမ္ ျပီးဆ့ုးပါျပီ ..
အဲ ပထမ စစ္ေဆးမႈက မွားေနတယ္ ဆိုရင္ေတာ့ ေနာက္တစ္ေၾကာင္းကိုေက်ာ္ဆင္းပါလိမ့္မယ္ ..နားမလည္ဘူးဆိုရင္ ပံုကိုျပန္ၾကည့္ပါခင္ဗ်ာ ..ပံုထဲကအတုိင္းပါပဲ .. ရွင္းသြားပါလိမ့္မယ္ ..
If (num < 0 ) ကို ထပ္စစ္ပါမယ္ .. အေပၚမွာစစ္သလိုပါပဲ .. ၀ ထက္ ငယ္မယ္ဆိုရင္ အႏႈတ္ေပါ့ ဒါဆို အႏႈတ္ဆိုတဲ့အေၾကာင္းေျပာျပျပီး ပရိုဂရမ္ ျပီးဆ့ုးသြားပါလိမ့္မယ္ .. ဒီေလာက္ဆို If သေဘာေပါက္မယ္ထင္ပါတယ္ ..
ေနာက္တစ္မ်ိဳးကေတာ့ If else ပါ .. သူကေတာ့ ပိုတိုသြားတာေပါ့ .. if (condition) မွာ လိုခ်င္တာ ဟုတ္မဟုတ္ စစ္တယ္ …
မွန္တယ္ဆိုရင္ လိုခ်င္တာဟုတ္ေၾကာင္းေျပာမယ့္ေအာက္ကိုဆက္သြားျပီး .. မဟုတ္ဘူးဆိုရင္ေက်ာ္ခ်သြားျပီးေတာ့ ေအာက္က else ထဲကို ဆက္သြားပါတယ္ ..

… If else Statement ….

if(condition)

statement1;

else
statement2;

(0r)

if (condition)
{
      <Block of statement1>;
}    
else
{
      <Block of statement12>;
}


ေအာက္မွာၾကည့္ရင္နားလည္ပါလိမ့္မယ္ ..
  
//Test positive or negative

#include <iostream>
using namespace std;
int main()
{
      int num;
      cout << "Please enter any number (+ or -) : ";
      cin >> num;
      if (num >=0)
            cout << "\nThe number is positive \n\n";
      else
            cout <<"\nThe number is negative \n\n";
      return 0;
}

ေနာက္ဆံုးတစ္မ်ိဳးကေတာ့ အကယ္လို if else ေတြ အမ်ားၾကီးျဖစ္လာမယ္ဆိုရင္ စစ္ေဆးမႈေတြ အရမ္းမ်ားလာမယ္ဆိုရင္ အသံုးျပဳပါတယ္ .. ပိုျပီးရွင္းလင္းသြားတာေပါ့ ကုတ္ေတြ ဖတ္တဲ့အခါ … သေဘာကေတာ့ အတူတူပါပဲ

if
else if  ကို ေအာက္မွာဖတ္ၾကည့္လုိက္ပါ ..

#include <iostream>
using namespace std;
int main()
{
      int num1,num2;
      cout << "Please enter any number (num 1 and num 2) : ";
      cin >> num1 >> num2;

      if (num1 < num2)
            cout << "\nThe secone num is Larger\n\n";
      else if (num1 > num2)
            cout << "\nThe first num is Larger\n\n";
     
      else if (num1 != num2)
            cout << "\nThey are not the same number\n\n";
      else if(num1 = num2)
            cout <<"\n They are the same \n\n";
      else if(num1 % 2 == 0)
            cout<< "\nThey first number is even \n\n";
      else
            cout << "\nThe first number is odd \n\n";
      return 0;
}

0 မွတ္ခ်က္: