Operations. Making Things Happen

Similar documents
Engineering Problem Solving with C++, Etter/Ingber

Chapter 2. Outline. Simple C++ Programs

A First Program - Greeting.cpp

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

BITG 1233: Introduction to C++

Visual C# Instructor s Manual Table of Contents

C++ Programming: From Problem Analysis to Program Design, Third Edition

Week 2: Console I/O and Operators Arithmetic Operators. Integer Division. Arithmetic Operators. Gaddis: Chapter 3 (2.14,3.1-6,3.9-10,5.

2 nd Week Lecture Notes

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

LECTURE 02 INTRODUCTION TO C++

Basics of Java Programming

ADARSH VIDYA KENDRA NAGERCOIL COMPUTER SCIENCE. Grade: IX C++ PROGRAMMING. Department of Computer Science 1

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Introduction to C++ Introduction and History. Characteristics of C++

Characters, c-strings, and the string Class. CS 1: Problem Solving & Program Design Using C++

The C++ Language. Arizona State University 1

UNIT- 3 Introduction to C++

Summary of basic C++-commands

Chapter 2 Basic Elements of C++

EEE145 Computer Programming

Programming Fundamentals. With C++ Variable Declaration, Evaluation and Assignment 1

Chapter 2: Using Data

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

Chapter Overview. C++ Basics. Variables and Assignments. Variables and Assignments. Keywords. Identifiers. 2.1 Variables and Assignments

Structured Programming Using C++ Lecture 2 : Introduction to the C++ Language. Dr. Amal Khalifa. Lecture Contents:

Introduction to Programming EC-105. Lecture 2

6.096 Introduction to C++ January (IAP) 2009

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Chapter 2: Overview of C++

Objectives. In this chapter, you will:

Reserved Words and Identifiers

! A program is a set of instructions that the. ! It must be translated. ! Variable: portion of memory that stores a value. char

ME240 Computation for Mechanical Engineering. Lecture 4. C++ Data Types

Chapter 2: Introduction to C++

CSCI 6610: Review. Chapter 7: Numbers Chapter 8: Characters Chapter 11 Pointers

C++ Programming Lecture 11 Functions Part I

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

CS313D: ADVANCED PROGRAMMING LANGUAGE

Fundamentals of Programming CS-110. Lecture 2

C++ PROGRAMMING. For Industrial And Electrical Engineering Instructor: Ruba A. Salamh

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

Lecture 2 Tao Wang 1

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Chapter 2. C++ Basics

Unit 3. Constants and Expressions

CS103L FALL 2017 UNIT 1: TYPES, VARIABLES,EXPRESSIONS,C++ BASICS

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

Chapter 2 Working with Data Types and Operators

CSCI 123 Introduction to Programming Concepts in C++

Chapter 2 Elementary Programming

c++ keywords: ( all lowercase ) Note: cin and cout are NOT keywords.

Java Notes. 10th ICSE. Saravanan Ganesh

Module 2 Introducing Data Types and Operators

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

Chapter 4: Basic C Operators

CS2141 Software Development using C/C++ C++ Basics

JAVA Programming Fundamentals

REVIEW. The C++ Programming Language. CS 151 Review #2

CSc Introduction to Computing

Datatypes, Variables, and Operations

3.1. Chapter 3: The cin Object. Expressions and Interactivity

1. a) What #include statement do you put at the top of a program that does uses cin, cout or endl?

CS31 Discussion 1E. Jie(Jay) Wang Week3 Oct.12

Variables and Literals

PART I. Part II Answer to all the questions 1. What is meant by a token? Name the token available in C++.

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

CS1010E Lecture 3 Simple C Programs Part 2

ME 172. Lecture 2. Data Types and Modifier 3/7/2011. variables scanf() printf() Basic data types are. Modifiers. char int float double

COMP2121: Microprocessors and Interfacing. Number Systems

Differentiate Between Keywords and Identifiers

Full file at

Chapter 1. C++ Basics. Copyright 2010 Pearson Addison-Wesley. All rights reserved

CHAPTER 3 BASIC INSTRUCTION OF C++

Objects and Types. COMS W1007 Introduction to Computer Science. Christopher Conway 29 May 2003

A Java program contains at least one class definition.

These are reserved words of the C language. For example int, float, if, else, for, while etc.

This watermark does not appear in the registered version - Slide 1

Numbers. John Perry. Spring 2017

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

The sequence of steps to be performed in order to solve a problem by the computer is known as an algorithm.

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

BASIC ELEMENTS OF A COMPUTER PROGRAM

CSCI 1061U Programming Workshop 2. C++ Basics

