Module 3 – Operators

ක්‍රියාකරුවන්

1. Operators යනු කුමක්ද?

Operators යනු variables සහ අගයන් මත විවිධ මෙහෙයුම් (operations) සිදු කිරීමට භාවිතා කරන විශේෂ සංකේත වේ. උදාහරණයක් ලෙස, + operator එක අංක දෙකක් එකතු කිරීමට භාවිතා කරයි.


2. ප්‍රධාන Operator වර්ග

C++ හි Operator වර්ග කිහිපයක් ඇත. අපි බහුලවම භාවිතා වන වර්ග කිහිපයක් දෙස බලමු.

  • Arithmetic Operators: ගණිතමය ගණනය කිරීම් සඳහා යොදා ගනී. (+, -, *, /, %, ++, --)
  • Assignment Operators: Variables වලට අගයන් පැවරීමට යොදා ගනී. (=, +=, -=, etc.)
  • Comparison Operators: අගයන් දෙකක් සංසන්දනය කර true (1) හෝ false (0) ලෙස ප්‍රතිඵල ලබා දේ. (==, !=, >, <, >=, <=)
  • Logical Operators: කොන්දේසි සහිත ප්‍රකාශන (conditional statements) එකට සම්බන්ධ කිරීමට යොදා ගනී. (&&, ||, !)

ප්‍රායෝගික උදාහරණ 10ක්

1. Arithmetic Operators (ගණිතමය ක්‍රියාකරුවන්)

සරල ගණිත කර්ම කිහිපයක් සිදු කර බලමු.

#include <iostream>

int main() {
    int x = 15;
    int y = 4;
    std::cout << "Sum: " << x + y << std::endl;
    std::cout << "Difference: " << x - y << std::endl;
    std::cout << "Product: " << x * y << std::endl;
    std::cout << "Division: " << x / y << std::endl; // Integer division
    return 0;
}
ප්‍රතිඵලය:
Sum: 19
Difference: 11
Product: 60
Division: 3 (int බෙදීමේදී දශම කොටස නොසලකා හරී)
2. Modulus Operator (%)

% operator එක මගින් එක් අංකයක් අනෙකකින් බෙදූ විට ලැබෙන ඉතිරිය (remainder) ලබා දෙයි.

#include <iostream>

int main() {
    int number = 17;
    int remainder = number % 2;
    std::cout << "Remainder is: " << remainder;
    return 0;
}
ප්‍රතිඵලය:
Remainder is: 1
3. Increment and Decrement Operators (`++`, `--`)

Variable එකක අගය 1කින් වැඩි කිරීමට `++` ද, 1කින් අඩු කිරීමට `--` ද භාවිතා කරයි.

#include <iostream>

int main() {
    int count = 5;
    count++; // Increment
    std::cout << "After increment: " << count << std::endl;
    
    count--; // Decrement
    std::cout << "After decrement: " << count;
    return 0;
}
ප්‍රතිඵලය:
After increment: 6
After decrement: 5
4. Assignment Operators (පැවරුම් ක්‍රියාකරුවන්)

+= වැනි operators මගින් variable එකක අගය යාවත්කාලීන කිරීම කෙටි කර ගත හැක.

#include <iostream>

int main() {
    int score = 100;
    score += 50; // Same as: score = score + 50;
    std::cout << "New score is: " << score;
    return 0;
}
ප්‍රතිඵලය:
New score is: 150
5. Comparison Operator: Equal (==)

අගයන් දෙකක් සමාන දැයි පරීක්ෂා කිරීමට == භාවිතා කරයි.

#include <iostream>

int main() {
    int a = 10;
    int b = 10;
    if (a == b) {
        std::cout << "a and b are equal.";
    }
    return 0;
}
ප්‍රතිඵලය:
a and b are equal.
6. Comparison Operator: Not Equal (!=)
#include <iostream>
#include <string>

int main() {
    std::string color = "red";
    if (color != "blue") {
        std::cout << "The color is not blue.";
    }
    return 0;
}
ප්‍රතිඵලය:
The color is not blue.
7. Comparison Operator: Greater Than or Equal To (>=)
#include <iostream>

int main() {
    int age = 20;
    if (age >= 18) {
        std::cout << "You are an adult.";
    } else {
        std::cout << "You are a minor.";
    }
    return 0;
}
ප්‍රතිඵලය:
You are an adult.
8. Logical Operator: AND (`&&`)

&& operator එක මගින් කොන්දේසි දෙකම සත්‍ය (true) නම් පමණක් සමස්ත ප්‍රකාශනයම true වේ.

#include <iostream>

int main() {
    int age = 25;
    bool hasLicense = true;
    if (age >= 18 && hasLicense) {
        std::cout << "You can drive.";
    }
    return 0;
}
ප්‍රතිඵලය:
You can drive.
9. Logical Operator: OR (`||`)

|| operator එක මගින් කොන්දේසි දෙකෙන් එකක් හෝ සත්‍ය (true) නම් සමස්ත ප්‍රකාශනයම true වේ.

#include <iostream>

int main() {
    bool isWeekend = true;
    bool isHoliday = false;
    if (isWeekend || isHoliday) {
        std::cout << "You can rest today!";
    }
    return 0;
}
ප්‍රතිඵලය:
You can rest today!
10. Logical Operator: NOT (`!`)

! operator එක මගින් boolean අගයක ප්‍රතිඵලය අනෙක් අතට හරවයි (true නම් false, false නම් true).

#include <iostream>

int main() {
    bool isLoggedIn = false;
    if (!isLoggedIn) {
        std::cout << "Please log in.";
    }
    return 0;
}
ප්‍රතිඵලය:
Please log in.