Вы находитесь на странице: 1из 4

Ch.

4 Manipulating Data with operators (Additional C


operators)
1. Arithmetic additional C operators:
+= Addition assignment x+=y
x=x+y
-= Subtraction assignment x -=y
x=xy
*= Multiplication assignment x *= y
x=x*y
/= Division assignment x /= y x
=x/y
%= Reminder assignment x %= y x
=x%y

Ex. p.p 48
Note that : z*=y+x = z*(y+x)=z*y+z*x and this
is not the same as :
Z*=y+x z=z*y+x
2. Unary Minus sign (related to the negative sign
operator):
It is the negative of a variable; if x = 12
-x = -12
In C- we can write z =x - - y , this is equivalent to z
= x (-y)

Unary Subtraction
minus operator
3. Increment and decrement operators:
Increment is the increase by "1" x=x+1
Decrement is the decrease by "1" x=x-1
x=x+1 is equivalent to ++x or x++
x=x-1 is equivalent to - -x or x- -


pre process
post process

It should be noted that in some cases an operator can be used


either pre-increment ( ++x ) or post-increment ( x++ ). A pre-
increment operator tells the compiler, Increment the variables
value and then use the variable. A post-increment operator tells
the compiler, Use the variable first and then increment its value.
For example, this code:
x = 10 cout << x = x <<
x = 11 endl;
x = 12 will result in the
x = 12 following output:
Ex.1
# include <stdio.h>
main()
{ int x; x=5;
Printf("The value of x without change =%d\n", x); // x=5
Printf("The value of x with pre-increment =%d\n",++ x); // x=6
Printf("The value of x after increment =%d\n", x); // x=6
Printf("The value of x with post-increment =%d\n", x++); // x=6
Printf("The value of x after post-increment =%d\n", x); // x=7
Return 0;}
Ex.2
# include <stdio.h>
main()
{ int x= 7;
Printf("x1=%d\n", x); // x1=7
Printf("x2=%d\n", x++); // x2=7
Printf("x3=%d\n", x); // x3=8
Printf("x4=%d\n", ++x); // x4=9
Printf("x5=%d\n", x--); // x5=9
Printf("x6=%d\n", x); // x6=8
Printf("x7=%d\n",-- x); // x7=7
Printf("x8=%d\n", x); // x8=7

Sammary of "Equality, Logical and Relational Operators":


.Table: Relational, equality and logical operators
Algebraic
C++ relational
Meaning of C++ Sample C++ relational or
or equality
condition condition equality
operator
operator
x is greater
x>y < <
than y
x is less than y x <y > > Relational
x is greater than or
x >= y =< operator
equal y
x is less than or
x <= y =>
equal y
x is equal to y x == y == =
Equality
x is not equal to
x != y =! operator
y
if (x>=1 && AND Logical
x12 &&
x<=2) () Operators
if (x<1||x>2) if (x>=1 || || OR
x<=2) ( )

Library Functions
// sqrt.c
// demonstrates sqrt() and pow(,) library function
#include <stdio.h>
#include <math.h> //for sqrt() and pow(,)
int main(){
double number, sqrt_root,power_to; //sqrt() and pow(,)require type double
printf( Enter a number: );
scanf(" %f ",number ); //get the number
sqrt_root= sqrt(number); //find square root
power_to= pow(number,5) // findnumber to the power 5
printf("Square root of number is : %f", sqrt_root);
printf("number to the power 5 is : %f", power_to);
return 0; }
Example of Relational Operators
// relat.c
// demonstrates relational operators This program performs three
#include <stdio.h> kinds of comparisons between
int main(){ 10 and a number entered by
int numb; the user.
printf( Enter a number: ); Heres the output when the
scanf("%d", numb); user enters 20:
printf(numb<10 is,(numb < 10));
printf(numb>10 is , (numb > 10)); Enter a number: 20
printf(numb==10 is , (numb == 10)); numb<10 is 0
return 0;} numb>10 is 1
numb==10 is 0
As you can see from the output, the C++ compiler considers that a true
expression has the value 1, while a false expression has the value 0.

Вам также может понравиться