SlideShare a Scribd company logo
1 of 40
Intro to Programming
with C++ /
Programming Using
C++
CSD 106 / ICT 120
Comments
• Used to document parts of the program
• Intended for persons reading the source code of
the program:
– Indicate the purpose of the program
– Describe the use of variables
– Explain complex sections of code
• Are ignored by the compiler
Single-Line Comments
Begin with // through to the end of line:
float adjacent = 2.5;// adjacent in
cm
float opposite = 3.5; // opposite in
cm
float hypotenuse; // hypotenuse
that will be calculated
Multi-Line Comments
• Begin with /*, end with */
• Can span multiple lines:
/* this is a multi-line
comment
*/
• Can begin and end on the same line:
int volume; /* calculated volume */
Data Type
• Fundamental Data Type
– Fundamental data types are basic types implemented
directly by the language that represent the basic
storage units supported natively by most systems.
– C++ supports three simple data types
• integer
• floating point
• character
numeric
Data Type
• Integer
– Is a whole number, either positive or negative
– Does not include decimal points
– Cannot be written with commas, dollar signs, or any
symbols other than a leading + or -.
– An integer variable is declared with the keyword int
Data Type
Data Type Size Range
int 4bytes -2147483648 to 2147483647
unsigned int 4bytes 0 to 4294967295
signed int 4bytes -2147483648 to 2147483647
short int 2bytes -32768 to 32767
unsigned short int 2bytes 0 to +65,535
long int 4bytes -2,147,483,647 to 2,147,483,647
Unsigned long int 4 bytes 0 to 4,294,967,295
Integer
Data Type
• Floating-Point
– Real or Floating-Point Numbers
• Numbers that include decimal points
• A floating point variable is declared with the keyword float
or double and long double. long double uses more
memory space than double. Likewise double occupies
more space than float.
Floating-Point Data Types
Data Type
• Character
– character may hold any single symbol in the ASCII
character set.
– It contains letters of the alphabet, digits, space,
punctuation mark, arithmetic symbol, or other
special symbol.
– Character value is always expressed in single quotes
‘’
– A character variable is declared with the keyword
char
The char Data Type
• Used to hold characters or very small integer
values
• Usually 1 byte of memory
• Numeric value of character from the character
set is stored in memory:
CODE:
char letter;
letter = 'C';
MEMORY:
letter
67
The Wider character Data
Type
• Usually more than 1 byte of memory
• A wider character variable is declared with the
keyword wchar_t
The C++ string Class
• Special data type supports working with strings
• Include the string header file
– #include <string>
• Define a string variable type called a string object using
the keyword string
– For example:
string address, occupation;
• Can receive values with assignment operator:
• Can be displayed via cout
cout << address << " " << occupation;
The string class
Variables
• Variable names correspond to locations in the computer’s
memory.
• It must be defined before it can be used.
• It include letters, numbers and underscores, but must
begin with a letter (a-z or A-Z) or underscore(_).
• No spaces or other special characters are allowed within a
C++ variable name.
• Examples of valid names
 Gender
 cumGPA
 Total_Cost
 _emailAddress
