‹ back to Programming C++

NotesProgramming C++ · 29 November 2015

Operators

Introduction to Computing: Operators and if else

Through out the programming from basics to advance , we need operations to solve our problems

All operations can be categorized under following groups

1. Assignment Operator

  1. Arithmetic Operators
  2. Relational Operators
  3. Bitwise Operators
  4. Logical Operator

Assignment Operator

Symbol =

Copy value from right hand side to left hand side

EXAMPLE

#include<iostream>

using namespace std;

main()

{

cout<<“program to represent = operator”<<endl;

int a=0; int b=0;

cout<<“input value for a”<<endl; //ask user to enter the value of a

cin>>a;

b=a; // equating b=1;

cout<<“value of b is “<<b<<endl;

}

Arithmetic Operators

operator Description
+ Adds two operands
– Subtracts second operand from the first
* Multiplies both operands
/ Divides numerator by de-numerator
% Modulus Operator and remainder of after an integer division
++ Increment operator, increases integer value by one
— Decrement operator, decreases integer value by one
#include<iostream>

using namespace std;

main()

{

cout<<“program to represent aRITHMATIC operations”<<endl;

int a=0;

int b=0;

cout<<“Enter Value for a and b “<<endl;

cin>>a;

cin>>b;

cout<<“Addition +”<<endl;

cout<<a+b<<endl;

cout<<“Subtraction -“<<endl;

cout<<a-b<<endl;

cout<<“Multiplication *”<<endl;

cout<<a*b<<endl;

cout<<“division / -“<<endl;

cout<<“a/b”<<endl;

cout<<a/b<<endl;

cout<<“b/a”<<endl;

cout<<b/a<<endl;

cout<<“modulus %”<<endl;

cout<<“a moduls 2″<<endl;

cout<<a%2<<endl;

cout<<a%3<<endl;

cout<<“increment a ++”<<endl;

cout<<++a<<endl;

cout<<“decremnet b –“<<endl;

cout<<–b<<endl;

}

If operator

The if statement is a powerful decision making statement and used to control the flow of execution of statements.

Syntax

if(condition)
{
Block of Statement
}
statement;

#include <iostream>

using namespace std;

main()

{

cout<<“enter your gender m or f “<<endl;

char a=0;

cin>>a;

if(a==’m’)

{

cout<<“user is male”<<endl;

}

if(a==’f’)

{

cout<<“user is female”<<endl;

}

}

Relational operators

Operator Description
== Checks if the values of two operands are equal or not,
!= Checks if the values of two operands are equal or not
> Checks if the value of left operand is greater than the value of right operand.
< Checks if the value of left operand is less than the value of right operand
>= Checks if the value of left operand is greater than or equal to the value of right operand,
<= Checks if the value of left operand is less than or equal to the value of right operand
#include <iostream>

using namespace std;

main()

{

int a = 0;

int b = 0;

int c=0 ;

cout<<“enter the value of a and b”<<endl;

cin>>a;

cin>>b;

if( a == b )

{

cout << “yes a = b” << endl ;

}

else

{

cout << “no a is not equal to b ” << endl ;

}

if ( a < b )

{

cout << “yes a < b” << endl ;

}

else

{

cout << “no a is not < b” << endl ;

}

if ( a > b )

{

cout << “yes a is greater than b ” << endl ;

}

else

{

cout << “no a is not greater than b” << endl ;

}

if ( a <= b )

{

cout <<” a is either equal or less than to b” << endl ;

}

else

{

cout << “a is not less than or equal to b ” << endl ;

}

}

Bitwise Operators

Used for binary operations

#include <iostream>

using namespace std;

main()

{

int a;

int b;

int c;

cout<<“enter the value of a and b in 0 and 1 form “<<endl;

cin>>a;

cin>>b;

cout<<“performing and function bit wise”<<endl;

cout<<“the value of a was “<<a<<endl;

cout<<“the value of b was “<<b<<endl;

c=a&b;

cout<<“the answer is “<<c<<endl;

cout<<“performing or function bit wise “<<endl;

cout<<“the value of a was “<<a<<endl;

cout<<“the value of b was “<<b<<endl;

c=a|b;

cout<<“the answer is “<<c<<endl;

cout<<“performing xor function bit wise “<<endl;

cout<<“the value of a was “<<a<<endl;

cout<<“the value of b was “<<b<<endl;

c=a^b;

cout<<“the answer is “<<c<<endl;

cout<<“performing not function bit wise “<<endl;

cout<<“the value of a was “<<a<<endl;

c=a’;

cout<<“the answer of a is “<<c<<endl;

cout<<“the value of b was “<<b<<endl;

c=~b;

cout<<“the answer of a is “<<c<<endl;

}

Operator Description
& Performs and operation
| Performs or operation
^ Performs xor operation
~ Perform compliment function

Logical Operator

&& Called Logical AND operator. Applied on complete number
|| Called Logical OR Operator. Applied on whole number
! Called Logical NOT Operator. Applied on whole number

Understanding && operation

Ask user to enter his gpa and attendance . If attendance is less than 80 and GPA is less or equal to 2.0, inform him that he is on probation

#include <iostream>

using namespace std;

main()

{

float gpa=0;

float atted=0;

cout<<“Enter your Gpa”<<endl;

cin>>gpa;

cout<<“enter your attedance”<<endl;

cin>>atted;

if ( ( gpa >= 2.0 ) && ( atted >= 80 ) )

{

cout<<“you are good to go “<<endl;

}

else

{

cout<<“sorry bro… you are in prob”<<endl;

}

}

What really happened in this program

Lets say we have entered

GPA = 1.0

Atted= 99

Now we will try to see what will be the result of following conditions

if ( ( gpa >= 2.0 ) && ( atted >= 80 ) )

( gpa >= 2.0 ) = 0 (as entered GPA=1

( atted >= 80 )=1 (as entered atted is 99)

Now we know that 0 and 1 = 0 so condition is false , and else statement will be used

…………….

Understanding || operation

? Opertor

condition ? result1 : result2

/ conditional operator

#include <iostream>

using namespace std;

main ()

{

int a,b,c,d;

cin>>a;

cin>>b;

cin>>c;

d = (a>b) ? a : b;

d= (d>c) ? d : c;

cout << d << ‘n’;

}

First published on fatsup.wordpress.com ↗