Outline
Operators and Statements
• Assignation (=).
• Arithmetic operators
• Precedence Rules of Arithmetic Operators
• Compound assignation operators
• Increment and decrement Operators
• Relational operators
• Logic operators
• Type Conversion
• Converting Algebraic Expressions to C++
• Library Function
Assignation(=)
• The assignation operator serves to assign
a value to a variable.
• a = 5; assigns the integer value 5 to
variable a. The part at the left of the
=operator is known as lvalue (left value)
and the right one as rvalue (right value).
lvalue must always be a variable whereas
the right side can be either a constant, a
variable, the result of an operation or any
combination of them.
Arithmetic operators (+, -, *, /, %)
• Addition (+)
• Subtraction (-)
• Multiplication (*)
• Division (/)
• Module (%)
• Module, specified with the percentage sign
(%). Module is the operation that gives the
rest of a division of two integer values. For
example, if we write a = 11 % 3; the
variable a will contain 2 as result since 2 is
the rest from dividing 11 between 3.
Increment and decrement Operators
• the increase operator (++) and the decrease
operator (--). They increase or reduce by 1 the
value stored in a variable. They are equivalent
to +=1 and to -=1, respectively.
• Thus:
• a++;
• a+=1;
• a=a+1;
• are all equivalent in its functionality: the three
increase by 1 the value of a.
•
Example
• Example 1 Example
• B=3 B=3
• A=++B A=B++
• //A=4,B=4 //A=3,B=4
Exercises
do it the above example in Decrement(--)
Relational operators (==,!=, >, <, >=, <= )
• = = Equal
• ! = Different
• > Greater than
• < Less than
• > = Greater than or Equal to
• < = Less than or Equal to
Logic operators ( !, &&, || ).
• Operator! is equivalent to
Boolean operation NOT, it has
only one operand, located at its
right, and the only thing that it
does is to invert the value of it,
producing false if its operand is
true and true if its operand is
false.
Conditional operator ( ? ).
• condition ? result1 : result2
• if condition is true the expression will
return result1, if not it will return result2.
• Example:-
• 7= =5? 4 : 3 returns 3 since 7 is not equal to 5
• 7= =5+2 ? 4:3 returns 4 since 7 is equal to 5
+2
• 5> 3 ? a: b returns a, since 5 is greater than 3
Other Operators
• The extraction operator (>>)
extracts an item from the input
stream
• The insertion operator (<<)
insert an item to output stream
Type Conversion
• Type casting operators allows you to convert
a datum of a given type to another.
• static_cast=(dataType)Value;
• This is called type casting or type conversion.
• Example:
• int i;
• float f = 3.14;
• i = (int) f;
Converting Algebraic Expressions to C++
• One of the challenges of learning a new
computer language is the task of changing
algebraic expressions to their equivalent
computer instructions.
• Example:
4y (3-2) y+7
• How would this algebraic expression be
implemented in C++?
• 4 * y * (3-2) * y + 7
Library Function
The C++ standard library provides a rich
collection of functions for
• Performing common mathematical
calculations
• String manipulation
• Character manipulation
• Input/output
• Error checking and so on
Library Functions…
Some of the library functions commonly
used are:
• iostream.h
• math.h
• iomanip.h
• string.h
• conio.h...............etc
Precedence Rules of Arithmetic Operators
– Precedence Rules of Arithmetic Operators
• Anything grouped in parentheses is top priority
• Unary negation (example: –8)
• Multiplication, Division and Modulus * / %
• Addition and Subtraction + –
• Example: (8 * 4/2 + 9 - 4/2 + 6 * (4+3))
• (8 * 4/2 + 9 - 4/2 + 6 * 7)
• (32 /2 + 9 - 4/2 + 6 * 7)
• (16 + 9 - 4/2 + 6 * 7)
• (16 + 9 - 2 + 6 * 7)
• (16 + 9 - 2 + 42)
• (25 - 2 + 42)
• (23+ 42) = 65
Example 1 – adding 2 numbers
• Abebe: Hey Bekele, I just learned how to add two numbers
together.
• Bekele: Cool!
• Abebe : Give me the first number.
• Bekele: 2.
• Abebe : Ok, and give me the second number.
• Bekele: 5.
• Abebe : Ok, here's the answer: 2 + 5 = 7.
• Bekele: Wow! You are amazing!
• after Bekele says “2”, Abebe has to keep this
number in his mind.
• after Bekele says “5”, Abebe also needs to keep
this number in his mind.
2 5 7
First number: Second number: Sum:
Compound assignation operators
• (+=, -=, *=, /=, %=, >=, <=, &=, ^=, |=)
• Is d combination of athematic or logical
operate or both
• Example:
• value += increase; is equivalent to value =
value + increase; a -= 5; is equivalent to
• a = a - 5; a /= b; is equivalent to a = a / b; price
*= units + 1; is equivalent to
• price = price * (units + 1);
The Corresponding C++ Program
#include <iostream.h>
int main()
{
int first, second, sum;
cout << "Abebe: Hey Bekele, I just learned how to add”
<< “ two numbers together."<< endl;
cout << "Bekele: Cool!" <<endl;
cout << "Abebe: Give me the first number."<< endl;
cout << "Bekele: ";
cin >> first;
cout << "Abebe: Give me the second number."<< endl;
cout << "Bekele: ";
cin >> second;
sum = first + second;
cout << "Abebe: OK, here is the answer:";
cout << sum << endl;
cout << "Bekele: Wow! You are amazing!" << endl;
return 0;
}
Quiz 1
1._____________is a data type that holds logical data (true/false)
2.What is % operator and its use
3.the use of cin and cout in C++
4.What statement would you use to print the phrase “Hello, world “and then
start a new line?
5.What statement would you use to create an integer variable with the name
x and assign value 780?
6.The <<“n” is a special character that is used for ___________________
7.What is the final value (in C++) of the following expression?
(5 - 30 / 2 * 3 + (9 + 2 * 7) – 5*4)
8. How would the following expression be written in C++?
9y(7X4)+ √8
9. ______________is the preprocessor directive that must be included for
sqrt()and pow() to be used in a C++ program
10. What is the use of the following preprocessor directive?
#include <iostream.h>