Variable Names
• A variable name should represent the purpose of
the variable. For example:
costOfItem
The purpose of this variable is to hold the cost of
item.
Variables
• Variable
– Note: C++ is case-sensitive as such a variable name
such as tax, Tax and TAX are different variable names
– Good programming practice: use all lowercase letters
for variable names or else capitalize only the first
letter of each new word after the first word, eg
firstSemesterGPA (camel case)
– C++ keywords cannot be used as a variable name.
Variables
• Variable
– Variable declaration is a C++ statement, all C++
statements must end with a semi-colon (;)
– Explicitly stating the value of a variable is called
assignment.
• It is achieved with the assignment operator =
– Assigning a value to a variable upon creation is often
referred to as initializing the variable.
Variables
• Variable
– Declaring, initializing, and assigning values to
variables
float tax_rate;
float amount = 525.45;
float tax_Amount;
tax_rate = 0.15;
tax_Amount = tax_rate * amount ;
Variables
• Variable
– The const Qualifier
• A variable that does not change in a program should be
declared as a constant
• The keyword used to declare a variable constant is const
• It is ideal to use all uppercase letters for a constant name
• General Syntax
const dataType VARIABLE_NAME;
Scope
• The scope of a variable: the part of the program
in which the variable can be accessed
• A variable cannot be used before it is defined
Variable Out of Scope
C++ Binary Arithmetic Operators
• C++ provides Five (5) simple arithmetic operators
for creating arithmetic expressions
• Each of these arithmetic operators is a binary
operator
– Each takes two operands, one on each side of the
operator
C++ Binary Arithmetic Operators
C++ Binary Arithmetic Operators
• Addition, subtraction, multiplication, or division of any
two integers results in an integer.
• If either or both of the operands in addition, subtraction,
multiplication or division is a floating – point number,
then the result is also a floating point number.
• Division (/ )
• Integer division truncates remainder
• Modulus (%)
– Modulus operator returns remainder
•
9 / 2 evaluates to 4
9 % 2 evaluates to 1
C++ Binary Arithmetic Operators
• Parentheses for Grouping Sub-expressions
– Parentheses are used in C++ expressions to group
sub-expressions
• Same manner as in algebraic expressions
– Example
• b * ( d + e )
– Multiple b times the quantity d + e
C++ Shortcut Arithmetic
Operators
• The following operators are valid shortcut
arithmetic operators:
+=
-=
*=
/=
• The assignment operator (=) always appears
second.
• You must not insert a space between the
operators
C++ Shortcut Arithmetic
Operators
Assignment
operator
Sample
expression
Explanation Assigns
Assume: int c = 3, d = 5, e = 4, f = 6, g = 12;
+= c += 7 c = c + 7 10 to c
-= d -= 4 d = d - 4 1 to d
*= e *= 5 e = e * 5 20 to e
/= f /= 3 f = f / 3 2 to f
%= g %= 9 g = g % 9 3 to g
C++ Binary Arithmetic Operators
• Rules of Operator Precedence
Operator(s) Operation(s) Order of evaluation (precedence)
( ) Parentheses Evaluated first. If the parentheses are nested, the
expression in the innermost pair is evaluated first.
If there are several pairs of parentheses “on the
same level” (i.e., not nested), they are evaluated left
to right.
*
/
%
Multiplication
Division
Modulus
Evaluated second. If there are several, they are
evaluated left to right.
+
-
Addition
Subtraction
Evaluated last. If there are several, they are
evaluated left to right.
C++ Binary Arithmetic Operators
• Example: Rules of Operator Precedence
C++ Unary Operators
• Unary operators are those operators that
require only one operand.
• The prefix and postfix increment and decrement
operators are examples of unary operators
++count
count++
--num
num--
Prefix increment
Postfix increment
Prefix decrement
Postfix decrement
C++ Unary Operators
• Prefix operator, the
mathematical operation
takes place before the
expression is evaluated.
• Postfix operator, the
mathematical operation
takes place after the
expression is evaluated.
int num = 6;
result = ++num;
cout << result; // result is 7
cout << num; // num is 7
int num = 6;
result = num++;
cout << result; // result is 6
cout << num; // num is 7
Mathematical Expressions
• An expression is a programming statement that has a
value.
• An expression usually consists of an operator and its
operands
• Can create complex expressions using multiple
mathematical operators
• An expression can be a literal, a variable, or a
mathematical combination of constants and variables
• Can be used in assignment, cout, other statements:
area = 2 * PI * radius;
cout << "border is: " << 2*(l+w);
Associativity of Operators
• - (unary negation) associates right to left
• *, /, %, +, - associate left to right
• parentheses ( ) can be used to override the order of
operations:
4 + 4 * 4 – 4 =
(4 + 4) * 4 – 4 =
4 + 4 * (4 – 4) =
(4 + 4) * (4 – 4) =
?
?
?
?
Evaluating Boolean Expressions
• Boolean Expression
– Evaluates as true or false
• C++ employs six relational binary operators.
Standard algebraic
equality or relational
operator
C++ equality
or relational
operator
Sample
C++
condition
Meaning of
C++ condition
Relational operators
 > x > y x is greater than y
 < x < y x is less than y
 >= x >= y x is greater than or equal to y
 <= x <= y x is less than or equal to y
