Recap……Last Time [Variables, Data Types and Constants]

44
Recap……Last Time [Variables, Data Types and Constants]

description

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.

Transcript of Recap……Last Time [Variables, Data Types and Constants]

Page 1: Recap……Last Time [Variables, Data Types and Constants]

Recap……Last Time

[Variables, Data Types and Constants]

Page 2: Recap……Last Time [Variables, Data Types and Constants]

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;

Page 3: Recap……Last Time [Variables, Data Types and Constants]

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.

Page 4: Recap……Last Time [Variables, Data Types and Constants]

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

Page 5: Recap……Last Time [Variables, Data Types and Constants]

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?

Page 6: Recap……Last Time [Variables, Data Types and Constants]

#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

Page 7: Recap……Last Time [Variables, Data Types and Constants]

Operators, Expressions and Statements

Yared SemuAddis Ababa Institute of Technology

April 2012

Page 8: Recap……Last Time [Variables, Data Types and Constants]

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.

Page 9: Recap……Last Time [Variables, Data Types and Constants]

Operators

• Five types of Operators

–Arithmetic Operators–Assignment Operators– Increment/ Decrement Operators–Relational Operators–Logical Operators

Page 10: Recap……Last Time [Variables, Data Types and Constants]

Arithmetic Operators

• Are binary Mathematical Operators

Page 11: Recap……Last Time [Variables, Data Types and Constants]

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)

Page 12: Recap……Last Time [Variables, Data Types and Constants]

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

Page 13: Recap……Last Time [Variables, Data Types and Constants]

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

Page 14: Recap……Last Time [Variables, Data Types and Constants]

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;

Page 15: Recap……Last Time [Variables, Data Types and Constants]

Compounded Arithmetic Operators

• There are arithmetic assignment operators corresponding to all arithmetic operations +=, -=, *=, /=, and %=.

Page 16: Recap……Last Time [Variables, Data Types and Constants]

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;}

Page 17: Recap……Last Time [Variables, Data Types and Constants]

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

Page 18: Recap……Last Time [Variables, Data Types and Constants]

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.

Page 19: Recap……Last Time [Variables, Data Types and Constants]

Example: Arithmetic Operator Precedence

Evaluate the Expression:20 – 4/5 * 2 + 3 * 5 % 4

Step 1: 20 – (4/5) * 2 + 3 * 5 % 4Step 2: 20 – (0 * 2) + 3 * 5 % 4Step 3: 20 - 0 + (3 * 5) % 4 Step 4: 20 - 0 + (15 % 4)Step 5: (20 - 0) + 3Step 6: 20 + 3Step 7 : 23

Page 20: Recap……Last Time [Variables, Data Types and Constants]

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;

Page 21: Recap……Last Time [Variables, Data Types and Constants]

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

Page 22: Recap……Last Time [Variables, Data Types and Constants]

Assignment(=) Operator

Page 23: Recap……Last Time [Variables, Data Types and Constants]

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--

Page 24: Recap……Last Time [Variables, Data Types and Constants]

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

Page 25: Recap……Last Time [Variables, Data Types and Constants]

Relational OperatorsOperator 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 // gives 1

> Greater Than 5 > 5.5 // gives 0

>= Greater Than or Equal 6.3 >= 5 // gives 1

Relational operators

Page 26: Recap……Last Time [Variables, Data Types and Constants]

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

Page 27: Recap……Last Time [Variables, Data Types and Constants]

• Example –!20 // gives 0–10 && 5 // gives 1–10 || 5.5 // gives 1–10 && 0 // gives 0

Page 28: Recap……Last Time [Variables, Data Types and Constants]

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.

Page 29: Recap……Last Time [Variables, Data Types and Constants]

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.

Page 30: Recap……Last Time [Variables, Data Types and Constants]

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)

Page 31: Recap……Last Time [Variables, Data Types and Constants]

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

Page 32: Recap……Last Time [Variables, Data Types and Constants]

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 // 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

Page 33: Recap……Last Time [Variables, Data Types and Constants]

• Types are considered “higher” or “lower”, based roughly on the order show below

Long double > double > float > long > int > short > char

Page 34: Recap……Last Time [Variables, Data Types and Constants]

• 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;

Page 35: Recap……Last Time [Variables, Data Types and Constants]

Using Library Functions

Page 36: Recap……Last Time [Variables, Data Types and Constants]

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++).

Page 37: Recap……Last Time [Variables, Data Types and Constants]

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.

Page 38: Recap……Last Time [Variables, Data Types and Constants]

Mathematical Functions … The function that computes the power of two numbers is :

More examples of mathematical functions are:

Page 39: Recap……Last Time [Variables, Data Types and Constants]

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;}

Page 40: Recap……Last Time [Variables, Data Types and Constants]

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;}

Page 41: Recap……Last Time [Variables, Data Types and Constants]

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;}

Page 42: Recap……Last Time [Variables, Data Types and Constants]

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;}

Page 43: Recap……Last Time [Variables, Data Types and Constants]

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

Page 44: Recap……Last Time [Variables, Data Types and Constants]

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]