Download - Variables, Data Types and Constants Yared Semu Addis Ababa Institute of Technology Mar 31, 2012.

Transcript
Page 1: Variables, Data Types and Constants Yared Semu Addis Ababa Institute of Technology Mar 31, 2012.

Variables, Data Types and Constants

Yared SemuAddis Ababa Institute of Technology

Mar 31, 2012

Page 2: Variables, Data Types and Constants Yared Semu Addis Ababa Institute of Technology Mar 31, 2012.

Last Time….

• Basic (C++) program Construction

• We greeted the world with our first C++ program

– “Hello World!”

Page 3: Variables, Data Types and Constants Yared Semu Addis Ababa Institute of Technology Mar 31, 2012.

1//A Simple C++ Program

#include <iostream>using namespace std;

int main(){

cout<<“Programming is great fun!”;return 0;

}

Comments

Directive

Namespace

Statement

Functions

Return Control to OS

Page 4: Variables, Data Types and Constants Yared Semu Addis Ababa Institute of Technology Mar 31, 2012.

Question

• Modify the previous program so that it outputs the following lines?

Hint: Use either endl (end current line manipulator ) or the escape sequence character(\n – new line)

Programming is great fun!I love to Program!

Page 5: Variables, Data Types and Constants Yared Semu Addis Ababa Institute of Technology Mar 31, 2012.

Escape Sequences

Page 6: Variables, Data Types and Constants Yared Semu Addis Ababa Institute of Technology Mar 31, 2012.

Basic Elements

• Five kind of tokens in C++–Comments–Keywords (Reserved words)– Identifiers–Literals–Operators

Page 7: Variables, Data Types and Constants Yared Semu Addis Ababa Institute of Technology Mar 31, 2012.

Comments

• Typical Uses– Identify program and who wrote it– Record when program was written– Add description of modifications– Explain programs to other programmers

Page 8: Variables, Data Types and Constants Yared Semu Addis Ababa Institute of Technology Mar 31, 2012.

Keywords

• Words with special meaning to the compiler

• Have a predefined meaning that cannot be changed

• All the reserved words are in lower-case letter

Page 9: Variables, Data Types and Constants Yared Semu Addis Ababa Institute of Technology Mar 31, 2012.

C++ Keyword set

Page 10: Variables, Data Types and Constants Yared Semu Addis Ababa Institute of Technology Mar 31, 2012.

Identifiers

• An identifier is a programmer-defined name that represents some element of a program.– Element : function name, variable (data

object)

• Your identifiers must not be any of the C++ keywords.– Keywords : have specific purposes.

Page 11: Variables, Data Types and Constants Yared Semu Addis Ababa Institute of Technology Mar 31, 2012.

Naming Conventions

1. You can use UPPERCASE and lowercase letters (a-z, A-Z) The digits from 0 to 9 The underscore symbol

2. You can’t use a C++ keyword as an identifier.

3. An identifier can’t begin with a number4. C++ is case sensitive

YARED Yared yAaReD

Page 12: Variables, Data Types and Constants Yared Semu Addis Ababa Institute of Technology Mar 31, 2012.

Exercise

• Which of the following identifiers are legal?home1min_Agecla!sY.S_4days-in-yearfirst_1forCout

Page 13: Variables, Data Types and Constants Yared Semu Addis Ababa Institute of Technology Mar 31, 2012.

Literals• Explicit (constant) value that is used by a

program

• Literals can be digits, letters or others that represent constant value to be stored in variables – Assigned to variables – Used in expressions – Passed to methods

• E.g.– Pi = 3.14; // Assigned to variables – C= a * 60; // Used in expressions

Page 14: Variables, Data Types and Constants Yared Semu Addis Ababa Institute of Technology Mar 31, 2012.

Variables

• Why do we need variables?

• Mental Exercise!

Page 15: Variables, Data Types and Constants Yared Semu Addis Ababa Institute of Technology Mar 31, 2012.

Variable Analogy

• Retain the number 5 in your memory• Memorize the number 2 at the same time• You have now stored two different values

in your memory• Add 1 to the 1st number• Now you should be retaining the

numbers 6 and 2 in your memory• Subtract this two values• You should obtain 4

Page 16: Variables, Data Types and Constants Yared Semu Addis Ababa Institute of Technology Mar 31, 2012.

If you were to do it on Paper …..

• The same process expressed in C++ instruction set looks like

a = 5;b = 2;c = a + 1;result = c – b;

• Variable = Portion of memory to which we can

store a value and from which we can later retrieve that value.

Page 17: Variables, Data Types and Constants Yared Semu Addis Ababa Institute of Technology Mar 31, 2012.

Variables

A Variable is a named storage location in the computer’s memory used for data storage.

A variable is a portion of the computer’s memory ,in which we can store a value and from which we can later retrieve that value

Variable DefinitionVariable InitializationVariable Assignment

Page 18: Variables, Data Types and Constants Yared Semu Addis Ababa Institute of Technology Mar 31, 2012.

• All variables have two important attributes:

– A type – Once defined, the type a C++ variable cannot be changed

– A value – can be changed by assigning a new value.

Example: int a = 5;

Page 19: Variables, Data Types and Constants Yared Semu Addis Ababa Institute of Technology Mar 31, 2012.

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 20: Variables, Data Types and Constants Yared Semu Addis Ababa Institute of Technology Mar 31, 2012.

More on Variable Definition

• A declaration (definition) of a variable is a statement that defines a variable.

• A comma separated list of one or more identifiers.

float base_price, last_selling_price, averageSellingPrice;

Page 21: Variables, Data Types and Constants Yared Semu Addis Ababa Institute of Technology Mar 31, 2012.

Variable Initialization

Let say we have a integer variable named number.

Definition:

Q. What is the content of the variable number?

