Thứ Ba, 21 tháng 1, 2014

C++ Basics 02

Slide 2- 5
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Display 2.1
Variables and Assignments

Variables are like small blackboards

We can write a number on them

We can change the number

We can erase the number

C++ variables are names for memory locations

We can write a value in them

We can change the value stored there

We cannot erase the memory location

Some value is always there
Slide 2- 6
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Identifiers

Variables names are called identifiers

Choosing variable names

Use meaningful names that represent data to
be stored

First character must be

a letter

the underscore character

Remaining characters must be

letters

numbers

underscore character
Slide 2- 7
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Keywords

Keywords (also called reserved words)

Are used by the C++ language

Must be used as they are defined in
the programming language

Cannot be used as identifiers
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Declaring Variables (Part 1)

Before use, variables must be declared

Tells the compiler the type of data to store
Examples: int number_of_bars;
double one_weight, total_weight;

int is an abbreviation for integer.

could store 3, 102, 3211, -456, etc.

number_of_bars is of type integer

double represents numbers with a fractional
component

could store 1.34, 4.0, -345.6, etc.

one_weight and total_weight are both of type double
Slide 2- 9
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Declaring Variables (Part 2)

Immediately prior to use
int main()
{

int sum;
sum = score1 + score 2;

return 0;
}

At the beginning
int main()
{
int sum;

sum = score1 +
score2;

return 0;
}
Two locations for variable declarations
Slide 2- 10
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Declaring Variables (Part 3)

Declaration syntax:

Type_name Variable_1 , Variable_2, . . . ;

Declaration Examples:

double average, m_score, total_score;

double moon_distance;

int age, num_students;

int cars_waiting;
Slide 2- 11
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Assignment Statements

An assignment statement changes the value of a variable

total_weight = one_weight + number_of_bars;

total_weight is set to the sum one_weight + number_of_bars

Assignment statements end with a semi-colon

The single variable to be changed is always on the left
of the assignment operator ‘=‘

On the right of the assignment operator can be

Constants age = 21;

Variables my_cost = your_cost;

Expressions circumference = diameter * 3.14159;
Slide 2- 12
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Assignment Statements and Algebra

The ‘=‘ operator in C++ is not an equal sign

The following statement cannot be true in
algebra

number_of_bars = number_of_bars + 3;

In C++ it means the new value of
number_of_bars
is the previous value of number_of_bars plus 3
Slide 2- 13
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Initializing Variables

Declaring a variable does not give it a value

Giving a variable its first value is initializing the variable

Variables are initialized in assignment statements
double mpg; // declare the variable
mpg = 26.3; // initialize the variable

Declaration and initialization can be combined
using two methods

Method 1
double mpg = 26.3, area = 0.0 , volume;

Method 2
double mpg(26.3), area(0.0), volume;
Slide 2- 14
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Section 2.1 Conclusion

Can you

Declare and initialize two integers variables to zero?
The variables are named feet and inches.

Declare and initialize two variables, one int and one
double?
Both should be initialized to the appropriate form of 5.

Give good variable names for identifiers to store

the speed of an automobile?

an hourly pay rate?

the highest score on an exam?

Xem chi tiết: C++ Basics 02


Không có nhận xét nào:

Đăng nhận xét