ක්රියාකරුවන්
Operators යනු variables සහ අගයන් මත විවිධ මෙහෙයුම් (operations) සිදු කිරීමට භාවිතා කරන විශේෂ සංකේත වේ. උදාහරණයක් ලෙස, + operator එක අංක දෙකක් එකතු කිරීමට භාවිතා කරයි.
C++ හි Operator වර්ග කිහිපයක් ඇත. අපි බහුලවම භාවිතා වන වර්ග කිහිපයක් දෙස බලමු.
+, -, *, /, %, ++, --)=, +=, -=, etc.)true (1) හෝ false (0) ලෙස ප්රතිඵල ලබා දේ. (==, !=, >, <, >=, <=)&&, ||, !)සරල ගණිත කර්ම කිහිපයක් සිදු කර බලමු.
#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: 19Difference: 11Product: 60Division: 3 (int බෙදීමේදී දශම කොටස නොසලකා හරී)% operator එක මගින් එක් අංකයක් අනෙකකින් බෙදූ විට ලැබෙන ඉතිරිය (remainder) ලබා දෙයි.
#include <iostream>
int main() {
int number = 17;
int remainder = number % 2;
std::cout << "Remainder is: " << remainder;
return 0;
}Remainder is: 1Variable එකක අගය 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: 6After decrement: 5+= වැනි 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අගයන් දෙකක් සමාන දැයි පරීක්ෂා කිරීමට == භාවිතා කරයි.
#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.#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.#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.&& 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.|| 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!! 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.