0% found this document useful (0 votes)
6 views

4 Operator Precedence

Uploaded by

Zulqurnan Anjum
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

4 Operator Precedence

Uploaded by

Zulqurnan Anjum
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Computer Programming Lab

Experiment 4

Understanding Operator Precedence and Associativity & Conditional Statement

Objectives:

 To understand operator precedence


 To understand conditional statments in C++ and their usage in programs

Operator Precedence and Associativity:

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:

Category Operator Associativity

PostFix () [] -> . ++ - - Left to right

+ - ! ~ ++ - - (type)* &
Unary Right to left
sizeof

Multiplicative */% Left to right

Additive +- Left to right

Shift << >> Left to right

Relational < <= > >= Left to right

Equality == != Left to right

Designed & Prepared by Engr. Syed Ahtisham Mehmood Shah


Computer Programming Lab
Experiment 4

Bitwise AND & Left to right

Bitwise XOR ^ Left to right

Bitwise OR | Left to right

Logical AND && Left to right

Logical OR || Left to right

Conditional ?: Right to left

= += -= *= /= %=
Assignment Right to left
>>= <<= &= ^= |=

Comma , Left to right

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>

using namespace std;

int main(){

int a,b,c,d;

float e;

Designed & Prepared by Engr. Syed Ahtisham Mehmood Shah


Computer Programming Lab
Experiment 4

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;

Also consider the following code as example:

#include<iostream>

#include<conio.h>

using namespace std;

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++);

Designed & Prepared by Engr. Syed Ahtisham Mehmood Shah


Computer Programming Lab
Experiment 4

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>

using namespace std;

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;

If Else If Else Statement:

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;

Designed & Prepared by Engr. Syed Ahtisham Mehmood Shah


Computer Programming Lab
Experiment 4

else if (Condition ){

Statements;

else {

Statements;

The flow chart is given as:

If Statement

Test
Condition

False True

Code inside if block


Else if
condition
False True

Code inside else block Code inside else if block

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>

using namespace std;

Designed & Prepared by Engr. Syed Ahtisham Mehmood Shah


Computer Programming Lab
Experiment 4

int main(){

float in,out;

cout<<"Please Enter The Number"<<endl;

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>

using namespace std;

Designed & Prepared by Engr. Syed Ahtisham Mehmood Shah


Computer Programming Lab
Experiment 4

int main(){

float in,out;

cout<<"Please Enter The Number"<<endl;

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;

Designed & Prepared by Engr. Syed Ahtisham Mehmood Shah


Computer Programming Lab
Experiment 4

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>

using namespace std;

int main(){

int in,out;

cout<<"Please Enter The Number"<<endl;

cin>>in;

switch(in){

case 95:

cout<<"The entered number which is "<<in<<" so the grade will be A+ "<<endl;

break;

case 90:

cout<<"The entered number which is "<<in<<" so the grade will be A- "<<endl;

break;

case 80:

Designed & Prepared by Engr. Syed Ahtisham Mehmood Shah


Computer Programming Lab
Experiment 4

cout<<"The entered number which is "<<in<<" so the grade will be B+ "<<endl;

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:

Designed & Prepared by Engr. Syed Ahtisham Mehmood Shah

You might also like