Overview of C. Basic Data Types Constants Variables Identifiers Keywords Basic I/O

THE INTEGER DATA TYPES. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

Unit 3, Lesson 2 Data Types, Arithmetic,Variables, Input, Constants, & Library Functions. Mr. Dave Clausen La Cañada High School

Exercises Software Development I. 03 Data Representation. Data types, range of values, internal format, literals. October 22nd, 2014

Variables, Constants, and Data Types

Zheng-Liang Lu Java Programming 45 / 79

Maciej Sobieraj. Lecture 1

Java Programming Fundamentals. Visit for more.

Chapter 2. Lexical Elements & Operators

Introduction to Programming

C++ Overview. Chapter 1. Chapter 2

Increment and the While. Class 15

Types and Expressions. Chapter 3

Number Systems, Scalar Types, and Input and Output

Transcription:

Operations Making Things Happen

Object Review and Continue Lecture 1 2

Object Categories There are three kinds of objects: Literals: unnamed objects having a value (0, -3, 2.5, 2.998e8, 'A', "Hello\n",...) Variables: named objects whose values can change during program execution Constants: named objects whose values do not change during program execution 3

Literals int literals are whole numbers: -27, 0, 4, +4 double literals are real numbers, and can be: fixed-point: -0.333, 0.5, 1.414,... floating-point: 2.998e8, 0.2998e9,... There are just two bool literals: false, true char literals are single ASCII characters: 'A', 'a', '9', '$', '?',... string literals are ASCII character sequences: "Hello", "Goodbye", "Goodbye\n",... 4

Variable Declarations Variables are used to store values, and can be either initialized or uninitialized... Examples: int age = 18; double GPA = 3.25, credits; char lettergrade = 'A'; bool ok, done = false; Pattern: Type Name [ = Expression ] ; 5

Assignment Statements The value of a variable can be changed using an assignment statement... Examples: age = 19; credits = hours * 3.0; lettergrade = 'B'; done = true; Pattern: Name = Expression; 6

Constant Declarations Constants are used to represent a value with a meaningful name, and must be initialized. Examples: const int MAX_SCORE = 100; const double PI = 3.14159; const char MIDDLE_INITIAL = 'A'; const string PROMPT = "Enter a number: "; Pattern: const Type Name = Expression; 7

Identifiers Technically, the name of an object is called an identifier (it identifies the object). C++ identifiers must begin with a letter (underscores are permitted, but discouraged) followed by zero or more letters, digits or underscores. Valid: age, r2d2, mygpa, MAX_SCORE,... Invalid: 123go, coffee-time, sam s, $name,... To be continued. 8

Conventions To keep variable and constant objects distinct: Constant names are all uppercase, with multiple words separated by underscores (e.g., MAX_SCORE) Variable names are all lowercase, with the first letter of each word after the first capitalized (e.g., lastname) 9

char Objects... are represented in memory by a code ASCII code uses 8 bits to represent a character, allowing for 2 8 = 256 different characters. Unicode uses 16 bits to represent a character, allowing for 2 16 = 65,536 different characters. ASCII is the most commonly used code: '0' = 48 = 00110000 'A' = 65 = 01000001 'a' = 97 = 01100001 10

Escape Characters C++ provides a number of escape characters: '\n' newline character '\t' horizontal tab '\v' vertical tab '\f' form feed '\a' alert/bell '\\' backslash char '\'' apostrophe '\"' double quote '\xdd' char with hex code 11

int Objects Three forms: decimal (base-10): begin with a non-zero or sign (-45, -2, 0, +21, 36, 65536,...) octal (base-8): a zero followed by digits (01, 02, 03, 04, 05, 06, 07, 010, 011, 012,...) hexadecimal (base-16): zero-x followed by digits with a, b, c, d, e, f = 10, 11, 12, 13, 14, 15 (0x1, 0x2,..., 0x7, 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0x10, 0x11,...) 12

Expressions 13

Expressions In a C++ program, any sequence of objects and operations that combine to produce a value is called an expression. Here is an example from snow weight problem: double weight = length*width*depth*wpcm*rate; or double w = l*wi*d*wpcm*r; Today, we re going to focus on C++ operations 14

Numeric Expressions C++ provides four familiar arithmetic operators: + for performing addition - for performing subtraction * for performing multiplication / for performing division Each of these four can be applied to either real (double) or integer (int) operands. 15

Division However, / behaves differently for int and double operands: If both operands are integers, an integer division is performed... 16

Integer vs. Real Division How does integer division differ from real? Back to primary school: Teacher: 4 goes into 3 how many times? Pupils: 0 times with a remainder 3 The expression 3 / 4 returns the quotient. The expression 3 % 4 returns the remainder. 17

