Download presentation
Presentation is loading. Please wait.
Published byConstance Knight Modified over 9 years ago
1
Recap……Last Time [Variables, Data Types and Constants]
2
Variable Definition Variable Definition Syntax: 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 type variable_name;
3
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.
4
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
5
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?
6
#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
7
Operators, Expressions and Statements Yared Semu Addis Ababa Institute of Technology April 2012
8
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.
9
Operators Five types of Operators –Arithmetic Operators –Assignment Operators –Increment/ Decrement Operators –Relational Operators –Logical Operators
10
Arithmetic Operators Are binary Mathematical Operators
11
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)
12
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
13
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
14
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;
15
Compounded Arithmetic Operators There are arithmetic assignment operators corresponding to all arithmetic operations +=, -=, *=, /=, and %=.
16
Example: Predict the Output #include 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; }
17
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
18
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.
19
Example: Arithmetic Operator Precedence Evaluate the Expression: 20 – 4/5 * 2 + 3 * 5 % 4 Step 1: 20 – (4/5) * 2 + 3 * 5 % 4 Step 2:20 – (0 * 2) + 3 * 5 % 4 Step 3: 20 - 0 + (3 * 5) % 4 Step 4: 20 - 0 + (15 % 4) Step 5: (20 - 0) + 3 Step 6: 20 + 3 Step 7 : 23
20
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;
21
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
22
Assignment(=) Operator
23
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--
24
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
25
Relational Operators Operator NameExample ==Equality5 == 5// gives 1 !=Inequality5 != 5// gives 0 <Less Than5 < 5.5// gives 1 <=Less Than or Equal5 <= 5// gives 1 >Greater Than5 > 5.5// gives 0 >=Greater Than or Equal6.3 >= 5// gives 1 Relational operators
26
Logical Operators Like the relational operators, logical operators evaluate to 1 or 0. OperatorNameExample ! Logical Negation !(5 == 5)// gives 0 && Logical And 5 < 6 && 6 < 6// gives 1 || Logical Or 5 < 6 || 6 < 5// gives 1 Logical operators
27
Example –!20// gives 0 –10 && 5// gives 1 –10 || 5.5 // gives 1 –10 && 0 // gives 0
28
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.
29
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.
30
Examples Expressions can contain: – a number literal, 3.14 – a variable, count – a function call, sum(x, y) – an operator between two expressions (binary operator), a +b – an operator applied to one expression (unary operator), -discount – expressions in parentheses. (3.14-amplitude)
31
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
32
Type Conversion A value in any of the built-in types can be converted – Called type cast Syntax ( )value; or (value); Example: –(int) 3.14// converts 3.14 to an int to give 3 –long (3.14)// converts 3.14 to a long to give 3L –(double) 2// converts 2 to a double to give 2.0 –(char) 122// converts 122 to a char whose code is 122 –(unsigned short) 3.14// gives 3 as an unsigned short
33
Types are considered “higher” or “lower”, based roughly on the order show below Long double > double > float > long > int > short > char
34
Some times the compiler does the type casting – implicit type cast Example: –>> double d = 1; // d receives 1.0 –>> int i = 10.5;// i receives 10 –>> i = i + d;
35
Using Library Functions
36
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++).
37
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.
38
Mathematical Functions … The function that computes the power of two numbers is : More examples of mathematical functions are:
39
Predict the output_02 #include 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; }
40
Predict the output _03 #include using namespace std; #define WHO “Abebe” #define DID “ Won” #define WHAT “the Marathon” int main() { cout<<WHO <<“ ”<< DID << “ ” << WHAT <<endl; return 0; }
41
Fix the Errors #include 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; }
42
Quiz_1: Predict the output #include using namespace std; int main() { int a, x=23; a = x % 2; cout<< x <<endl<< a<<endl; return 0; }
43
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
44
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]
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.