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-6622218c4eff7152340898/]

this is same with

[crayon-6622218c4efff364405728/]

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-6622218c4f000839520730/]

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

[crayon-6622218c4f002734286748/]

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-6622218c4f004498013369/]
[crayon-6622218c4f006817973874/]

Subtraction (-) : Subtracts one value from another

[crayon-6622218c4f007551996919/]

Multiplication ( * ) : Multiplies two values x * y

[crayon-6622218c4f009783360063/]

Division ( / ) : Divides one value by another

[crayon-6622218c4f00a810111385/]

Modulus ( / ): Returns the division remainder

[crayon-6622218c4f00c851133867/]

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

[crayon-6622218c4f00d054002721/]

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

[crayon-6622218c4f00f087835091/]

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-6622218c4f011962715482/]

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

[crayon-6622218c4f014450520626/]

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

[crayon-6622218c4f016373074916/]

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

[crayon-6622218c4f018963547754/]

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

[crayon-6622218c4f019376968999/]

Power Assignment ( ^= ) : Power of variable;

[crayon-6622218c4f01b070123074/]

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

[crayon-6622218c4f01c120826797/]

Logical AND Assignment ( &= )

[crayon-6622218c4f01e471975609/]

Logical OR Assignment ( |= )

[crayon-6622218c4f01f740971165/]

Shifting Bits to Left Assignment ( >>= )

[crayon-6622218c4f021284494246/]

Shifting Bits to Right Assignment ( <<= )

[crayon-6622218c4f023347886472/]

3. Comparison Operators

All comparison operators are given in this example below,

[crayon-6622218c4f024574179003/]

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-6622218c4f026718471459/]

 

Exit mobile version