"Z Code Writer" is an Online Blog for Programmer & Beginner. Here, You will Find tech, Build your Programming Skills and other Programming Language Like C, C++, HTML, Java Script Java Code, Web Designing and University Of Sialkot (USKT) CGPA / GPA Calculator etc.

Friday, 12 July 2019

Decrement Operator

Decrement Operator

      The operator that is used to subtract 1 from the value of a variable is called decrement operator. It is represented by -- (double plus sign). It is a unary operator. It is applied to a single variable only.
      The decrement operator can be used before or after the variable name. In both cases, the variable is decrement by 1 but their functions are difficult in expressions and other operator.
      Therefore, increment operator is divided into two types:
   i)       Postfix Decrement Operator
   ii)      Prefix Decrement Operator


i)    Postfix Decrement Operator

      When the decrement operator is written after the variable name, it is known as postfix decrement operator. In this case, first the value of variable is used in the operation and then variable is decrement by 1.
      For example, to subtract 1 from variable 'a', the statement is written as :
        a-- ;

ii)   Prefix Decrement Operator 

       When the decrement operator is written before the variable name, it is known as Prefix decrement operator. In this case, first the value of variable is decremented by 1 and then the value of variable is used in the operation. 
      For example, to subtract 1 from variable 'a', the statement is written as :
        --a ;


Write a program to evaluate an expressen "--a+b--". suppose the value of a = 3 and b = 5. Display the result on screen In C.

#include < stdio.h >
#include < conio.h >
void main ( )
{
     int a = 3 , b = 5 , ans ;
     printf ( " Value After A and B Before Evaluation \n " ) ;
     printf ( " Value Of  A = %d \n " , a ) ;
     printf ( " Value Of  B = %d \n " , b ) ;
     ans = --a+b-- ;
     printf ( " Value After A and B After Evaluation \n " ) ;
     printf ( " Value Of  A = %d \n " , a ) ;
     printf ( " Value Of  B = %d \n " , b ) ;
     printf ( " Answer is = %d \n " , ans ) ;
}

Write a program to evaluate an expressen "--a+b--". suppose the value of a = 3 and b = 5. Display the result on screen In C++.


#include < iostream >
using namespace std;
int main ( )
{
     int a = 3 , b = 5 , ans ;
     cout << " Value After A and B Before Evaluation \n ";
     cout << " Value Of  A = " << a ;
     cout << " Value Of  B = " << b ;
     ans = --a+b-- ;
     cout << " Value After A and B After Evaluation \n " ; 
     cout << " Value Of  A = " << a ;
     cout << " Value Of  B = " << b ;
     cout << " Answer is = " << ans ; 
     return 0;
}


Output of the program

Value After A and B Before Evaluation
Value of A = 3
Value of A = 5
Value After A and B After Evaluation
Value of A = 2
Value of A = 4
Answer is = 7

No comments:

Post a Comment

Thanks For Visiting Here...!! I will Reply you as soon as possible.