Equality operators
= == x == y x is equal to y
≠ != x != y x is not equal to y
Tips
A syntax error will occur if any of the
operators ==, !=, >= and <= appears with
spaces between its pair of symbols.
Reversing the order of the pair of
symbols in any of the operators !=, >=
and <= (by writing them as =!, => and =<,
respectively) is normally a syntax error. In
some cases, writing != as =! will not be a
syntax error, but almost certainly will be
a logic error that has an effect at
execution time
Converting Algebraic expressions
to Programming Statements
• In algebra it is not always necessary to use an
operator for multiplication.
• C++, however, requires an operator for any
mathematical operation.
• In converting some algebraic expressions to C++,
you may have to insert parentheses that do not
appear in the algebraic expression
Converting Algebraic expressions
to Programming Statements
Converting Algebraic expressions
to Programming Statements
Exponents in C++
• Standard library function pow()
– Calculates an exponent
– Example
• xy = pow( x, y )
– Calculates the value of x raised to the yth power
– Requires header file <cmath>
• <cmath>
– Contains function prototypes for math library
functions

More Related Content

What's hot

Constant, variables, data types
Constant, variables, data typesConstant, variables, data types
Constant, variables, data typesPratik Devmurari
 
Java: Primitive Data Types
Java: Primitive Data TypesJava: Primitive Data Types
Java: Primitive Data TypesTareq Hasan
 
Data types in java | What is Datatypes in Java | Learning with RD | Created b...
Data types in java | What is Datatypes in Java | Learning with RD | Created b...Data types in java | What is Datatypes in Java | Learning with RD | Created b...
Data types in java | What is Datatypes in Java | Learning with RD | Created b...rahuldaredia21
 
Constants and variables in c programming
Constants and variables in c programmingConstants and variables in c programming
Constants and variables in c programmingChitrank Dixit
 
Constant and variacles in c
Constant   and variacles in cConstant   and variacles in c
Constant and variacles in cyash patel
 
Datatype in c++ unit 3 -topic 2
Datatype in c++ unit 3 -topic 2Datatype in c++ unit 3 -topic 2
Datatype in c++ unit 3 -topic 2MOHIT TOMAR
 
Data types and Operators
Data types and OperatorsData types and Operators
Data types and Operatorsraksharao
 
Data Type in C Programming
Data Type in C ProgrammingData Type in C Programming
Data Type in C ProgrammingQazi Shahzad Ali
 
Chapter1 c programming data types, variables and constants
Chapter1 c programming   data types, variables and constantsChapter1 c programming   data types, variables and constants
Chapter1 c programming data types, variables and constantsvinay arora
 
3 data-types-in-c
3 data-types-in-c3 data-types-in-c
3 data-types-in-cteach4uin
 
Complete Tokens in c/c++
Complete Tokens in c/c++Complete Tokens in c/c++
Complete Tokens in c/c++Shobi P P
 
What is identifier c programming
What is identifier c programmingWhat is identifier c programming
What is identifier c programmingRumman Ansari
 
Concept of c data types
Concept of c data typesConcept of c data types
Concept of c data typesManisha Keim
 
datatypes and variables in c language
 datatypes and variables in c language datatypes and variables in c language
datatypes and variables in c languageRai University
 
Chapter 2: Elementary Programming
Chapter 2: Elementary ProgrammingChapter 2: Elementary Programming
Chapter 2: Elementary ProgrammingEric Chou
 
Getting started with c++
Getting started with c++Getting started with c++
Getting started with c++K Durga Prasad
 

What's hot (20)

Constant, variables, data types
Constant, variables, data typesConstant, variables, data types
Constant, variables, data types
 
Java: Primitive Data Types
Java: Primitive Data TypesJava: Primitive Data Types
Java: Primitive Data Types
 
Data types in java | What is Datatypes in Java | Learning with RD | Created b...
Data types in java | What is Datatypes in Java | Learning with RD | Created b...Data types in java | What is Datatypes in Java | Learning with RD | Created b...
Data types in java | What is Datatypes in Java | Learning with RD | Created b...
 
