by Pearson Education, Inc. All Rights Reserved. 2

Similar documents
Introduction to C++ Programming Pearson Education, Inc. All rights reserved.

Chapter 2, Part III Arithmetic Operators and Decision Making

Chapter 1 Introduction to Computers and C++ Programming

Arithmetic Operators. Binary Arithmetic Operators. Arithmetic Operators. A Closer Look at the / Operator. A Closer Look at the % Operator

Another Simple Program: Adding Two Integers

Introduction to Programming

Object Oriented Design

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments.

Introduction to C++ Programming. Adhi Harmoko S, M.Komp

Lecture 3 Tao Wang 1

Full file at C How to Program, 6/e Multiple Choice Test Bank

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

CS242 COMPUTER PROGRAMMING

download instant at Introduction to C++

Introduction to Python Programming

2.5 Another Application: Adding Integers

IT 374 C# and Applications/ IT695 C# Data Structures

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

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

Operators. Java operators are classified into three categories:

Engineering Problem Solving with C++, Etter/Ingber

download instant at Introduction to C++

9/10/10. Arithmetic Operators. Today. Assigning floats to ints. Arithmetic Operators & Expressions. What do you think is the output?

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.

Introduction to Java Applications; Input/Output and Operators

Add Subtract Multiply Divide

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

Programming for Engineers Introduction to C

Introductionto C++ Programming

Exception Handling Pearson Education, Inc. All rights reserved.

C: How to Program. Week /Mar/05

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

Unit 3. Operators. School of Science and Technology INTRODUCTION

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. Outline. Simple C++ Programs

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

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

Full file at

CHAPTER 1.2 INTRODUCTION TO C++ PROGRAMMING. Dr. Shady Yehia Elmashad

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

CHAPTER 3 Expressions, Functions, Output

1. In C++, reserved words are the same as predefined identifiers. a. True

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

The C++ Language. Arizona State University 1

C++ As A "Better C" Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan.

Section we will not cover section 2.11 feel free to read it on your own

Chapter 2: Overview of C++

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

Slide 1 CS 170 Java Programming 1 Expressions Duration: 00:00:41 Advance mode: Auto

VARIABLES & ASSIGNMENTS

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

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

Chapter 2. C++ Basics

Introduction to TURING

Homework #3 CS2255 Fall 2012

Chapter 2: Introduction to C++

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

SSEA Computer Science: Track A. Dr. Cynthia Lee Lecturer in Computer Science Stanford

Programming - 1. Computer Science Department 011COMP-3 لغة البرمجة 1 لطالب كلية الحاسب اآللي ونظم المعلومات 011 عال- 3

Will introduce various operators supported by C language Identify supported operations Present some of terms characterizing operators

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

Programming with C++ as a Second Language

JavaScript: Introduction to Scripting

Objectives. In this chapter, you will:

Full file at

Programming. C++ Basics

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

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

Chapter 2 - Introduction to C Programming

Introduction to C# Applications Pearson Education, Inc. All rights reserved.

Tutorial-2a: First steps with C++ programming

Introduction to C# Applications

Perl: Arithmetic, Operator Precedence and other operators. Perl has five basic kinds of arithmetic:

A First Program - Greeting.cpp

Lesson 1: Arithmetic Review

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

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

BITG 1233: Introduction to C++

Operators and Expressions in C & C++ Mahesh Jangid Assistant Professor Manipal University, Jaipur

Chapter 1 Introduction to Computers and C++ Programming

Ex: If you use a program to record sales, you will want to remember data:

Introduction to Computer Programming

2 nd Week Lecture Notes

19 Much that I bound, I could not free; Much that I freed returned to me. Lee Wilson Dodd

Chapter 4 Section 2 Operations on Decimals

Lecture 3. Input and Output. Review from last week. Variable - place to store data in memory. identified by a name should be meaningful Has a type-

1. Variables 2. Arithmetic 3. Input and output 4. Problem solving: first do it by hand 5. Strings 6. Chapter summary

Course PJL. Arithmetic Operations

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

2. Distinguish between a unary, a binary and a ternary operator. Give examples of C++ operators for each one of them.

Building Java Programs

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

Introduction to Programming

JavaScript: Introductionto Scripting

Maciej Sobieraj. Lecture 1

Information Science 1

Data and Expressions. Outline. Data and Expressions 12/18/2010. Let's explore some other fundamental programming concepts. Chapter 2 focuses on:

CS102: Variables and Expressions

Transcription:

1992-2010 by Pearson Education, Inc. All Rights Reserved. 2

1992-2010 by Pearson Education, Inc. All Rights Reserved. 3

1992-2010 by Pearson Education, Inc. All Rights Reserved. 4

