Thuta Learning
ရှာဖွေရန်
BasicProgrammingbeginner

Variables

စိတ်လျှော့ပါ။ ဒီခန်းကို စာအုပ်လိုမဟုတ်ဘဲ စကားပြောသလိုပဲ၊ နားလည်လွယ်အောင် ရှင်းပါမယ်။

Variable ဆိုတာ program ထဲမှာ value သိမ်းထားတဲ့ box လေးလို တွေးနိုင်ပါတယ်။ C++ က statically typed language ဖြစ်လို့ variable တစ်ခုမသုံးခင် data type ကို ကြေညာပေးရပါတယ်။ ဥပမာ int age ဆိုရင် age ထဲမှာ integer value သာ သိမ်းမယ်လို့ compiler ကို ကြိုပြောထားတာပါ။

cpp
#include <iostream>
#include <string>
using namespace std;

int main() {
    int age = 20;
    double height = 5.8;
    char grade = 'A';
    string name = "Thuta";
    bool isLearning = true;

    cout << name << " is " << age << " years old." << endl;
    cout << "Height: " << height << endl;
    cout << "Grade: " << grade << endl;
    cout << "Learning C++: " << isLearning;
    return 0;
}

ဒီ example မှာ data type မတူတဲ့ variable ၅ မျိုးကို သိမ်းထားပါတယ်။ string သုံးထားလို့ #include <string> ထည့်ထားတာပါ။ cout မှာ << ကို သုံးပြီး စာသားနဲ့ variable value တွေကို ဆက်ထုတ်နိုင်ပါတယ်။

You should see
Thuta is 20 years old. Height: 5.8 Grade: A Learning C++: 1

Info

bool ကို cout နဲ့ထုတ်ရင် default အနေနဲ့ 1 သို့မဟုတ် 0 ထွက်နိုင်ပါတယ်။ true က 1, false က 0 ဖြစ်ပါတယ်။

ဒီနေရာမှာ လူအများမှားတတ်တယ်

  • string ကို include မလုပ်ဘဲ သုံးတာ၊ character ကို double quote နဲ့ရေးတာ၊ number value ကို text quote ထဲထည့်ပြီး တွက်ချင်တာတွေကို သတိထားပါ။