### Lecture_05 - Electrical and Computer Engineering

```Recap……Last Time
[Variables, Data Types and Constants]
Variable Definition
• Variable Definition Syntax:
type variable_name;
• Note that:
•
•
•
•
You must declare a variable before using it
Variable declaration can be placed anywhere in the program
Readability Purpose: beginning of the main function.
type: Specifies two things
Input with cin
• The keyword cin represents a standard input stream.
The input stream represents data coming from the
keyboard.
• The >> is the extraction operator or get from
keyboard operator.
Circle : 03
Write a program that calculates and displays the area and the
circumference of a circle based on its radius entered from the
keyboard.
[Using Constants]
const qualifier
#define directive
const
Qualifier
• The const Qualifier specifies the value of the variable will
not change throughout the program and any attempt to do
so will generate an error.
const float PI = 3.14159;
Question: Why bother with using a variable when we can simply
place the literal constant in its place everywhere in the code?
#define
Directive
• Constants can be specified using the
preprocessor directive #define.
• This directive sets up an equivalence between
an identifier and a text phrase.
#define PI 3.14159
Operators, Expressions and
Statements
Yared Semu
Addis Ababa Institute of Technology
April 2012
Operators
• An operator is a symbol that takes one or more arguments
(operands) and operates on them to product a result.
• An Operand is something that an operator acts on.
• A unary operator requires one operand.
• A binary operator requires two operands.
Operators
• Five types of Operators
– Arithmetic Operators
– Assignment Operators
– Increment/ Decrement Operators
– Relational Operators
– Logical Operators
Arithmetic Operators
• Are binary Mathematical Operators
Note
• No exponentiation operator
• Single division operator
• Operators are overloaded to work with more than one type
of object (Work on both integer and floating point data
types)
Integer Division
• Integer division produces an integer result
– Truncates the result
• Examples:
– 3/2 evaluates to 1
– 4/6 evaluates to 0
– 10/3 evaluates to 3
Modulus
• Produces the remainder of the division
• Works only with integer variables
• Examples:
– 5 % 2 evaluates to 1
– 12 % 4 evaluates to 0
– 4 % 5 evaluates to 4
Arithmetic Assignment (Compound Assignment)
Operators
• It offers a way to shorten and clarify your code.
• Common Statement is C++:
» Total = Total + item;
• Condensed Approached:
>> Total += item;
Compounded Arithmetic Operators
• There are arithmetic assignment operators corresponding
to all arithmetic operations +=, -=, *=, /=, and %=.
Example: Predict the Output
#include <iostream>
using namespace std;
int main()
{
int ans = 27;
ans += 10;
cout<<ans<<“, ”;
ans -= 7;
cout<<ans<<“,”;
ans *= 2;
cout<<ans<<“,”;
ans %= 3;
cout<<ans<<“,” <<endl;
return 0;
}
Arithmetic Operators and Precedence
• Consider m*x + b which of the following is it equivalent
to:
>> (m*x) + b
>> m* (x + b)
Operator precedence tells how to evaluate expressions
Standard Precedence Order
• ()
Evaluates first, if nested innermost
done first.
• */%
Evaluated second. If there are several
then evaluate form left-to-right.
• +-
Evaluated third. If there are several, then
evaluate form left-to-right.
Example: Arithmetic Operator Precedence
Evaluate the Expression:
20 – 4/5 * 2 + 3 * 5 % 4
Step 1:
Step 2:
Step 3:
Step 4:
Step 5:
Step 6:
Step 7 :
20 – (4/5) * 2 + 3 * 5 % 4
20 – (0 * 2) + 3 * 5 % 4
20 - 0 + (3 * 5) % 4
20 - 0 + (15 % 4)
(20 - 0) + 3
20 + 3
23
Assignment Operator
• Assignment Operator =
– Assigns value on left to variable on right
– Binary Operator (two operands)
– Example:
•
•
•
•
•
int a = 5;
float b = 9.66;
char ch = ‘d’;
int m, n, p ;
m = n = p = 100;
Assignment Operator
• The part on the left hand side of the assignment operator
is known as lvalue
• The part on the right hand side of the assignment operator
is known as rvalue
Assignment(=) Operator
Increment & Decrement Operators
• Increment operator: increment variable by 1
• Decrement operator: decrement variable by 1
– Pre-increment: ++variable
– Post-increment: variable++
– Pre-decrement: --variable
– Post-decrement: variable--
Increment & Decrement Operators
• ++count; or count++; increments the value of
count by 1
• --count; or count--; decrements the value of
count by
• If x = 5; and y = ++x;
– After the second statement both x and y are 6
• If x = 5; and y = x++;
– After the second statement y is 5 and x is 6
Relational Operators
Operator
Name
Example
==
Equality
5 == 5
// gives 1
!=
Inequality
5 != 5
// gives 0
<
Less Than
5 < 5.5
// gives 1
<=
Less Than or Equal
5 <= 5
>
Greater Than
5 > 5.5
// gives 0
>=
Greater Than or Equal
6.3 >= 5
// gives 1
Relational operators
// gives 1
Logical Operators
• Like the relational operators, logical operators evaluate to
1 or 0.
Operator
Name
Example
!
Logical Negation
!(5 == 5)
// gives 0
&&
Logical And
5 < 6 && 6 < 6
// gives 1
||
Logical Or
5 < 6 || 6 < 5
// gives 1
Logical operators
• Example
–!20
–10 && 5
–10 || 5.5
–10 && 0
// gives 0
// gives 1
// gives 1
// gives 0
Short Circuit
• C++ only evaluates what is necessary from left to right to
come up with the combined relational result, ignoring the
rest
• E.g.
Expressions
• Combine literals, variables, and operators to form
expression.
• The expressions could be Simple Expressions or Complex
Expressions.
– Complex Expressions : Consists of Simple Expressions
Connected by Operator.
Examples
• Expressions can contain:
– a number literal,
– a variable,
– a function call,
– an operator between two
expressions (binary operator),
– an operator applied to one
expression (unary operator),
– expressions in parentheses.
3.14
count
sum(x, y)
a +b
-discount
(3.14-amplitude)
Statements
• Roughly Equivalent to sentences in natural languages
• Forms a complete unit of execution
– A complete direction instructing the computer to carry out some task.
• Terminating expression with a semicolon (;)
• Three Kind of Statements in C++:
– Expression Statements
– Declaration Statements
– Control Flow Statements
Type Conversion
• A value in any of the built-in types can be converted
– Called type cast
• Syntax
(<data – type>)value;
or
<data – type> (value);
• Example:
–
–
–
–
(int) 3.14
long (3.14)
(double) 2
(char) 122
code is 122
– (unsigned short) 3.14
// converts 3.14 to an int to give 3
// converts 3.14 to a long to give 3L
// converts 2 to a double to give 2.0
// converts 122 to a char whose
// gives 3 as an unsigned short
• Types are considered “higher” or “lower”, based roughly
on the order show below
Long double > double > float > long > int > short > char
• Some times the compiler does the type casting – implicit
type cast
• Example:
– >> double d = 1;
– >> int i = 10.5;
– >> i = i + d;
// d receives 1.0
// i receives 10
Using Library Functions
Mathematical Functions
The mathematical functions allow us to do mathematical operations.
These operations include:
• raising a number to a certain power,
• computing the square root of a number,
• computing the cosine of an angle, etc... .
These functions are defined in the header file math.h (or cmath in standard
C++).
Mathematical Functions …
sqrt is the name of the function that performs the square root operation. This
function takes one argument of type double and returns a result of type double.
Mathematical Functions …
The function that computes the power of two numbers is :
More examples of mathematical functions are:
Predict the output_02
#include <iostream>
using namespace std;
int main()
{
long x, y, z;
x = y = z = 4;
y += 2;
y -= 1;
z *= 3;
cout<< x <<“ ”<< y << “ ” << z <<endl;
return 0;
}
Predict the output _03
#include <iostream>
using namespace std;
#define WHO “Abebe”
#define DID
“ Won”
#define WHAT “the Marathon”
int main()
{
cout<<WHO <<“ ” << DID << “ ” << WHAT <<endl;
return 0;
}
Fix the Errors
#include <iostream>
using namespace std;
int main()
{
int number1, number2;
cout<<“Enter two numbers and I will multiply\n”
<<“them by 50 for you.\n”
cin>>number1,number2;
number1 *= 50;
number2 =* 50;
return 0;
cout<<number1<<“ ”<<number2;
}
Quiz_1: Predict the output
#include <iostream>
using namespace std;
int main()
{
int a, x=23;
a = x % 2;
cout<< x <<endl<< a<<endl;
return 0;
}
Quiz _2 : Implement an Pseudocode
Write a program that implements the following algorithm.
Start
Read the total hours the employee has worked, TotalHours
Read the hourly rate of pay for the employee, HourlyRate
GrossSalary = TotalHours * HourlyRate
Tax = GrossSalary * 0.1
NetSalary = GrossSalary - Tax
Display NetSalary
Stop
Quiz _3:Currency
[Write a program that will convert U.S. dollar amounts to
Japanese yen and to euros. The conversion factors to use are
1dollar = 108.5 yen and 1 dollar = 0.8218 euros]
```