To make use of the variable we have in our programs, we need to give them value.

>> Direct Initialization>> Copy Initialization

int number;

Page 22: Variables, Data Types and Constants Yared Semu Addis Ababa Institute of Technology Mar 31, 2012.

Variable Assignment

• The = sign is an operator that copies the value of its right into the variable named on its left.

int number = 5;or

number = 5;

Page 23: Variables, Data Types and Constants Yared Semu Addis Ababa Institute of Technology Mar 31, 2012.

Good Programming Practice

Place a space after each comma (,) to make programs more readable.

Page 24: Variables, Data Types and Constants Yared Semu Addis Ababa Institute of Technology Mar 31, 2012.

Good Programming Practice

Some programmers prefer to declare each variable on a separate line. This format allows for easy insertion of a descriptive comment next to each declaration.

Page 25: Variables, Data Types and Constants Yared Semu Addis Ababa Institute of Technology Mar 31, 2012.

Portability Tip

C++ allows identifiers of any length, but your C++ implementation may impose some restrictions on the length of identifiers. Use identifiers of 31 characters or fewer to ensure portability.

Page 26: Variables, Data Types and Constants Yared Semu Addis Ababa Institute of Technology Mar 31, 2012.

Good Programming Practice

Choosing meaningful identifiers helps make a program self-documenting—a person can understand the program simply by reading it rather than having to refer to manuals or comments.

Page 27: Variables, Data Types and Constants Yared Semu Addis Ababa Institute of Technology Mar 31, 2012.

Good Programming Practice

Avoid identifiers that begin with underscores and double underscores, because C++ compilers may use names like that for their own purposes internally. This will prevent names you choose from being confused with names the compilers choose.

Page 28: Variables, Data Types and Constants Yared Semu Addis Ababa Institute of Technology Mar 31, 2012.

Exercise. List all the variables and constants that appear in the following program.

// This program uses variables and constants#include <iostream>using namespace std;int main(){

int little;int big;little = 2;big = 2000;cout << "The little number is " << little << endl;cout << "The big number is " << big << endl;return 0;

}

Page 29: Variables, Data Types and Constants Yared Semu Addis Ababa Institute of Technology Mar 31, 2012.

Data Types• Broadly C++ provides two data types

Numeric Integral Numbers

short int long

Fractional Numbers float double

Character or sequence of characters char

Boolean Values bool

Page 30: Variables, Data Types and Constants Yared Semu Addis Ababa Institute of Technology Mar 31, 2012.

Integer Data Types, Sizes and Ranges

Page 31: Variables, Data Types and Constants Yared Semu Addis Ababa Institute of Technology Mar 31, 2012.

Type: int

• Represent integers or whole numbers

– Signed – (Positive and negative Numbers)

– Unsigned – (Only Postive)– Short – Long

Page 32: Variables, Data Types and Constants Yared Semu Addis Ababa Institute of Technology Mar 31, 2012.

Type: double, float

• Used to represent real numbers• Avoid leading zeros, trailing zeros are

ignored

Page 33: Variables, Data Types and Constants Yared Semu Addis Ababa Institute of Technology Mar 31, 2012.

Type: char

• Used to represent character data– A single character which includes a

space– 1 byte , enough to hold 256 values–Must be enclosed in single quotes E.g ‘d’

Page 34: Variables, Data Types and Constants Yared Semu Addis Ababa Institute of Technology Mar 31, 2012.

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 35: Variables, Data Types and Constants Yared Semu Addis Ababa Institute of Technology Mar 31, 2012.

Exercise

Write a program that accepts a character from the user and echo it to the screen.

[Demo]

Page 36: Variables, Data Types and Constants Yared Semu Addis Ababa Institute of Technology Mar 31, 2012.

Area of a Rectangle//Calculates the area of a rectangle

int main(){

int length, width;

cout<<“Enter the length of the rectangle>>”;cin>>length;cout<<“Enter the width of the rectangle>>”;cin>>width;

int area = length * width;

cout<<“Area: ”<<area<<endl;

return 0;

}

Output Window

Enter the length of the rectangle: 10Enter the width of the rectangle: 20Area: 200

Page 37: Variables, Data Types and Constants Yared Semu Addis Ababa Institute of Technology Mar 31, 2012.

Working Examples: Temperature Conversion

//Temperature Conversion Programint main(){

int ftemp; //Temprature in Fahrenheit

cout<<“Enter the temperature in Fahrenheit: ”;

int ctemp = (ftemp-32)*5/9;

cout<<“Equivalent in Celsius: ”<<ctemp<<endl;

return 0;

}

Output WindowEnter the temperature in Fahrenheit:23Equivalent in Celsius: 4

Page 38: Variables, Data Types and Constants Yared Semu Addis Ababa Institute of Technology Mar 31, 2012.

Predict the output_01

#include <iostream>using namespace std;

int main(){

cout<<“I am the incredible”; cout<<“computing machine”; cout<<“\n and I will \namze \n”; cout<<“you.\n”; return 0;}

Page 39: Variables, Data Types and Constants Yared Semu Addis Ababa Institute of Technology Mar 31, 2012.

Addition: 01

• Write a program that accept two numbers from the user and displays the output back to the user.

– Hint: First write the simple algorithm using a flowchart.

Page 40: Variables, Data Types and Constants Yared Semu Addis Ababa Institute of Technology Mar 31, 2012.

Swap: 02

Write a program that swaps the values of two variables and displays their former and current values. The values of the variables are to be entered from the keyboard.

Page 41: Variables, Data Types and Constants Yared Semu Addis Ababa Institute of Technology Mar 31, 2012.

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 42: Variables, Data Types and Constants Yared Semu Addis Ababa Institute of Technology Mar 31, 2012.

Implement an Pseudocode: 04

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