c++ expressions - lecture 5 sections 2.6, 2.13, 2people.hsc.edu/faculty-staff/robbk/coms261/lectures...

23
C++ Expressions Lecture 5 Sections 2.6, 2.13, 2.15 Robb T. Koether Hampden-Sydney College Wed, Sep 5, 2018 Robb T. Koether (Hampden-Sydney College) C++ Expressions Wed, Sep 5, 2018 1 / 23

Upload: others

Post on 02-Aug-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: C++ Expressions - Lecture 5 Sections 2.6, 2.13, 2people.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2018/Lecture … · These five operators are all left associative. Unary operators

C++ ExpressionsLecture 5

Sections 2.6, 2.13, 2.15

Robb T. Koether

Hampden-Sydney College

Wed, Sep 5, 2018

Robb T. Koether (Hampden-Sydney College) C++ Expressions Wed, Sep 5, 2018 1 / 23

Page 2: C++ Expressions - Lecture 5 Sections 2.6, 2.13, 2people.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2018/Lecture … · These five operators are all left associative. Unary operators

1 Declaration of Objects

2 Arithmetic Operators

3 Expressions

4 Precedence and Associativity

5 Examples

6 Assignment

Robb T. Koether (Hampden-Sydney College) C++ Expressions Wed, Sep 5, 2018 2 / 23

Page 3: C++ Expressions - Lecture 5 Sections 2.6, 2.13, 2people.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2018/Lecture … · These five operators are all left associative. Unary operators

Outline

1 Declaration of Objects

2 Arithmetic Operators

3 Expressions

4 Precedence and Associativity

5 Examples

6 Assignment

Robb T. Koether (Hampden-Sydney College) C++ Expressions Wed, Sep 5, 2018 3 / 23

Page 4: C++ Expressions - Lecture 5 Sections 2.6, 2.13, 2people.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2018/Lecture … · These five operators are all left associative. Unary operators

Object Names

To store a quantity, it must first be given a name, called itsidentifier.Naming rules.

An identifier must begin with a letter.The identifier may otherwise contain any combination of letters,digits, and underscores (no embedded blanks).Names are case-sensitive: cost and Cost are not the same.

Robb T. Koether (Hampden-Sydney College) C++ Expressions Wed, Sep 5, 2018 4 / 23

Page 5: C++ Expressions - Lecture 5 Sections 2.6, 2.13, 2people.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2018/Lecture … · These five operators are all left associative. Unary operators

Object-Naming Conventions

Naming conventions.The name should be meaningful.The name should begin with a lowercase letter.In a multi-word name

Begin each subsequent word with an uppercase letter (camel style),e.g., numberOfStudents.Or, separate the words with underscores and use all lowercase, e.g.,number_of_students.

Robb T. Koether (Hampden-Sydney College) C++ Expressions Wed, Sep 5, 2018 5 / 23

Page 6: C++ Expressions - Lecture 5 Sections 2.6, 2.13, 2people.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2018/Lecture … · These five operators are all left associative. Unary operators

Object Declarations

To declare an object is to introduce its name and to specify itsdata type.An object must be declared either before or at its point of first use.It must be declared once and only once.

Robb T. Koether (Hampden-Sydney College) C++ Expressions Wed, Sep 5, 2018 6 / 23

Page 7: C++ Expressions - Lecture 5 Sections 2.6, 2.13, 2people.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2018/Lecture … · These five operators are all left associative. Unary operators

Object Declarations

Object Declarationint a; // Reserves 4 unitialized bytesfloat f; // Reserves 4 unitialized byteschar c; // Reserves 1 unitialized bytestring s; // Reserves 12 bytes (empty string)

To write a declaration, begin the statement with the object type,followed by the name of the object.The compiler will reserve the appropriate number of bytes ofmemory for the object’s value and correctly interpret that valuethroughout the program.

Robb T. Koether (Hampden-Sydney College) C++ Expressions Wed, Sep 5, 2018 7 / 23

Page 8: C++ Expressions - Lecture 5 Sections 2.6, 2.13, 2people.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2018/Lecture … · These five operators are all left associative. Unary operators

Object Initialization

Object Initializationint a; // Uninitializedint b = 5; // Initialized to 5int c = b; // Initialized to 5int d = b + 2*c; // Initialized to 15string s; // Initialized to ""string t = "hello" // Initialized to "hello"

Fundamental-type objects are not automatically initialized.string objects are automatically initialized to the empty string.An object may be initialized using either a literal, or anothernamed object, or an expression.

Robb T. Koether (Hampden-Sydney College) C++ Expressions Wed, Sep 5, 2018 8 / 23

Page 9: C++ Expressions - Lecture 5 Sections 2.6, 2.13, 2people.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2018/Lecture … · These five operators are all left associative. Unary operators

Outline

1 Declaration of Objects

2 Arithmetic Operators

3 Expressions

4 Precedence and Associativity

5 Examples

6 Assignment

Robb T. Koether (Hampden-Sydney College) C++ Expressions Wed, Sep 5, 2018 9 / 23

Page 10: C++ Expressions - Lecture 5 Sections 2.6, 2.13, 2people.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2018/Lecture … · These five operators are all left associative. Unary operators

Unary Operators

A unary operator operates on a single object.Some unary operators for ints and floats are

Negation: -Increment: ++Decrement: --

Robb T. Koether (Hampden-Sydney College) C++ Expressions Wed, Sep 5, 2018 10 / 23

Page 11: C++ Expressions - Lecture 5 Sections 2.6, 2.13, 2people.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2018/Lecture … · These five operators are all left associative. Unary operators

