28
Increment Operator To increment a variable X by 1. X+1; X+=; ++X; X = 10 Y = X + 1; Y = 11 X += 1; X = 11 Y = ++X + 4; Y = 15

Increment Operator To increment a variable X by 1. X+1;X+=;++X; X = 10 Y = X + 1;Y = 11 X += 1;X = 11 Y = ++X + 4;Y = 15

Embed Size (px)

Citation preview

Page 1: Increment Operator To increment a variable X by 1. X+1;X+=;++X; X = 10 Y = X + 1;Y = 11 X += 1;X = 11 Y = ++X + 4;Y = 15

Increment Operator

• To increment a variable X by 1.

X+1; X+=; ++X;

X = 10

Y = X + 1; Y = 11

X += 1; X = 11

Y = ++X + 4; Y = 15

Page 2: Increment Operator To increment a variable X by 1. X+1;X+=;++X; X = 10 Y = X + 1;Y = 11 X += 1;X = 11 Y = ++X + 4;Y = 15

Increment Operator

• To increment a variable X by 1.

X+1; X+=; ++X;

• Operator before the variable are called prefix.

X = 10

X = X + 1; X = 11

X += 1; X = 12

Y = ++X + 4; Y = 17

Page 3: Increment Operator To increment a variable X by 1. X+1;X+=;++X; X = 10 Y = X + 1;Y = 11 X += 1;X = 11 Y = ++X + 4;Y = 15

Example 6

#include<iostream.h>

int main()

{

int x, y;

x=10; //initial value for x

x=x+1; //increment x by 1

cout <<"x=" <<x

<<endl;

x+=1; //increment x by 1

cout <<"x=" <<x

<<endl;

y=++x+4; //increment x by 1 and add 4

cout <<"y=" <<y <<"\t and \t x=" <<x

<<endl;

return 0;

}

Page 4: Increment Operator To increment a variable X by 1. X+1;X+=;++X; X = 10 Y = X + 1;Y = 11 X += 1;X = 11 Y = ++X + 4;Y = 15

Increment Operator

• The operator can also be written after the variable to which it applies.

• Operator before the variable are called postfix.

• Effect of postfix in operation is slightly different. The incrementing of the variable to which it applies occurs after its value is used in context.

Page 5: Increment Operator To increment a variable X by 1. X+1;X+=;++X; X = 10 Y = X + 1;Y = 11 X += 1;X = 11 Y = ++X + 4;Y = 15

Example 7

#include<iostream.h>

int main()

{

int x, y;

x=10; //initial value for x

x=x+1; //increment x by 1

cout <<"x=" <<x

<<endl;

x+=1; //increment x by 1

cout <<"x=" <<x

<<endl;

y=4 + x++ ; //add 4 to x and increment x by 1

cout <<"y=" <<y <<"\t and \t x=" <<x

<<endl;

return 0;

}

Page 6: Increment Operator To increment a variable X by 1. X+1;X+=;++X; X = 10 Y = X + 1;Y = 11 X += 1;X = 11 Y = ++X + 4;Y = 15

Decrement Operator

• To decrement a variable X by 1.

X = X - 1

--X

X--

-=X

Page 7: Increment Operator To increment a variable X by 1. X+1;X+=;++X; X = 10 Y = X + 1;Y = 11 X += 1;X = 11 Y = ++X + 4;Y = 15

Example 8

#include<iostream.h>

int main()

{

int x, y, z;

x=10; //initial value for x

x=x-1; //decrement x by 1

cout <<"x=" <<x

<<endl;

x-=1; //decrement x by 1

cout <<"x=" <<x

<<endl;

y=--x +4; //decrement x by 1 and add 4

cout <<"y=" <<y <<"\t and \t x=" <<x

<<endl;

z=4 + x--; //add 4 to x and decrement x by 1

cout <<"z=" <<z <<"\t and \t x=" <<x

<<endl;

return 0;

}

Page 8: Increment Operator To increment a variable X by 1. X+1;X+=;++X; X = 10 Y = X + 1;Y = 11 X += 1;X = 11 Y = ++X + 4;Y = 15

Relational Operators

• < less than

• <= less than equal

• > greater than

• >= greater than equal

• == equal to

• != not_eq not equal to

Page 9: Increment Operator To increment a variable X by 1. X+1;X+=;++X; X = 10 Y = X + 1;Y = 11 X += 1;X = 11 Y = ++X + 4;Y = 15

Logical Operators

