5. Operators And Expressions

Previous (4.cpp) <<

Next 6.cpp >>

Operators ::
    Statment vs Expression 
    example : 
        int totalMarks = a +b +c ; 
        //is a statement , while a+b+c is expression ;
        //statements may contains expression ;
        //expression have a value or a result 
        //a+b+c > 30 is a boolean expression , returns a boolean value 

        //the '>' is a kind of operators , and (a+b+c) and 30 are called Operands 
        //depending on operands operator's functionality changes like Overloaded functions 
        //we have operator Overloading example '<<' is a overloaded operator 
        //Operator working between two operands is binary operator 
        //Operator working with only one Operands is unary operator


ArithMatic Operator
    '+' ,'-' ,'*' ,'/' ,'%'

Increment Decrement Operator 
    a = a + 1 ; //  is replaced by a++ or ++a 
    so ,
    '++a', // means : before next semicolon use value of a as it is a+1 and after semicolon increment a by 1
    'a++', // means : before next semicolon use value of a as it is a and after semicolon increment a by 1
    '--a' ,  //  means : before next semicolon use value of a as it is a -1 and after semicolon decrement a by 1
    'a--'    // means : before next semicolon use value of a as it is a and after semicolon decrement a by 1



Compound  Assignment Operator
    // '=' is assignment operator
    // '==' is a comparison opertor

    a = a+b; // statement can be written to    ' a+= b'
    a = a * b                   >>>>                       ' a*= b'
    a = a - b                   >>>>                       '  a-=b'
    a = a/b                      >>>>>                     ' a /=b' 
these are called compound assignment operators 
    '+=' , '-=' , '*=' , '/=' , '%=' 

Relational Operator 
    //  put relation ,  generally have boolean value , these all are comparison Operators 
    '==' , '!=', '>' , '<' , '>=', '<=' 
    
Logical Operators 
    // like and , or , provides logic operation between boolean data (generally boolean ) 
    '&&' , '||' , '!' 
    &&  : and 
    || :       or 
    ! :       not   // example              if( !isRainy ) 

 // mechanism ( short-circuting in case of logical operators ) 
        // if( expression1 && ..... ) if expression1 is false then compiler don't evaluate further expression 
        // if( expression1 || ex...    ) if expression1 is true then compiler don't bother to evaluate further 

BitWiseOperator
    &,| ,^ ,~ : and , or ,xor and not respectively 
    a&b if  we convert a and b to binary and just do logical operation between each bits then the final number comes out will be the result of the bitwise operation
    // even numbers with 1 always give 0 with bitwise operator
    
int main() {
    int n;
    cin >> n;
    
    //bitwise 
    if( n& 1)  cout << "Odd" <<endl; // and
    else cout<<"even" << endl;
    
    cout<< ( 2|5 ) <<endl; // or
    cout<< ( 5^7) <<endl; // xor 
    cout<< ( ~0 )  << endl; // not  output : -1
    cout<< ( ~1 ) << endl ; // not  output : -2

    return 0;
}

Left and Right shift operators 
     >>, << etc 
    right shift >> 
       a>> b : means right shift of bits of number a and inserting 0's  from left and remove b bits from right 
 
// right shift by 1 means division by 2 
// right shift by 2 means division by 4
// a >> b is equivalent to a/( 2^b)
// left shift works just opposite 

Other Operators 
    // sizeof(int) 

    // x%2==0 ? cout<<"even" : cout<<"odd";
    
    // dot operator (.) 
    // arrow operator (->) 
    // printing << 
    // reading >> 
Previous (4.cpp) <<

Next 6.cpp >>


Comments

Popular posts from this blog