Increment Operator
The operator that is used to add 1 to the value of a variable is called increment operator. It is represented by ++ (double plus sign). It is a unary operator. It is applied to a single variable only.The increment operator can be used before or after the variable name. In both cases, the variable is increment by 1 but their functions are difficult in expressions and other operator.
Therefore, increment operator is divided into two types:
i) Postfix Increment Operator
ii) Prefix Increment Operator
i) Postfix Increment Operator
When the increment operator is written after the variable name, it is known as Postfix increment operator. In this case, first the value of variable is used in the operation and then variable is increment by 1.
For example, to add 1 to variable 'a', the statement is written as :
a++ ;
ii) Prefix Increment Operator
When the increment operator is written before the variable name, it is known as Prefix increment operator. In this case, first the value of variable is incremented by 1 and then the value of variable is used in the operation.
For example, to add 1 to variable 'a', the statement is written as :
++a;
Write a program to evaluate an expressen "a+b++". supose 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++". supose 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;
}
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 = 3
Value of A = 6
Answer is = 8
Output of
the program
Value of A = 3
Value of A = 5
Value After A and B After Evaluation
Value of A = 3
Value of A = 6
Answer is = 8
No comments:
Post a Comment
Thanks For Visiting Here...!! I will Reply you as soon as possible.