Binary Operators

A binary operator operates on a pair of objects called the leftoperand and the right operand.Some binary operators for ints and floats are

Addition: +Subtraction: -Multiplication: *Division: /

Robb T. Koether (Hampden-Sydney College) C++ Expressions Wed, Sep 5, 2018 11 / 23

Page 12: C++ Expressions - Lecture 5 Sections 2.6, 2.13, 2people.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2018/Lecture … · These five operators are all left associative. Unary operators

The Remainder Operator

The remainder operator %, also called the mod operator, gives theremainder when the left operand is divided by the right operand.The remainder operator may be applied only to integer types.Numerical examples

20 % 3 = 250 % 5 = 03 % 10 = 3

Robb T. Koether (Hampden-Sydney College) C++ Expressions Wed, Sep 5, 2018 12 / 23

Page 13: C++ Expressions - Lecture 5 Sections 2.6, 2.13, 2people.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2018/Lecture … · These five operators are all left associative. Unary operators

Outline

1 Declaration of Objects

2 Arithmetic Operators

3 Expressions

4 Precedence and Associativity

5 Examples

6 Assignment

Robb T. Koether (Hampden-Sydney College) C++ Expressions Wed, Sep 5, 2018 13 / 23

Page 14: C++ Expressions - Lecture 5 Sections 2.6, 2.13, 2people.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2018/Lecture … · These five operators are all left associative. Unary operators

Expressions

An expression consists of any legitimate combination of objectsand operators.For example,

2*a + b

Every expression has a value and a type.

Robb T. Koether (Hampden-Sydney College) C++ Expressions Wed, Sep 5, 2018 14 / 23

Page 15: C++ Expressions - Lecture 5 Sections 2.6, 2.13, 2people.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2018/Lecture … · These five operators are all left associative. Unary operators

Expression Types

If an arithmetic expression consists only of integers, then theresult is an integer.If an arithmetic expression consists only of floating-point numbers,then the result is a floating-point number.The one tricky case is integer division:

An integer divided by an integer is an integer.

Robb T. Koether (Hampden-Sydney College) C++ Expressions Wed, Sep 5, 2018 15 / 23

Page 16: C++ Expressions - Lecture 5 Sections 2.6, 2.13, 2people.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2018/Lecture … · These five operators are all left associative. Unary operators

Outline

1 Declaration of Objects

2 Arithmetic Operators

3 Expressions

4 Precedence and Associativity

5 Examples

6 Assignment

Robb T. Koether (Hampden-Sydney College) C++ Expressions Wed, Sep 5, 2018 16 / 23

Page 17: C++ Expressions - Lecture 5 Sections 2.6, 2.13, 2people.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2018/Lecture … · These five operators are all left associative. Unary operators

Precedence

When two different operators are used in an expression, aprecedence rule tells which one is done first.The operator with higher precedence is done before the operatorwith lower precedence.

Robb T. Koether (Hampden-Sydney College) C++ Expressions Wed, Sep 5, 2018 17 / 23

Page 18: C++ Expressions - Lecture 5 Sections 2.6, 2.13, 2people.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2018/Lecture … · These five operators are all left associative. Unary operators

Associativity

When the same operator is used twice in an expression, anassociativity rule tells which one is done first.Left associativity – perform operations from left to right.Right associativity – perform operations from right to left.

Robb T. Koether (Hampden-Sydney College) C++ Expressions Wed, Sep 5, 2018 18 / 23

Page 19: C++ Expressions - Lecture 5 Sections 2.6, 2.13, 2people.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2018/Lecture … · These five operators are all left associative. Unary operators

Summary of Precedence and Associativity Rules

Addition and subtraction have the same precedence.Multiplication, division, and remainder have the same precedence,which is higher than addition and subtraction.These five operators are all left associative.Unary operators have the highest precedence and they are rightassociative.In all other cases, use parentheses to specify the order or look itup.

Robb T. Koether (Hampden-Sydney College) C++ Expressions Wed, Sep 5, 2018 19 / 23

Page 20: C++ Expressions - Lecture 5 Sections 2.6, 2.13, 2people.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2018/Lecture … · These five operators are all left associative. Unary operators

Outline

1 Declaration of Objects

2 Arithmetic Operators

3 Expressions

4 Precedence and Associativity

5 Examples

6 Assignment

Robb T. Koether (Hampden-Sydney College) C++ Expressions Wed, Sep 5, 2018 20 / 23

Page 21: C++ Expressions - Lecture 5 Sections 2.6, 2.13, 2people.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2018/Lecture … · These five operators are all left associative. Unary operators

Sample Programs

ExamplesBMI.cppQuadraticRoots.cpp

Robb T. Koether (Hampden-Sydney College) C++ Expressions Wed, Sep 5, 2018 21 / 23

Page 22: C++ Expressions - Lecture 5 Sections 2.6, 2.13, 2people.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2018/Lecture … · These five operators are all left associative. Unary operators

Outline

1 Declaration of Objects

2 Arithmetic Operators

3 Expressions

4 Precedence and Associativity

5 Examples

6 Assignment

Robb T. Koether (Hampden-Sydney College) C++ Expressions Wed, Sep 5, 2018 22 / 23

Page 23: C++ Expressions - Lecture 5 Sections 2.6, 2.13, 2people.hsc.edu/faculty-staff/robbk/Coms261/Lectures 2018/Lecture … · These five operators are all left associative. Unary operators

Assignment

AssignmentRead Sections 2.6, 2.13, 2.15

Robb T. Koether (Hampden-Sydney College) C++ Expressions Wed, Sep 5, 2018 23 / 23