Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 19

Programming Fundamentals

Lecture 6

Copyright © 2013 The McGraw-Hill Companies, Inc.


Permission required for reproduction or display. 1
Variables and Declaration Statements

• In high-level languages such as C++,


symbolic names, called variables,
are used in place of memory
addresses.

2
Variables

• Name assigned to refer to computer storage locations.

• The term “variable” is used because the value stored in the


memory locations assigned to the variable can change or vary.

• For each name the programmer uses, the computer keeps


track of the memory address corresponding to that name.

3
Variables

Rules:
• The variable name must begin with a letter or
underscore (_) and can contain only letters,
underscores, or digits. It can’t contain blank
spaces, commas, or special symbols, such as
( ), & , $ # . ! \ ?.

• A variable name can’t be a keyword (see Table


2.1).

• A variable name can’t consist of more than


1024 characters.
Copyright © 2013 The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4
Assignment Statements

C++ statements:

num1 = 45; //assignment statement


num2 = 12;
total = num1 + num2;

Each of these statements is called an


assignment statement.

Important:
Assignment statement has one equal to
sign (=) and one variable name
immediately to the left of = 5
Declaration Statements

• To name a variable and specify the data type that can be stored in it
dataType variableName;

Examples :
int variableName;
int sum;
long datenum;
long int datenum;
float firstnum; (single precision number)
double secnum; (double precision number)
Copyright © 2013 The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 6
Rules for declaration statements

• a variable must always be declared before using it,


• Declaration statements must end with a semicolon.

7
Multiple Declarations

• Variables of the same data type can always be grouped together and declared by
using a single declaration statement, which has this common form:
dataType variableList;
• For example, the four separate declarations used in Program 2.7
double grade1;
double grade2;
double total;
double average;

double grade1, grade2, total, average;

char ch; char key;


//We can write above declaration statement as: 8
When a declaration statement is used to
store a value in a variable, the variable is
said to be initialized.

Initializatio • int num1 = 15;


n • double grade1 = 87.0;
• double grade2 = 93.5;
• double total;
• int num2=100, num3=200;

• How many are initialized and how many


declared ?

9
Example

We can also write:


double grade1=85.5, grade2=97.0, total, average;
10
Example

#include<iostream>
using namespace std;

int main()
{
double grade1 = 85.5, grade2 = 97.0, total, average;

total = grade1 + grade2;


average = total/2.0; // divide the total by 2.0
cout << "The average grade is" << average << endl;
return 0;
}
Copyright © 2013 The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 11
Assignment Operations

• variable = expression;

• length = 25;
• width = 17.5;

12
Examples using operators

slope = 3.7;
slope = 6.28; //

sum = 3 + 7;
diff = 15 - 6;
product = .05 * 14.6;
tally = count + 1;
newtotal = 18.3 + total;
taxes = .06 * amount;
totalWeight = factor * weight;
average = sum / items;
slope = (y2 - y1) / (x2 - x1);
Copyright © 2013 The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 13
Example

amount + 1892 = 1000 + 10 * 5; INVALID

14
Assignment Expression

• assignment operator
The = symbol

• assignment expression.
expression using this operator e.g. interest = principal * rate

15
Multiple assignments

a = b = c = 25;
This statement is equivalent to
a= (b=(c=25))

This statement assigns the value 25 to the three variables,


equivalent to the following order:
c = 25;
b = 25;
a = 25;

Copyright © 2013 The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 16
Coercion

• int temp;
• temp = 25.89;

• What will be saved in


temp ?

Value of a?

17
Casts

• C++ provides for explicit user-specified


type conversions.

• The operator used to force converting a


value to another type is the cast operator.

dataType (expression)
• The dataType is the data type to which the
expression in parentheses is converted. For
example, the following expression
int (a * b)
• converts the value of the expression a * b
to an integer value
Runtime casts & compile
time casts
• static_cast<data-type> (expression)

• For example, the runtime cast static_cast<int> (a *


b) is equivalent to the compiletime cast int (a* b).

Run this code:


#include<iostream>
using namespace std;

int main()
{
float a=2.7,b=3.2; cout<<a*b<<endl;
cout<<int(a*b)<<endl;
cout<<static_cast<int>(a*b)<<endl;
cout<< (int(a)*int(b))<<endl;
return 0; } 19

You might also like