Constants and variables in c programming
Constants and variables in c programmingConstants and variables in c programming
Constants and variables in c programming
 
Lecture02(constants, variable & data types)
Lecture02(constants, variable & data types)Lecture02(constants, variable & data types)
Lecture02(constants, variable & data types)
 
Constant and variacles in c
Constant   and variacles in cConstant   and variacles in c
Constant and variacles in c
 
Datatype in c++ unit 3 -topic 2
Datatype in c++ unit 3 -topic 2Datatype in c++ unit 3 -topic 2
Datatype in c++ unit 3 -topic 2
 
Data types and Operators
Data types and OperatorsData types and Operators
Data types and Operators
 
Data type in c
Data type in cData type in c
Data type in c
 
Data Type in C Programming
Data Type in C ProgrammingData Type in C Programming
Data Type in C Programming
 
Data types
Data typesData types
Data types
 
Chapter1 c programming data types, variables and constants
Chapter1 c programming   data types, variables and constantsChapter1 c programming   data types, variables and constants
Chapter1 c programming data types, variables and constants
 
Lect 9(pointers) Zaheer Abbas
Lect 9(pointers) Zaheer AbbasLect 9(pointers) Zaheer Abbas
Lect 9(pointers) Zaheer Abbas
 
3 data-types-in-c
3 data-types-in-c3 data-types-in-c
3 data-types-in-c
 
Complete Tokens in c/c++
Complete Tokens in c/c++Complete Tokens in c/c++
Complete Tokens in c/c++
 
What is identifier c programming
What is identifier c programmingWhat is identifier c programming
What is identifier c programming
 
Concept of c data types
Concept of c data typesConcept of c data types
Concept of c data types
 
datatypes and variables in c language
 datatypes and variables in c language datatypes and variables in c language
datatypes and variables in c language
 
Chapter 2: Elementary Programming
Chapter 2: Elementary ProgrammingChapter 2: Elementary Programming
Chapter 2: Elementary Programming
 
Getting started with c++
Getting started with c++Getting started with c++
Getting started with c++
 

Similar to Lecture 2 variables

M.Florence Dayana / Basics of C Language
M.Florence Dayana / Basics of C LanguageM.Florence Dayana / Basics of C Language
M.Florence Dayana / Basics of C LanguageDr.Florence Dayana
 
Esoft Metro Campus - Programming with C++
Esoft Metro Campus - Programming with C++Esoft Metro Campus - Programming with C++
Esoft Metro Campus - Programming with C++Rasan Samarasinghe
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAAiman Hud
 
Basics of c++ Programming Language
Basics of c++ Programming LanguageBasics of c++ Programming Language
Basics of c++ Programming LanguageAhmad Idrees
 
2nd PUC Computer science chapter 5 review of c++
2nd PUC Computer science chapter 5   review of c++2nd PUC Computer science chapter 5   review of c++
2nd PUC Computer science chapter 5 review of c++Aahwini Esware gowda
 
Basics of C.ppt
Basics of C.pptBasics of C.ppt
Basics of C.ppt8759000398
 
C programming language
C programming languageC programming language
C programming languageAbin Rimal
 
Basics of C.ppt
Basics of C.pptBasics of C.ppt
Basics of C.pptTanuGohel
 
Basics of C.ppt
Basics of C.pptBasics of C.ppt
Basics of C.pptMEHALAS3
 
Basics of C (1).ppt
Basics of C (1).pptBasics of C (1).ppt
Basics of C (1).pptSteveIrwin25
 
C programming basic pdf.ppt
C programming basic pdf.pptC programming basic pdf.ppt
C programming basic pdf.pptKauserJahan6
 

Similar to Lecture 2 variables (20)

M.Florence Dayana / Basics of C Language
M.Florence Dayana / Basics of C LanguageM.Florence Dayana / Basics of C Language
M.Florence Dayana / Basics of C Language
 
c-programming
c-programmingc-programming
c-programming
 
