operator in c plus plus

Posted under C++ Course On By mohammed.mutawe

operator in c

The operands are one of the basics of the C++ language, and they are in short the use of the addition, subtraction, division and multiplication signs (+, -, /,*) or greater than and less than (<, >), and other operators that will be mentioned in this lesson, without which programming languages ​​will not perform her job.

 

Uses

As mentioned earlier, operators can be used in C++ to perform arithmetic and logic operations in processing, which often leads to tangible results that have an impact on the program’s run page.

Types

We will give an example of each of the operands in the Main function, and to know the types and forms of operators in C language, they are in the form of:

  • Parentheses ( ): which contain values ​​in them.
#include<iostream>
using namespace std;

int main() {

    bool c = true;

    if (c)
    {
        cout << "True" << endl;
    }
    return 0;
}

 

  • Brackets []: These are commonly used in arrays and element designations.
#include<iostream>
using namespace std;

int main() {

int arr[10];

    return 0;
}

 

  • Pointer <-: It is frequently used in pointers.
struct foo
{
    int x;
    float y;
};




int main() {


    struct foo var;
    struct foo* pvar;

    var.x = 5;
    (&var)->y = 14.3;
    pvar->y = 22.4;
    (*pvar).x = 6;


    return 0;
}

 

  • ++ or — : in C++ means to increase or decrease.
int main() {
    fraction tot;
    tot.output();


    int a = 5;
    a++;
    cout << a << endl;
    a--;
    cout << a << endl;


    return 0;
}

 

  • ++ or –: which is the self-increment of the value of the variable.
#include<iostream>
using namespace std;

int main() {
    fraction tot;
    tot.output();


    int a = 5;
    --a;
    cout <<"This method is prefix : "<< a << endl;

    return 0;
}

 

  • * Asterisk: Since it is used in multiplication, it is also used in indicators and has other advanced uses.
#include<iostream>
using namespace std;


int main() {

    int a = 5;

    std::cout << "This is value of a : " << a << endl;
    std::cout << "This is address of a : " << &a << endl;

    int *pointer = &a;

    ++*pointer;  // use of * Asterisk

    std::cout <<"This value of a after affected by pointer : "<< a<< endl;


    return 0;
}

 

  • The & address tag is frequently used in the addresses of variables in C++.
#include<iostream>
using namespace std;


int main() {

    int a = 5;

    std::cout << "This is value of a : " << a << endl;
    std::cout << "This is address of a : " << &a << endl;

    int *pointer = &a;  //use of Address

    ++*pointer;

    std::cout <<"This value of a after affected by pointer : "<< a<< endl;


    return 0;
}

 

  • * , / , % symbols: division / , multiplication *, and percentage %.
#include<iostream>
using namespace std;


int main() {

    int x = 15;
    int y = 7;
    int multi = x * y;
    int devide = x / y;
    int mod = y % x;

    std::cout << "x multi y =  " << multi << endl;
    std::cout << "x devide y =  " << devide << endl;
    std::cout << "y modulas x =  " << mod << endl;



    return 0;
}

 

 

  • << , >> Bitwise symbols: They are frequently used in print sentences and input sentences, and they have other uses.
#include<iostream>
using namespace std;


int main() {


    int a = 10;
    char z = 'h';

    std::cout << "This is int a : " << 20 << endl;
    std::cout << "This is int a : " << a << endl;
    std::cout << "This is char z : " << z << endl;



    return 0;
}

 

  • <, <= signs: They are used to distinguish between greater than >, less than <, greater than or equal to <=, or less than or equal to >=.
// operators example

#include <iostream>
using namespace std;

int main()
{
    int x = 5;
    int y = 10;

    if (x <= y)
    {
        std::cout << "Hello World\n";
    }

    return 0;
}

 

  • Signs ==, !=: They are signs inside conditional sentences, equal to == or not equal to !=.
#include <iostream>
using namespace std;

int main()
{
    int x = 10;
    int y = 10;
    int z = -30;

    if (x == y && x != z)
    {
        std::cout << "Hello World\n";
    }

    return 0;
}

 

  • my mark || logical OR: which are used inside conditional sentences, and their presence indicates or.
#include <iostream>
using namespace std;

int main()
{
    int x = 10;
    int y = 10;
    int z = -30;

    if (x == y || x != z)
    {
        std::cout << "Hello World\n";
    }

    return 0;
}

 

  • && Logical And: which are used inside conditional sentences, and their presence indicates and.
#include <iostream>
using namespace std;

int main()
{
    int x = 10;
    int y = 10;
    int z = -30;

    if (x == y && x != z)
    {
        std::cout << "Hello World\n";
    }

    return 0;
}
  • Signs +=, -=: They are used as a shortcut to repeat the mention of the variable. The following example illustrates:
#include <iostream>
using namespace std;

int main()
{
    int x = 10;
    int y = 10;
    //traditional addition

    x = x + 5;

    //assignment addition

    y += 5;

    std::cout << "The traditional addition = " << x << endl;
    std::cout << "Our assignment addition = " << y << endl;


    //both same result

    return 0;
}

 

  • Sign = Assignment: It is the equality between two variables in value without conditions just for the sake of equality
#include <iostream>
using namespace std;

int main()
{
    int x = 10;
    int y = 50;

    x = y;

    std::cout << "x after assignment = " << x << endl;




    return 0;
}

 

3 comments

Leave a comment

Your email address will not be published. Required fields are marked *