Numeric Functions The library <cmath> contains a variety of mathematical functions, including: sin(x) asin(x) cos(x) acos(x) tan(x) atan(x) sqrt(x) log10(x) log(x) pow(x, y) floor(x) ceil(x) abs(x) 18

Using <cmath> functions #include <iostream> #include <cmath> using namespace std; int main() { cout << "\nenter base and exponent: "; double base, exponent; cin >> base >> exponent; double result = pow(base, exponent); cout << base << " raised to the power " << exponent << " is " << result << endl; } 19

Relational Operations C++ provides six operators for comparisons, each of which takes two operands and produces a bool value (true or false): x == y x!= y x < y x >= y x > y x <= y The easiest mistake to make in C++ is using = (assignment) in place of == (equality). 20

Preconditions If a program makes assumptions about its input values (e.g., that they re positive), such assumptions are called preconditions. Preconditions are boolean expressions that must be true in order for the program to work correctly. To check preconditions, C++ provides the assert() mechanism... 21

Assertions #include <iostream> #include <cassert> using namespace std; int main() { cout << "\nenter your age: "; int age; cin >> age; assert(age > 0); //... } assert() will halt the program if age <= 0... 22

Logical Operators More complex boolean expressions can be built using the logical operators: a && b // true iff a, b are both true a b // true iff a or b is true!a // true iff a is false Example: cin >> score; assert(0 <= score && score <= 100); 23

Ex2-1.cpp Character Functions The library <cctype> contains an assortment of boolean character-processing functions: isalpha(ch) isalnum(ch) isdigit(ch) iscntrl(ch) islower(ch) isupper(ch) isspace(ch) ispunct(ch) isprint(ch) isgraph(ch) plus two case-conversion functions: toupper(ch) tolower(ch) 24

Practice Write an assertion that will halt the program if a char object named ch is not an uppercase letter: assert(isupper(ch)); Write an assertion that will halt the program if ch is not one of A through G : Assert(( A <= ch && ch <= G ) ( a <= ch && ch <= g )); 25

Precedence Issue: Is the value of the expression 2 + 3 * 4 (2 + 3) * 4 20, or 2 + (3 * 4) 14? Operator precedence governs evaluation order. * has higher precedence than +, so it is applied first, making the answer 14. 26

Operator Precedence () HIGHER + (positive), - (negative),! (NOT) *, /, % + (addition), - (subtraction) <, <=, >, >= ==,!= && LOWER See Appendix C for a complete list... 27

Associativity Does the expression 8-4 -2 evaluate (8-4) - 2 2, or 8 - (4-2) 6? Precedence doesn t help us... Associativity tells us. Since - is left-associative, the left - is evaluated first, giving us 2. Most (but not all) C++ operators associate left. See Appendix C in the text for a complete list... 28

Assignment The assignment operator is one that is rightassociative, which supports expressions like: int w, x, y, z; w = x = y = z = 0; The rightmost = is applied first, assigning z zero, then y is assigned the value of z (0), then x is assigned the value of y (0), and finally w is assigned the value of x (0). 29

Assignment Shortcuts Some assignments are so common: var = var + x; // add x to var var = var - y; // sub y from var C++ provides shortcuts for them: var += x; // add x to var var -= y; // sub y from var 30

In General Most arithmetic expressions of the form: var = var value; can be written in the shortcut form: var = value; Examples: double x, y; cin >> x >> y; x *= 2.0; // double x s value y /= 2.0; // decrease y by half 31

Increment and Decrement Other common assignments include: var = var + 1; // add 1 to var var = var - 1; // sub 1 from var so C++ provides shortcuts for them, too: var++; // add 1 to var var--; // sub 1 from var 32

Prefix Increment The prefix form of increment produces the final (incremented) value as its result: int x, y = 0; x = ++y; cout << x; // 1 is displayed The prefix decrement behaves similarly... 33

Postfix Increment The postfix form of increment produces the original (unincremented) value as its result: int x, y = 0; x = y++; cout << x; // 0 is displayed The prefix decrement behaves similarly... 34

Ex2-2.cpp Prefix vs. Postfix So long as the increment (or decrement) operator is used as a separate statement: int y = 0, x = 0; ++x; // x == 1 y++; // y == 1 it makes no difference which version is used... 35

Summary C++ provides a rich set of operations, including arithmetic operations, character operations, and boolean operations. The C++ assignment operator = is a true operator that produces the value of its left operand as its result. C++ provides numerous shortcuts to reduce repetitive coding. 36