Site icon Learn C++

Learn How To Use Operators In C++

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,

[crayon-676329e6d8388507514468/]

this is same with

[crayon-676329e6d8390949578292/]

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.

[crayon-676329e6d8391913824270/]

Here, the equation at the second line, might be weird to a beginner because in mathematics it is something like,

[crayon-676329e6d8393037227666/]

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.

1. Arithmetic Operators

Basic Arithmetic Operator are;

Addition (+) : Adds two values, here are two different examples with integer and floating point numbers

[crayon-676329e6d8394260855568/]
[crayon-676329e6d8395726095740/]

Subtraction (-) : Subtracts one value from another

[crayon-676329e6d8398120257475/]

Multiplication ( * ) : Multiplies two values x * y

[crayon-676329e6d8399698677884/]

Division ( / ) : Divides one value by another

[crayon-676329e6d839a848519903/]

Modulus ( / ): Returns the division remainder

[crayon-676329e6d839b405748009/]

Increment (++): Increases the value of a variable by 1

[crayon-676329e6d839c280579177/]

Decrement (–): Decreases the value of a variable by 1

[crayon-676329e6d839d941500110/]

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,

[crayon-676329e6d839e559545991/]

Addition Assignment ( += ) : Adds result of right side to the left side variable,

[crayon-676329e6d839f654324838/]

Subtraction Assignment ( -= ) : Subtracts result of right side from the left side variable,

[crayon-676329e6d83a0166468935/]

Multiplication Assignment ( *= ) : Multiplies variable left side with the right side ;

[crayon-676329e6d83a1351296321/]

Division Assignment ( /= ) : Multiplies variable left side to the result of the right side;

[crayon-676329e6d83a2778780147/]

Power Assignment ( ^= ) : Power of variable;

[crayon-676329e6d83a3088415507/]

Modulus Assignment ( %= ) : Results modulus of variables;

[crayon-676329e6d83a4670476621/]

Logical AND Assignment ( &= )

[crayon-676329e6d83a5170701291/]

Logical OR Assignment ( |= )

[crayon-676329e6d83a6189304703/]

Shifting Bits to Left Assignment ( >>= )

[crayon-676329e6d83a7213826730/]

Shifting Bits to Right Assignment ( <<= )

[crayon-676329e6d83a8859694456/]

3. Comparison Operators

All comparison operators are given in this example below,

[crayon-676329e6d83a9445499409/]

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,

[crayon-676329e6d83aa670787075/]

 

Exit mobile version