C++ Programming Basics.pptx

  • 2.
    Outline Operators and Statements •Assignation (=). • Arithmetic operators • Precedence Rules of Arithmetic Operators • Compound assignation operators • Increment and decrement Operators • Relational operators • Logic operators • Type Conversion • Converting Algebraic Expressions to C++ • Library Function
  • 3.
    Assignation(=) • The assignationoperator serves to assign a value to a variable. • a = 5; assigns the integer value 5 to variable a. The part at the left of the =operator is known as lvalue (left value) and the right one as rvalue (right value). lvalue must always be a variable whereas the right side can be either a constant, a variable, the result of an operation or any combination of them.
  • 4.
    Arithmetic operators (+,-, *, /, %) • Addition (+) • Subtraction (-) • Multiplication (*) • Division (/) • Module (%) • Module, specified with the percentage sign (%). Module is the operation that gives the rest of a division of two integer values. For example, if we write a = 11 % 3; the variable a will contain 2 as result since 2 is the rest from dividing 11 between 3.
  • 5.
    Increment and decrementOperators • the increase operator (++) and the decrease operator (--). They increase or reduce by 1 the value stored in a variable. They are equivalent to +=1 and to -=1, respectively. • Thus: • a++; • a+=1; • a=a+1; • are all equivalent in its functionality: the three increase by 1 the value of a. •
  • 6.
    Example • Example 1Example • B=3 B=3 • A=++B A=B++ • //A=4,B=4 //A=3,B=4 Exercises do it the above example in Decrement(--)
  • 7.
    Relational operators (==,!=,>, <, >=, <= ) • = = Equal • ! = Different • > Greater than • < Less than • > = Greater than or Equal to • < = Less than or Equal to
  • 8.
    Logic operators (!, &&, || ). • Operator! is equivalent to Boolean operation NOT, it has only one operand, located at its right, and the only thing that it does is to invert the value of it, producing false if its operand is true and true if its operand is false.
  • 9.
    Conditional operator (? ). • condition ? result1 : result2 • if condition is true the expression will return result1, if not it will return result2. • Example:- • 7= =5? 4 : 3 returns 3 since 7 is not equal to 5 • 7= =5+2 ? 4:3 returns 4 since 7 is equal to 5 +2 • 5> 3 ? a: b returns a, since 5 is greater than 3
  • 10.
    Other Operators • Theextraction operator (>>) extracts an item from the input stream • The insertion operator (<<) insert an item to output stream
  • 11.
    Type Conversion • Typecasting operators allows you to convert a datum of a given type to another. • static_cast=(dataType)Value; • This is called type casting or type conversion. • Example: • int i; • float f = 3.14; • i = (int) f;
  • 12.
    Converting Algebraic Expressionsto C++ • One of the challenges of learning a new computer language is the task of changing algebraic expressions to their equivalent computer instructions. • Example: 4y (3-2) y+7 • How would this algebraic expression be implemented in C++? • 4 * y * (3-2) * y + 7
  • 13.
    Library Function The C++standard library provides a rich collection of functions for • Performing common mathematical calculations • String manipulation • Character manipulation • Input/output • Error checking and so on
  • 14.
    Library Functions… Some ofthe library functions commonly used are: • iostream.h • math.h • iomanip.h • string.h • conio.h...............etc
  • 15.
    Precedence Rules ofArithmetic Operators – Precedence Rules of Arithmetic Operators • Anything grouped in parentheses is top priority • Unary negation (example: –8) • Multiplication, Division and Modulus * / % • Addition and Subtraction + – • Example: (8 * 4/2 + 9 - 4/2 + 6 * (4+3)) • (8 * 4/2 + 9 - 4/2 + 6 * 7) • (32 /2 + 9 - 4/2 + 6 * 7) • (16 + 9 - 4/2 + 6 * 7) • (16 + 9 - 2 + 6 * 7) • (16 + 9 - 2 + 42) • (25 - 2 + 42) • (23+ 42) = 65
  • 16.
    Example 1 –adding 2 numbers • Abebe: Hey Bekele, I just learned how to add two numbers together. • Bekele: Cool! • Abebe : Give me the first number. • Bekele: 2. • Abebe : Ok, and give me the second number. • Bekele: 5. • Abebe : Ok, here's the answer: 2 + 5 = 7. • Bekele: Wow! You are amazing! • after Bekele says “2”, Abebe has to keep this number in his mind. • after Bekele says “5”, Abebe also needs to keep this number in his mind. 2 5 7 First number: Second number: Sum:
  • 17.
    Compound assignation operators •(+=, -=, *=, /=, %=, >=, <=, &=, ^=, |=) • Is d combination of athematic or logical operate or both • Example: • value += increase; is equivalent to value = value + increase; a -= 5; is equivalent to • a = a - 5; a /= b; is equivalent to a = a / b; price *= units + 1; is equivalent to • price = price * (units + 1);
  • 18.
    The Corresponding C++Program #include <iostream.h> int main() { int first, second, sum; cout << "Abebe: Hey Bekele, I just learned how to add” << “ two numbers together."<< endl; cout << "Bekele: Cool!" <<endl; cout << "Abebe: Give me the first number."<< endl; cout << "Bekele: "; cin >> first; cout << "Abebe: Give me the second number."<< endl; cout << "Bekele: "; cin >> second; sum = first + second; cout << "Abebe: OK, here is the answer:"; cout << sum << endl; cout << "Bekele: Wow! You are amazing!" << endl; return 0; }
  • 19.
    Quiz 1 1._____________is adata type that holds logical data (true/false) 2.What is % operator and its use 3.the use of cin and cout in C++ 4.What statement would you use to print the phrase “Hello, world “and then start a new line? 5.What statement would you use to create an integer variable with the name x and assign value 780? 6.The <<“n” is a special character that is used for ___________________ 7.What is the final value (in C++) of the following expression? (5 - 30 / 2 * 3 + (9 + 2 * 7) – 5*4) 8. How would the following expression be written in C++? 9y(7X4)+ √8 9. ______________is the preprocessor directive that must be included for sqrt()and pow() to be used in a C++ program 10. What is the use of the following preprocessor directive? #include <iostream.h>