• ! not logical NOT

• && and logical AND

• || or logical OR

Page 10: Increment Operator To increment a variable X by 1. X+1;X+=;++X; X = 10 Y = X + 1;Y = 11 X += 1;X = 11 Y = ++X + 4;Y = 15

Bitwise Operators

• Bitwise operators treat their operands as a series of individual bits rather than a numerical value.

• They work with integer or constants.

Page 11: Increment Operator To increment a variable X by 1. X+1;X+=;++X; X = 10 Y = X + 1;Y = 11 X += 1;X = 11 Y = ++X + 4;Y = 15

Bitwise Operators

• ~ comp1 one’s complement

• << left shift

• >> right shift

• & bitand bitwise And

• ^ xor bitwise Exclusive-OR

• | bitor bitwise OR

Page 12: Increment Operator To increment a variable X by 1. X+1;X+=;++X; X = 10 Y = X + 1;Y = 11 X += 1;X = 11 Y = ++X + 4;Y = 15

Example 9

#include<iostream.h>

main()

{

long letter1 =0x41, letter2 =0x5A, x=0, y=0, z=0;

cout <<"letter1=" <<letter1 <<"\t\t letter2=" <<letter2;

cout <<endl;

x= ~letter1;

y=letter1 & letter2;

z=letter1 | letter2;

cout <<"x= " <<x <<"\t\t y=" <<y <<"\t\t z=" <<z;

cout <<endl;

return 0;

}

Page 13: Increment Operator To increment a variable X by 1. X+1;X+=;++X; X = 10 Y = X + 1;Y = 11 X += 1;X = 11 Y = ++X + 4;Y = 15

Assignment Operators

• = assignment

• += addition update

• -= subtraction update

• *= multiplication update

• /= division update

Page 14: Increment Operator To increment a variable X by 1. X+1;X+=;++X; X = 10 Y = X + 1;Y = 11 X += 1;X = 11 Y = ++X + 4;Y = 15

Assignment Operators

• %= modulus update

• <<= left shift update

• >>= right shift update

• &= and_eq bitwise AND update

• |= or_eq bitwise OR update

• ^= xor_eq bitwise Exclusive-OR

Page 15: Increment Operator To increment a variable X by 1. X+1;X+=;++X; X = 10 Y = X + 1;Y = 11 X += 1;X = 11 Y = ++X + 4;Y = 15

If Statement

• The if statement enables the programmer to test for a condition and branch to different parts of the code depending on the result.

If (expression)

statement;

Page 16: Increment Operator To increment a variable X by 1. X+1;X+=;++X; X = 10 Y = X + 1;Y = 11 X += 1;X = 11 Y = ++X + 4;Y = 15

Example 10

#include<iostream.h>

main()

{

int w, x, y, z;

cout <<"enter a value for w" << endl <<"w=";

cin>>w;

cout <<"enter a value for x" << endl <<"x=";

cin>>x;

if (w>x)

{

y=w+x;

cout <<"w is greater than x" <<endl <<"y=w+x=" <<y <<endl;

}

if (w<x)

{

z=w-x;

cout <<"x is greater than w" <<endl <<"z=w-x=" <<z <<endl;

}

if (w==x)

cout <<"A tie" <<endl;

cout <<"Thanks for telling me. \n\n";

return 0;

}

Page 17: Increment Operator To increment a variable X by 1. X+1;X+=;++X; X = 10 Y = X + 1;Y = 11 X += 1;X = 11 Y = ++X + 4;Y = 15

The else keyword

• A program should take one branch if a condition is true, and another branch if the condition is false.

if (expression)

statement;

else

statement;

Page 18: Increment Operator To increment a variable X by 1. X+1;X+=;++X; X = 10 Y = X + 1;Y = 11 X += 1;X = 11 Y = ++X + 4;Y = 15

Example 11

#include<iostream.h>

main()

{

int w, x, y, z;

cout <<"enter a value for w" << endl <<"w=";

cin>>w;

cout <<"enter a value for x" << endl <<"x=";

cin>>x;

if (w>x)

{

y=w+x;

cout <<"w is greater than x" <<endl <<"y=w+x=" <<y <<endl;

}

else

{

z=w-x;

cout <<"x is greater than w" <<endl <<"z=w-x=" <<z <<endl;

}

cout <<"Thanks for telling me. \n\n";

return 0;

}

Page 19: Increment Operator To increment a variable X by 1. X+1;X+=;++X; X = 10 Y = X + 1;Y = 11 X += 1;X = 11 Y = ++X + 4;Y = 15