1992-2010 by Pearson Education, Inc. All Rights Reserved. 5

1992-2010 by Pearson Education, Inc. All Rights Reserved. 6

Declarations of variables can be placed almost anywhere in a program, but they must appear before their corresponding variables are used in the program. 1992-2010 by Pearson Education, Inc. All Rights Reserved. 7

1992-2010 by Pearson Education, Inc. All Rights Reserved. 8

A prompt it directs the user to take a specific action. A cin statement uses the input stream object cin (of namespace std) and the stream extraction operator, >>, to obtain a value from the keyboard. Using the stream extraction operator with std::cin takes character input from the standard input stream, which is usually the keyboard. 1992-2010 by Pearson Education, Inc. All Rights Reserved. 9

1992-2010 by Pearson Education, Inc. All Rights Reserved. 10

When the computer executes an input statement that places a value in an int variable, it waits for the user to enter a value for variable number1. The user responds by typing the number (as characters) then pressing the Enter key (sometimes called the Return key) to send the characters to the computer. The computer converts the character representation of the number to an integer and assigns (i.e., copies) this number (or value) to the variable number1. Any subsequent references to number1 in this program will use this same value. In this program, an assignment statement adds the values of variables number1 and number2 and assigns the result to variable sum using the assignment operator =. Most calculations are performed in assignment statements. 1992-2010 by Pearson Education, Inc. All Rights Reserved. 11

The = operator and the + operator are called binary operators because each has two operands. 1992-2010 by Pearson Education, Inc. All Rights Reserved. 12

1992-2010 by Pearson Education, Inc. All Rights Reserved. 13

std::endl is a so-called stream manipulator. The name endl is an abbreviation for end line and belongs to namespace std. The std::endl stream manipulator outputs a newline, then flushes the output buffer. This simply means that, on some systems where outputs accumulate in the machine until there are enough to make it worthwhile to display them on the screen, std::endl forces any accumulated outputs to be displayed at that moment. This can be important when the outputs are prompting the user for an action, such as entering data. 1992-2010 by Pearson Education, Inc. All Rights Reserved. 14

Using multiple stream insertion operators (<<) in a single statement is referred to as concatenating, chaining or cascading stream insertion operations. Calculations can also be performed in output statements. 1992-2010 by Pearson Education, Inc. All Rights Reserved. 15

Variable names such as number1, number2 and sum actually correspond to locations in the computer s memory. Every variable has a name, a type, a size and a value. When a value is placed in a memory location, the value overwrites the previous value in that location; thus, placing a new value into a memory location is said to be destructive. When a value is read out of a memory loca-tion, the process is nondestructive. 1992-2010 by Pearson Education, Inc. All Rights Reserved. 16

1992-2010 by Pearson Education, Inc. All Rights Reserved. 17

Most programs perform arithmetic calculations. Figure 2.9 summarizes the C++ arithmetic operators. The asterisk (*) indicates multiplication. The percent sign (%) is the modulus operator that will be discussed shortly. C++ provides the modulus operator, %, that yields the remainder after integer division. The modulus operator can be used only with integer operands. The arithmetic operators in Fig. 2.9 are all binary operators. Integer division (i.e., where both the numerator and the denominator are integers) yields an integer quotient. Any fractional part in integer division is discarded (i.e., truncated) no rounding occurs. 1992-2010 by Pearson Education, Inc. All Rights Reserved. 18

1992-2010 by Pearson Education, Inc. All Rights Reserved. 19

1992-2010 by Pearson Education, Inc. All Rights Reserved. 20

Arithmetic expressions in C++ must be entered into the computer in straight-line form. Expressions such as a divided by b must be written as a / b, so that all constants, variables and operators appear in a straight line. Parentheses are used in C++ expressions in the same manner as in algebraic expressions. For example, to multiply a times the quantity b + c we write a a * ( b + c ). 1992-2010 by Pearson Education, Inc. All Rights Reserved. 21

C++ applies the operators in arithmetic expressions in a precise sequence determined by the following rules of operator precedence, which are generally the same as those followed in algebra. 1992-2010 by Pearson Education, Inc. All Rights Reserved. 22

1992-2010 by Pearson Education, Inc. All Rights Reserved. 23

There is no arithmetic operator for exponentiation in C++, so x 2 is represented as x * x. Figure 2.11 illustrates the order in which the operators in a second-degree polynomial are applied. As in algebra, it s acceptable to place unnecessary parentheses in an expression to make the expression clearer. These are called redundant parentheses. 1992-2010 by Pearson Education, Inc. All Rights Reserved. 24

1992-2010 by Pearson Education, Inc. All Rights Reserved. 25