#Concept #Study #CPP C++ has three types of operators which determines on how many operatans an operator operates on. - unary - operates on a single operant - example: unary minus operator, negates it's operant - binary - operates on 2 operants - example: mathematical operators using 2 operants, x - y - ternary - operates on 3 operants - example: conditional operator Common operators can be grouped as: - assignment - used to modify the value of an object by assigning a new one - arithmetic - perform mathematical operations on operants - incement/decrement - to increment an operant - relational - compare value of 2 objects, greater than, less than etc. - logical - test for logical conditions (booleans for example) not, or etc. - member access - acces to a specific member - other - don't fit inside any other category ## Arithmetic operators Basic operators like addition, substraction etc. ```cpp int num1 {200}; int num2 {100}; // adding 2 variables num1 + num2 // substracting 2 variables num1 - num2 // We expect the result to be a float // because num1 and num2 are declared and initilized as integer, the result will be 0 num2 / num1; // Modulas operator, only gets the remainder of a division, // e.g. 10 % 3 would be 1, as it is the remainder of the devision num1 % num2; ``` ## Assignment Operator assigns a value to an operant. Assignment is checked at compilation, not like in other languages like python during runtime. This is pretty unique to C++ ```cpp lhs = rhs // stores the value rhs into the variable lhs // Can be chained, all variables will be set to 0, will be read from right to left when ran num1 = num2 = num3 = 0 ``` ## sizeof operator gets its vaules from iostream and climits, needs to be included in the program. It returns how much storage is necessary for the specified data type. ### Example Get the size of char datatype ```c++ #include <iostream> #include <climits> // both examples below are valid for sizeof sizeof(char) sizeof char ``` ## Increment/Decrement Operator increase or decrease an operant by one or move pointers. Should only be used once in a single statement, otherwise the behaviour will be undefined. ```cpp // Increment num1 by 1 num1++ // Preincrement num1 by 1 ++num1 // Decrement num1 by 1 num1-- // predecrement num1 by 1 --num1 ``` ## Relational operators binary operator (operant operator operant) ```cpp // greater than 2 > 1 // greater than or equal to 2 >= 2 2 >= 1 // les than 1 < 2 // less than or equal to 2 <= 2 1 <= 2 // three way comparison, // evaluates to 0 if they are equal 1 <=> 1 // 0 2 <=> 1 // less than 0 1 <=> 2 // greater than 0 ``` ## Logical operators ```cpp // not operator // 1 does not equal 2 1 != 2 // and operator // 1 does not equal 2 and 3 does not equal 4, should return true / 1 (1 != 2) && (3 != 4) // or operator // if x equals 1 or 2 it will return true / 1 (x = 1) || (x = 2) // if expr1 is true, it will not evaluate the rest of the statement // if expr1 is false, it will continue expr1 && expr2 && expr3 ``` ## 🔗Resources