Advanced if statement

• Any statement can be used in an if or else clause. if (expression1)

{

if (expression2)

statement1;

else

{

if (expression3)

statement2;

else

statement3;

}

}

else

statement4;

Page 20: Increment Operator To increment a variable X by 1. X+1;X+=;++X; X = 10 Y = X + 1;Y = 11 X += 1;X = 11 Y = ++X + 4;Y = 15

Switch Statement

• The switch statement enables you to select from multiple choice on a set of fixed values for a given expression.

Page 21: Increment Operator To increment a variable X by 1. X+1;X+=;++X; X = 10 Y = X + 1;Y = 11 X += 1;X = 11 Y = ++X + 4;Y = 15

Example 12#include<iostream.h>

main()

{

int x=0;

cout <<"Please select one of the following delicious dishes:" << endl

<<"1 \t Hamburger" <<endl

<<"2 \t Hamburger and coke" <<endl

<<"3 \t Coke" <<endl;

cout <<"Enter your choice \t";

cin>>x;

switch(x)

{

case 1: cout << endl <<"Hamburger" <<endl;

break;

case 2: cout << endl << "Hamburger and coke" <<endl;

break;

case 3: cout << endl << "Coke" <<endl;

break;

default: cout <<"You entered a wrong number" <<endl;

}

return 0;

}

Page 22: Increment Operator To increment a variable X by 1. X+1;X+=;++X; X = 10 Y = X + 1;Y = 11 X += 1;X = 11 Y = ++X + 4;Y = 15

Loop

• A loop executes a sequence of statements until a particular condition is true (or false).

loop:

statements

if ( )

goto loop;

Page 23: Increment Operator To increment a variable X by 1. X+1;X+=;++X; X = 10 Y = X + 1;Y = 11 X += 1;X = 11 Y = ++X + 4;Y = 15

Example 13

#include<iostream.h>

int main()

{

int i=1,y=20, x;

loop:

x=y+i;

cout <<"x=" <<x <<"\t i=" <<i <<"\t y=" <<y <<endl;

if (++i <= y)

goto loop;

cout <<"I love C++" <<endl;

return 0;

}

Page 24: Increment Operator To increment a variable X by 1. X+1;X+=;++X; X = 10 Y = X + 1;Y = 11 X += 1;X = 11 Y = ++X + 4;Y = 15

for Loop

• General form of the for loop is:

for (initializing; test; increment)

statements;

Page 25: Increment Operator To increment a variable X by 1. X+1;X+=;++X; X = 10 Y = X + 1;Y = 11 X += 1;X = 11 Y = ++X + 4;Y = 15

Example 14

#include<iostream.h>

int main()

{

int i=1,y=20, x;

for(i;i<=y;i++)

{

x=y+i;

cout <<"x=" <<x <<"\t i=" <<i <<"\t y=" <<y <<endl;

}

cout <<"I love C++" <<endl;

return 0;

}

Page 26: Increment Operator To increment a variable X by 1. X+1;X+=;++X; X = 10 Y = X + 1;Y = 11 X += 1;X = 11 Y = ++X + 4;Y = 15

Example 15

#include<iostream.h>

int main()

{

int y=20, x;

for(int i=1;i<=y;i++)

{

x=y+i;

cout <<"x=" <<x <<"\t i=" <<i <<"\t y=" <<y <<endl;

}

cout <<"I love C++" <<endl;

return 0;

}

Page 27: Increment Operator To increment a variable X by 1. X+1;X+=;++X; X = 10 Y = X + 1;Y = 11 X += 1;X = 11 Y = ++X + 4;Y = 15

Example 16

#include<iostream.h>

int main()

{

int i=1, y=20, x;

for(; i<=y; i++)

{

x=y+i;

cout <<"x=" <<x <<"\t i=" <<i <<"\t y=" <<y <<endl;

}

cout <<"I love C++" <<endl;

return 0;

}

Page 28: Increment Operator To increment a variable X by 1. X+1;X+=;++X; X = 10 Y = X + 1;Y = 11 X += 1;X = 11 Y = ++X + 4;Y = 15

Example 17

#include<iostream.h>

int main()

{

int i=1, y=20, x;

for(;i<=y;x=y+i++, cout <<"x=" <<x <<"\t i=" <<i <<"\t y=" <<y <<endl);

cout <<"I love C++" <<endl;

return 0;

}