#Code2 create c++ for beginners
#Code2 create  c++ for beginners #Code2 create  c++ for beginners
#Code2 create c++ for beginners
 
Esoft Metro Campus - Programming with C++
Esoft Metro Campus - Programming with C++Esoft Metro Campus - Programming with C++
Esoft Metro Campus - Programming with C++
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
C Language Part 1
C Language Part 1C Language Part 1
C Language Part 1
 
Basics of c++ Programming Language
Basics of c++ Programming LanguageBasics of c++ Programming Language
Basics of c++ Programming Language
 
2nd PUC Computer science chapter 5 review of c++
2nd PUC Computer science chapter 5   review of c++2nd PUC Computer science chapter 5   review of c++
2nd PUC Computer science chapter 5 review of c++
 
Basics of C.ppt
Basics of C.pptBasics of C.ppt
Basics of C.ppt
 
Java chapter 2
Java chapter 2Java chapter 2
Java chapter 2
 
C programming language
C programming languageC programming language
C programming language
 
Basics of C.ppt
Basics of C.pptBasics of C.ppt
Basics of C.ppt
 
Basics of C.ppt
Basics of C.pptBasics of C.ppt
Basics of C.ppt
 
Funa-C.ppt
Funa-C.pptFuna-C.ppt
Funa-C.ppt
 
Basics of C.ppt
Basics of C.pptBasics of C.ppt
Basics of C.ppt
 
Basics of C (1).ppt
Basics of C (1).pptBasics of C (1).ppt
Basics of C (1).ppt
 
Basics of C.ppt
Basics of C.pptBasics of C.ppt
Basics of C.ppt
 
C programming basic pdf.ppt
C programming basic pdf.pptC programming basic pdf.ppt
C programming basic pdf.ppt
 
Basics of C.ppt
Basics of C.pptBasics of C.ppt
Basics of C.ppt
 
Basics of C.ppt
Basics of C.pptBasics of C.ppt
Basics of C.ppt
 

