Chapter 4C++ Tutorial~1 min read
Operators in C++
Operators — गणित आणि Comparisons
C++ मध्ये C चे सगळे operators आहेत + काही extra (scope resolution ::, new/delete, reference &). Syntax C सारखाच आहे.
Arithmetic Operators
Basic math operations
cpp
#include <iostream>
using namespace std;
int main() {
int a = 10, b = 3;
cout << a + b << endl; // 13 — addition
cout << a - b << endl; // 7 — subtraction
cout << a * b << endl; // 30 — multiplication
cout << a / b << endl; // 3 — integer division
cout << a % b << endl; // 1 — modulus
// float division
cout << (double)a / b << endl; // 3.3333
// Increment / Decrement
int x = 5;
cout << x++ << endl; // 5 (print, then increment)
cout << x << endl; // 6
cout << ++x << endl; // 7 (increment, then print)
// Assignment operators
int c = 10;
c += 5; c -= 2; c *= 3; c /= 4; c %= 3;
cout << "c = " << c << endl; // (10+5-2)*3/4%3 = 2
return 0;
}Relational, Logical, Ternary
Comparisons and logic
cpp
int age = 20;
bool hasCard = true;
int marks = 75;
// Relational — result 1 (true) or 0 (false)
cout << (age >= 18) << endl; // 1
cout << (marks == 100) << endl; // 0
// Logical
cout << (age >= 18 && hasCard) << endl; // 1 — AND
cout << (age < 18 || hasCard) << endl; // 1 — OR
cout << (!hasCard) << endl; // 0 — NOT
// Ternary — condition ? trueVal : falseVal
string result = (marks >= 40) ? "Pass ✅" : "Fail ❌";
cout << result << endl; // Pass ✅
// boolalpha — true/false print करतो
cout << boolalpha << (age >= 18) << endl; // true✅ Key Points — लक्षात ठेवा
- ▸/ integer division (10/3=3), % modulus (10%3=1)
- ▸x++ post-increment, ++x pre-increment
- ▸Relational: ==, !=, <, >, <=, >= — result bool
- ▸&&, ||, ! — logical operators
- ▸Ternary: cond ? val1 : val2 — one-liner if-else
0/11 chapters पूर्ण