
C++ Constants and Operators Overview
Learn about constants in C++, including integer, floating point, character, and string constants. Understand how the const qualifier is used to declare constant variables and explore the concept of compile-time errors. Discover the #define directive for defining constant quantities and get insights into using operators in arithmetic expressions with proper precedence rules.
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
Instructor: Instructor: Engr.Romana Farhan Assistant Professor CPED Engr.Romana Farhan Assistant Professor CPED
Constants can not change their value during program execution. There are 4 types of constants in C++ Integer constant Floating point constant Character constant String constant The const qualifier is used to make a variable constant. Value to constant is initialized at its declaration. const float pi=3.1416;
#include<iostream> using namespace std; int main() { Int r; const float p=3.14; float peri; r=2; peri=2*p*r; cout<< result is= <<peri; return 0; }
Compile Compile- -Time Error Time Error
#define directive is also used to define a constant quantity. Following is the syntax of #define directive #define identifier constant Example #define pi 3.1416 NOTE: The identifier does not have any data type and any value can be assigned to it. The name of identifier can not be used in the program.
#include<iostream> #define p 3.14 using namespace std; int main() { int r; float peri; r=2; peri=2*p*r; cout<< result is= <<peri; return 0; }
#include <iostream> using namespace std; int main () { int i; cout << "Please enter an integer value: "; cin >> i; cout << "The value you entered is " << i; cout << " and its double is " << i*2 << ".\n"; return 0; }
All the operators on previous slide are binary operators as they require two operands. C++ applies the operators in arithmetic expressions in a precise sequence determined by the following rules of operator precedence, which are generally the same as those followed in algebra
Evaluate Y = 2*5*5+3*5+7; Y=(4-(3*5))+2 What is your answer?
Relational operators specify a relationship between two operands. The operands may be variables, constants or expressions. a >= b 10 < 20 ( a + b ) > ( c * d ) The relational operators along with the operands form a relational expression. The result of a relational expression is either true or false.
C++ provides logical operators that are used to form complex conditions by combining simple conditions. The logical operators are && (logical AND) || (logical OR) ! (logical NOT, also called logical negation).
The && (logical AND) operator is used to ensure that two conditions are both true before we choose a certain path of execution. The simple condition to the left of the && operator evaluates first. If necessary, the simple condition to the right of the && operator evaluates next. The right side of a logical AND expression is evaluated only if the left side is true.
The || (logical OR) operator determines if either or both of two conditions are true before we choose a certain path of execution. If any one of the simple conditions to the left or right of || is true, the output will be true. The && operator has a higher precedence than the || operator. Both operators associate from left to right.
C++ provides the ! operator (logical NOT) which is also called logical negation operator. It reverses a condition s meaning, i.e., if (x>y) is true then !(x>y) will be false. The unary logical negation operator has only a single condition as an operand.
What is short-circuit evaluation in context of && and || operators?
C++ provides several assignment operators for abbreviating assignment expressions. Any statement of the form variable = variable operator expression; in which the same variable appears on both sides of the assignment operator and operator is one of the binary operators +, -, *, /, or % , can be written in the form variable operator= expression; Thus the statement c = c + 3 which adds 3 to c can be written as c += 3
Write a program in c++ to assign three values to three integers a,b,c. Add variables a & b and multiply their sum to variable c by using compound assignment statements. Sol: #include<iostream> using namespace std; int main() { int a,b,c; a=3; b=6;c=9; c*= a+b; cout<< result= <<c; }
C++ also provides two unary operators for adding 1 to value of a variable or subtracting 1 from the value of a numeric variable. These are the unary increment operator ++ unary decrement operator --
#include<iostream> using namespace std; int main() { int a,b,c,sum; a=1,b=1,c=3; sum=a+b+(++c); cout<<"sum="<<sum; //sum=6 cout<<"c="<<c; //c=4 return 0; }
#include<iostream> using namespace std; int main() { int a,b,c,sum; a=1,b=1,c=3; sum=a+b+(c++); cout<<"sum="<<sum; //sum=5 cout<<"c="<<c; //c=4 return 0; }
#include<iostream> using namespace std; int main() { int a,b,c,sum; a=1,b=1,c=3; sum=a+b+(--c); cout<<"sum="<<sum; //sum=4 cout<<"c="<<c; //c=2 return 0; }
#include<iostream> using namespace std; int main() { int a,b,c,sum; a=1,b=1,c=3; sum=a+b+(c--); cout<<"sum="<<sum; //sum=5 cout<<"c="<<c; //c=2 return 0; }