Lecture 2 variables

  • 1. Intro to Programming with C++ / Programming Using C++ CSD 106 / ICT 120
  • 2. Comments • Used to document parts of the program • Intended for persons reading the source code of the program: – Indicate the purpose of the program – Describe the use of variables – Explain complex sections of code • Are ignored by the compiler
  • 3. Single-Line Comments Begin with // through to the end of line: float adjacent = 2.5;// adjacent in cm float opposite = 3.5; // opposite in cm float hypotenuse; // hypotenuse that will be calculated
  • 4. Multi-Line Comments • Begin with /*, end with */ • Can span multiple lines: /* this is a multi-line comment */ • Can begin and end on the same line: int volume; /* calculated volume */
  • 5. Data Type • Fundamental Data Type – Fundamental data types are basic types implemented directly by the language that represent the basic storage units supported natively by most systems. – C++ supports three simple data types • integer • floating point • character numeric
  • 6. Data Type • Integer – Is a whole number, either positive or negative – Does not include decimal points – Cannot be written with commas, dollar signs, or any symbols other than a leading + or -. – An integer variable is declared with the keyword int
  • 7. Data Type Data Type Size Range int 4bytes -2147483648 to 2147483647 unsigned int 4bytes 0 to 4294967295 signed int 4bytes -2147483648 to 2147483647 short int 2bytes -32768 to 32767 unsigned short int 2bytes 0 to +65,535 long int 4bytes -2,147,483,647 to 2,147,483,647 Unsigned long int 4 bytes 0 to 4,294,967,295 Integer
  • 8. Data Type • Floating-Point – Real or Floating-Point Numbers • Numbers that include decimal points • A floating point variable is declared with the keyword float or double and long double. long double uses more memory space than double. Likewise double occupies more space than float.
  • 10. Data Type • Character – character may hold any single symbol in the ASCII character set. – It contains letters of the alphabet, digits, space, punctuation mark, arithmetic symbol, or other special symbol. – Character value is always expressed in single quotes ‘’ – A character variable is declared with the keyword char
  • 11. The char Data Type • Used to hold characters or very small integer values • Usually 1 byte of memory • Numeric value of character from the character set is stored in memory: CODE: char letter; letter = 'C'; MEMORY: letter 67
  • 12. The Wider character Data Type • Usually more than 1 byte of memory • A wider character variable is declared with the keyword wchar_t
  • 13. The C++ string Class • Special data type supports working with strings • Include the string header file – #include <string> • Define a string variable type called a string object using the keyword string – For example: string address, occupation; • Can receive values with assignment operator: • Can be displayed via cout cout << address << " " << occupation;
  • 15. Variables • Variable names correspond to locations in the computer’s memory. • It must be defined before it can be used. • It include letters, numbers and underscores, but must begin with a letter (a-z or A-Z) or underscore(_). • No spaces or other special characters are allowed within a C++ variable name. • Examples of valid names  Gender  cumGPA  Total_Cost  _emailAddress
  • 16. Variable Names • A variable name should represent the purpose of the variable. For example: costOfItem The purpose of this variable is to hold the cost of item.
  • 17. Variables • Variable – Note: C++ is case-sensitive as such a variable name such as tax, Tax and TAX are different variable names – Good programming practice: use all lowercase letters for variable names or else capitalize only the first letter of each new word after the first word, eg firstSemesterGPA (camel case) – C++ keywords cannot be used as a variable name.
  • 18. Variables • Variable – Variable declaration is a C++ statement, all C++ statements must end with a semi-colon (;) – Explicitly stating the value of a variable is called assignment. • It is achieved with the assignment operator = – Assigning a value to a variable upon creation is often referred to as initializing the variable.
  • 19. Variables • Variable – Declaring, initializing, and assigning values to variables float tax_rate; float amount = 525.45; float tax_Amount; tax_rate = 0.15; tax_Amount = tax_rate * amount ;
  • 20. Variables • Variable – The const Qualifier • A variable that does not change in a program should be declared as a constant • The keyword used to declare a variable constant is const • It is ideal to use all uppercase letters for a constant name • General Syntax const dataType VARIABLE_NAME;
  • 21. Scope • The scope of a variable: the part of the program in which the variable can be accessed • A variable cannot be used before it is defined
  • 23. C++ Binary Arithmetic Operators • C++ provides Five (5) simple arithmetic operators for creating arithmetic expressions • Each of these arithmetic operators is a binary operator – Each takes two operands, one on each side of the operator
  • 25. C++ Binary Arithmetic Operators • Addition, subtraction, multiplication, or division of any two integers results in an integer. • If either or both of the operands in addition, subtraction, multiplication or division is a floating – point number, then the result is also a floating point number. • Division (/ ) • Integer division truncates remainder • Modulus (%) – Modulus operator returns remainder • 9 / 2 evaluates to 4 9 % 2 evaluates to 1
  • 26. C++ Binary Arithmetic Operators • Parentheses for Grouping Sub-expressions – Parentheses are used in C++ expressions to group sub-expressions • Same manner as in algebraic expressions – Example • b * ( d + e ) – Multiple b times the quantity d + e
  • 27. C++ Shortcut Arithmetic Operators • The following operators are valid shortcut arithmetic operators: += -= *= /= • The assignment operator (=) always appears second. • You must not insert a space between the operators
  • 28. C++ Shortcut Arithmetic Operators Assignment operator Sample expression Explanation Assigns Assume: int c = 3, d = 5, e = 4, f = 6, g = 12; += c += 7 c = c + 7 10 to c -= d -= 4 d = d - 4 1 to d *= e *= 5 e = e * 5 20 to e /= f /= 3 f = f / 3 2 to f %= g %= 9 g = g % 9 3 to g
  • 29. C++ Binary Arithmetic Operators • Rules of Operator Precedence Operator(s) Operation(s) Order of evaluation (precedence) ( ) Parentheses Evaluated first. If the parentheses are nested, the expression in the innermost pair is evaluated first. If there are several pairs of parentheses “on the same level” (i.e., not nested), they are evaluated left to right. * / % Multiplication Division Modulus Evaluated second. If there are several, they are evaluated left to right. + - Addition Subtraction Evaluated last. If there are several, they are evaluated left to right.
  • 30. C++ Binary Arithmetic Operators • Example: Rules of Operator Precedence
  • 31. C++ Unary Operators • Unary operators are those operators that require only one operand. • The prefix and postfix increment and decrement operators are examples of unary operators ++count count++ --num num-- Prefix increment Postfix increment Prefix decrement Postfix decrement
  • 32. C++ Unary Operators • Prefix operator, the mathematical operation takes place before the expression is evaluated. • Postfix operator, the mathematical operation takes place after the expression is evaluated. int num = 6; result = ++num; cout << result; // result is 7 cout << num; // num is 7 int num = 6; result = num++; cout << result; // result is 6 cout << num; // num is 7
  • 33. Mathematical Expressions • An expression is a programming statement that has a value. • An expression usually consists of an operator and its operands • Can create complex expressions using multiple mathematical operators • An expression can be a literal, a variable, or a mathematical combination of constants and variables • Can be used in assignment, cout, other statements: area = 2 * PI * radius; cout << "border is: " << 2*(l+w);
  • 34. Associativity of Operators • - (unary negation) associates right to left • *, /, %, +, - associate left to right • parentheses ( ) can be used to override the order of operations: 4 + 4 * 4 – 4 = (4 + 4) * 4 – 4 = 4 + 4 * (4 – 4) = (4 + 4) * (4 – 4) = ? ? ? ?
  • 35. Evaluating Boolean Expressions • Boolean Expression – Evaluates as true or false • C++ employs six relational binary operators. Standard algebraic equality or relational operator C++ equality or relational operator Sample C++ condition Meaning of C++ condition Relational operators  > x > y x is greater than y  < x < y x is less than y  >= x >= y x is greater than or equal to y  <= x <= y x is less than or equal to y Equality operators = == x == y x is equal to y ≠ != x != y x is not equal to y
  • 36. Tips A syntax error will occur if any of the operators ==, !=, >= and <= appears with spaces between its pair of symbols. Reversing the order of the pair of symbols in any of the operators !=, >= and <= (by writing them as =!, => and =<, respectively) is normally a syntax error. In some cases, writing != as =! will not be a syntax error, but almost certainly will be a logic error that has an effect at execution time
  • 37. Converting Algebraic expressions to Programming Statements • In algebra it is not always necessary to use an operator for multiplication. • C++, however, requires an operator for any mathematical operation. • In converting some algebraic expressions to C++, you may have to insert parentheses that do not appear in the algebraic expression
  • 38. Converting Algebraic expressions to Programming Statements
  • 39. Converting Algebraic expressions to Programming Statements
  • 40. Exponents in C++ • Standard library function pow() – Calculates an exponent – Example • xy = pow( x, y ) – Calculates the value of x raised to the yth power – Requires header file <cmath> • <cmath> – Contains function prototypes for math library functions

