4 Operator Precedence
4 Operator Precedence
Experiment 4
Objectives:
Operator precedence specifies the order of operations in expressions that contain more than one operator while
associativity determines how operators of the same precedence are grouped in absence of parenthesis. Together
with these two concepts, it is determined how an expression is evaluated. The table for operator precedence and
associativity is given as:
+ - ! ~ ++ - - (type)* &
Unary Right to left
sizeof
= += -= *= /= %=
Assignment Right to left
>>= <<= &= ^= |=
Example Codes:
The following code gives idea about operator precedence and associativity for arithmetic expressions. You are
required to first analyze the code and then verify your answers through this code. (Refer to class lectures)
#include<iostream>
#include<conio.h>
int main(){
int a,b,c,d;
float e;
a=2+5*10;
cout<<a<<endl;
b=3*7-6+2*5/4+6;
cout<<b<<endl;
c=18-32/6*3;
cout<<c<<endl;
d=25-7%3+8/3;
cout<<d<<endl;
e=18.0+5.0*3.0/4.0;
cout<<e<<endl;
getch();
return 0;
#include<iostream>
#include<conio.h>
int main(){
int a=5,b=6,c;
a=(b++)+3;
cout<<"a"<<"\t"<<"b"<<"\t"<<"c"<<endl;
cout<<a<<"\t"<<b<<"\t"<<c<<endl;
c=2*a+(++b);
cout<<a<<"\t"<<b<<"\t"<<c<<endl;
b=2*(++c)-(a++);
cout<<a<<"\t"<<b<<"\t"<<c<<endl;
getch();
return 0;
The following code gives idea about complex expressions. First evaluate the expression then verify through
code (Refer to class lectures).
#include<iostream>
#include<conio.h>
int main(){
int x=3,y,a;
a=2+3|12/6-1<<1&&6%7/3*5;
cout<<a<<endl;
y=x++ + ++x;
cout<<x<<"\t" <<y<<endl;
getch();
return 0;
These statements are used to execute a block of code if a certain condition is met. The syntax for if else if else
conditional statement is:
if (Condition ){
Statements;
else if (Condition ){
Statements;
else {
Statements;
If Statement
Test
Condition
False True
Example Code:
The following program accepts input number from user and checks whether the entered number is greater than,
less than or equal to 10. If entered number is greater than 10, then it returns (entered number + 2) as output. If
the entered number is less than 10, then it returns (entered number + 8) as output. If the entered number is equal
to 10, then it returns the entered number as it is.
#include<iostream>
#include<conio.h>
int main(){
float in,out;
cin>>in;
if (in>10){
out=in+2;
cout<<"The entered number which is "<<in<<" is greater than 10 so the incremented output number by 2 is
"<<out<<endl;
else if (in<10){
out=in+8;
cout<<"The entered number which is "<<in<<" is less than 10 so the incremented output number by 8 is
"<<out<<endl;
else {
cout<<"The entered number which is "<<in<<" is equal to 10 so there is no increment and the output is
"<<in<<endl;
getch();
return 0;
The same output can be achieved by using conditional operator (ternary operator as discussed in lecture as well
as in the previous manual in miscellaneous operator topic).
#include<iostream>
#include<conio.h>
int main(){
float in,out;
cin>>in;
if (in!=10){
out=(in>10)?in+2:in+8;
cout<<out<<endl;
else{
cout<<"The entered number which is "<<in<<" is equal to 10 so there is no increment and the output is
"<<in<<endl;
getch();
return 0;
Switch Statement:
A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case
and the variable being switched on is checked for each switch case. The syntax for switch statement is:
switch (expression){
case 1:
statements;
break;
case 2:
statements;
break;
case 3:
statements;
break;
default:
statements;
The following program takes integer input from user for three numbers 80, 90 or 95. Based on the number
entered, it displays the grade the student will be awarded. If the entered number is not one of these numbers, then
program displays “ invalid”.
#include<iostream>
#include<conio.h>
int main(){
int in,out;
cin>>in;
switch(in){
case 95:
break;
case 90:
break;
case 80:
break;
default:
cout<<"not valid";
getch();
return 0;
Tasks:
Verify the assignment questions about operator precedence given in class lecture through code.
Write a program that takes input from user about their gender and age to validate if they are eligible to
vote or not. If age of any gender is greater than 18 years, then outputs you are eligible otherwise
ineligible.
Write a program that takes two input numbers from user and returns the greatest of them.
Write a program that takes temperature input from user and converts it to Fahrenheit and kelvin scales.
Then outputs “temperature is high” if the temperature is greater than 45C. “Normal” if it less than 30
and greater than 19C and displays “cold”otherwise.
Write a program that takes input from user between 1 to 7 and based on the input , displays the name of
day.
Conclusion: