C Programming language is one of the oldest programming language. A lot of operands in other programming languages got inspiration from this language. C and C++ have the same operators and most of them are the same in other programming languages.
In programming variable at the left side of the equation symbol is a new variable, or variable that will have new value from the right side of the equation. And the variables at the right side are current variables with their values. For example, you can define a as an integer variable to a result of 2+3,
1 2 3 4 5 |
int a = 2 , b = 3; int c = a + b; |
this is same with
1 2 3 |
int a = 2 + 3; |
For a beginner, or someone (i.e. mathematician) who doesn’t know programming language equations and operators might seem to be weird. For example, let’s look at this simple equation.
1 2 3 4 |
int a = 2, b = 3; b = a + b; |
Here, the equation at the second line, might be weird to a beginner because in mathematics it is something like,
1 2 3 4 5 |
b = a + b b - b = a 0 = a |
As you see, in programming, using mathematical operations in programming is different than mathematical science. In scientific researches, in some cases, in these operations (mostly on floating point numbers) you must verify computational operations done with real mathematics, or you should understand well the mechanics of programming .
In programming , basically we can group these ways to use operators, Arithmetic Operators, Assignment Operators, Comparison Operators and Logical Operators.
Table of Contents
1. Arithmetic Operators
Basic Arithmetic Operator are;
Addition (+) : Adds two values, here are two different examples with integer and floating point numbers
1 2 3 4 |
int a = 5, b = 3; int c = a + b; |
1 2 3 4 |
float a = 5.0, b = 3.0; float c = a + b; |
Subtraction (-) : Subtracts one value from another
1 2 3 4 |
int a = 7, b = 4; int c = a - b; |
Multiplication ( * ) : Multiplies two values x * y
1 2 3 4 |
int a = 7, b = 4; int c = a * b; |
Division ( / ) : Divides one value by another
1 2 3 4 |
int a = 8, b = 4; int c = a / b; |
Modulus ( / ): Returns the division remainder
1 2 3 4 |
int a = 8, b = 4; int c = a % b; |
Increment (++): Increases the value of a variable by 1
1 2 3 4 |
int a = 7; --a; |
Decrement (–): Decreases the value of a variable by 1
1 2 3 4 |
int a = 7; --a; |
2. Assignment Operators
Basic assignment operator are the same as arithmetic’s, as they are used to assign values to variables, by using with = equation symbol. Assignment Operators are listed below with examples;
Equation ( = ) : Assigns lefts side variable to the result of right side,
1 2 3 |
int a = 5 + 3; |
Addition Assignment ( += ) : Adds result of right side to the left side variable,
1 2 3 4 |
int a = 5; a += 3; |
Subtraction Assignment ( -= ) : Subtracts result of right side from the left side variable,
1 2 3 4 |
int a = 5; a -= 3; |
Multiplication Assignment ( *= ) : Multiplies variable left side with the right side ;
1 2 3 4 |
int a = 5; a *= 3; |
Division Assignment ( /= ) : Multiplies variable left side to the result of the right side;
1 2 3 4 |
int a = 8; a /= 2; |
Power Assignment ( ^= ) : Power of variable;
1 2 3 4 |
int a = 3; a ^= 2; |
Modulus Assignment ( %= ) : Results modulus of variables;
1 2 3 4 |
int a = 8; a %= 2; |
Logical AND Assignment ( &= )
1 2 3 4 |
int a = 8; a %= 2; |
Logical OR Assignment ( |= )
1 2 3 4 |
int a = 8; a %= 2; |
Shifting Bits to Left Assignment ( >>= )
1 2 3 4 |
int a = 8; a >>= 2; |
Shifting Bits to Right Assignment ( <<= )
1 2 3 4 |
int a = 8; a <<= 2; |
3. Comparison Operators
All comparison operators are given in this example below,
1 2 3 4 5 6 7 8 9 10 11 12 |
int a = 5, b = 7; if ( a==b ) std::cout << "a is equal to b \n"; if ( a!=b ) std::cout << "a is not Equal to b \n"; if ( a>b ) std::cout << "a is greater than b \n"; if ( a<b ) std::cout << "a is lower than b \n"; if ( a>=b ) std::cout << "a is greater than or equal to b \n"; if ( a<=b ) std::cout << "a is lower than or equal to b \n"; |
4. Logical Operators
In C & C++ programming language,
&& is used as AND,
|| is used as OR,
! symbol is used as NOT.
Logical operators are given below,
1 2 3 4 5 6 7 8 9 10 11 12 |
bool a=true, b =false; // a AND b if ( a && b ) std::cout << "both true \n"; // a OR b if ( a || b ) std::cout << "a or b is true \n"; // NOT (a AND b) if ( !(a && b) ) std::cout << "both not true \n"; |