Editor's Notes

  1. Signed Range: the values can either be positive or negative values. Unsigned Range: the values would represent positive values and zero. Unsigned data types can only store nonnegative values. They can be used when you know your program will not encounter negative values. For example, variables that hold ages or weights would rarely hold numbers less than 0.
  2. Char variables hold only one character
  3. A = 65, B = 66
  4. A = 65, B = 66
  5. Size of a variable is the number of bytes of memory
  6. Keywords make up the core of the language and have specific purposes.
  7. The scope of a variable is the part of the program where the variable may be used.
  8. firstnumber = firstnumber + secondnumber; produces the same result as firstnumber += secondnumber;
  9. firstvalue = firstvalue + secondvalue; produces the same result as firstvalue += secondvalue;
  10. Rules of operator precedence Operators in parentheses evaluated first Nested/embedded parentheses Operators in innermost pair first Multiplication, division, modulus applied next Operators applied from left to right Addition, subtraction applied last Operators applied from left to right
  11. Increments operators increases variable by one Decrements operators decreases variable by one
  12. Prefix: Variable is changed, then the expression it is in is evaluated using the new value Postfix: Expression the variable is in executes using the old value, then the variable is changed
  13. An algebraic expression is a mathematical phrase that can contain ordinary numbers, variables (like x or y) and operators (like add,subtract,multiply, and divide)