Lecture 3 Tao Wang 1

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

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

The C++ Language. Arizona State University 1

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

C: How to Program. Week /Mar/05

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

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

Chapter 2 - Introduction to C Programming

Lecture 5 Tao Wang 1

Computer Programming CS F111

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

Objectives. Data Types (continued) Data Types 4. การเข ยนโปรแกรมพ นฐาน ว ทยาการคอมพ วเตอร เบ องต น Fundamentals of Computer Science

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

Introduction to Programming

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

Unit 3. Operators. School of Science and Technology INTRODUCTION

CSc Introduction to Computing

การเขยนโปรแกรมพนฐาน ภาคการศ กษาท 1 ป การศ กษา การเข ยนโปรแกรมพ นฐาน ว ทยาการคอมพ วเตอร เบ องต วทยาการคอมพวเตอรเบองตน น

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

Le L c e t c ur u e e 2 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Variables Operators

3. Except for strings, double quotes, identifiers, and keywords, C++ ignores all white space.

3. EXPRESSIONS. It is a sequence of operands and operators that reduce to a single value.

UNIT- 3 Introduction to C++

Chapter 1 Introduction to Computers and C++ Programming

I Internal Examination Sept Class: - BCA I Subject: - Principles of Programming Lang. (BCA 104) MM: 40 Set: A Time: 1 ½ Hrs.

Objectives. In this chapter, you will:

by Pearson Education, Inc. All Rights Reserved. 2

7/8/10 KEY CONCEPTS. Problem COMP 10 EXPLORING COMPUTER SCIENCE. Algorithm. Lecture 2 Variables, Types, and Programs. Program PROBLEM SOLVING

CHAPTER 3 BASIC INSTRUCTION OF C++

Chapter 2, Part III Arithmetic Operators and Decision Making

BITG 1233: Introduction to C++

C++ Basic Elements of COMPUTER PROGRAMMING. Special symbols include: Word symbols. Objectives. Programming. Symbols. Symbols.

Add Subtract Multiply Divide

LECTURE 3 C++ Basics Part 2

Basics of Programming

Operators in C. Staff Incharge: S.Sasirekha

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

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

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

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

On a 64-bit CPU. Size/Range vary by CPU model and Word size.

CMSC 104 -Lecture 6 John Y. Park, adapted by C Grasso

Chapter 2: Introduction to C++

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

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

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

Operators. Lecture 3 COP 3014 Spring January 16, 2018

Creating a C++ Program

Outline. Data and Operations. Data Types. Integral Types

Introduction to Computers. Laboratory Manual. Experiment #3. Elementary Programming, II

VARIABLES & ASSIGNMENTS

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

Chapter 1 & 2 Introduction to C Language

Chapter 2: Overview of C++

Operators. Java operators are classified into three categories:

COMP Primitive and Class Types. Yi Hong May 14, 2015

Chapter 1 INTRODUCTION

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

Engineering Problem Solving with C++, Etter/Ingber

Overview: Programming Concepts. Programming Concepts. Names, Values, And Variables

Overview: Programming Concepts. Programming Concepts. Chapter 18: Get With the Program: Fundamental Concepts Expressed in JavaScript

Basics of Java Programming

Programming in C++ 5. Integral data types

Class 2: Variables and Memory. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

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

Introduction to Computer Programming

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

Programming for Engineers Iteration

Introduction to Computer Science Midterm 3 Fall, Points

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

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

Another Simple Program: Adding Two Integers

More Programming Constructs -- Introduction

Reserved Words and Identifiers

Lecture 2 Tao Wang 1

Lecture 4 Tao Wang 1

Chapter 4: Basic C Operators

CS242 COMPUTER PROGRAMMING

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

Homework #3 CS2255 Fall 2012

CS313D: ADVANCED PROGRAMMING LANGUAGE

Chapter 2: Using Data

1. C++ Overview. C++ Program Structure. Data Types. Assignment Statements. Input/Output Operations. Arithmetic Expressions.

2 nd Week Lecture Notes

LECTURE 02 INTRODUCTION TO C++

Operators & Expressions

Course Outline. Introduction to java

4. Inputting data or messages to a function is called passing data to the function.

Chapter 7. Additional Control Structures

CHRIST THE KING BOYS MATRIC HR. SEC. SCHOOL, KUMBAKONAM CHAPTER 9 C++

Computer System and programming in C

Contents. Jairo Pava COMS W4115 June 28, 2013 LEARN: Language Reference Manual

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

Full file at

Programming for Engineers Introduction to C

Chapter 2 Basic Elements of C++

BASIC ELEMENTS OF A COMPUTER PROGRAM

(2-1) Numeric Expressions in C H&K Chapter 2. Instructor - Andrew S. O Fallon CptS 121 (August 27, 2018) Washington State University

Transcription:

Lecture 3 Tao Wang 1

Objectives In this chapter, you will learn about: Arithmetic operations Variables and declaration statements Program input using the cin object Common programming errors C++ for Engineers and Scientists, Third Edition 2

Arithmetic Operations C++ supports addition, subtraction, multiplication, division, and modulus division Different data types can be used in the same arithmetic expression Arithmetic operators are binary operators Binary operators: Require two operands C++ for Engineers and Scientists, Third Edition 3

Arithmetic Operations (continued) Operation Addition Subtraction Multiplication Division Modulus division Operator + - * / % C++ for Engineers and Scientists, Third Edition 4

Arithmetic Operations (continued) C++ for Engineers and Scientists, Third Edition 5

Expression Types Expression: Any combination of operators and operands that can be evaluated to yield a value If all operands are the same data type, the expression is named by the data type used (integer expression, floating-point expression, etc.) Mixed-mode expression: Contains integer and floating-point operands Yields a double-precision value C++ for Engineers and Scientists, Third Edition 6

Integer Division Integer division: Yields an integer result Any fractional remainders are dropped (truncated) Example: 15/2 yields 7 Modulus (remainder) operator: Returns only the remainder Example: 9 % 4 yields 1 C++ for Engineers and Scientists, Third Edition 7

Negation Unary operator: Requires only one operand Negation operator (-): Reverses the sign of the number C++ for Engineers and Scientists, Third Edition 8

Operator Precedence and Associativity Rules for writing arithmetic expressions: Never place two consecutive binary arithmetic operators side by side Use parentheses to form groupings Contents within parentheses are evaluated first May nest parentheses within other parentheses Evaluated from innermost to outermost Use the * operator for multiplication, not parentheses C++ for Engineers and Scientists, Third Edition 9

Operator Precedence and Associativity (continued) Expressions with multiple operators are evaluated by precedence of operators: All negations occur first Multiplication, division, and modulus are next, from left to right Addition and subtraction are last, from left to right C++ for Engineers and Scientists, Third Edition 10

Operator Precedence and Associativity (continued) Associativity: the order in which operators of the same precedence are evaluated Table 2.8 Operator Precedence and Associativity C++ for Engineers and Scientists, Third Edition 11

Variables and Declaration Statements Variable: All integer, float-point, and other values used in a program are stored and retrieved from the computer's memory Each memory location has a unique address Figure 2.8 Enough storage for two integers C++ for Engineers and Scientists, Third Edition 12

Variables and Declaration Statements (continued) Variable: Symbolic identifier for a memory address where data can be held Use identifier naming rules for variable names Figure 2.9 Naming storage locations C++ for Engineers and Scientists, Third Edition 13

Variables and Declaration Statements (continued) Assignment statement: Used to store a value into a variable Value of the expression on the right side of the = is assigned to the memory location of the variable on the left side of the = Examples: num1 = 45; num2 = 12; total = num1 + num2; C++ for Engineers and Scientists, Third Edition 14

Variables and Declaration Statements (continued) Declaration statement: Specifies the data type and identifier of a variable; sets up the memory location Syntax: datatype variablename; Data type is any valid C++ data type Example: int sum; Declarations may be used anywhere in a function Usually grouped at the opening brace C++ for Engineers and Scientists, Third Edition 15

Variables and Declaration Statements (continued) Character variables: Declared using the char keyword Multiple variables of the same data type can be declared in a single declaration statement Example: double grade1, grade2, total, average; Variables can be initialized in a declaration Example: double grade1 = 87.0 A variable must be declared before it is used C++ for Engineers and Scientists, Third Edition 16

Variables and Declaration Statements (continued) C++ for Engineers and Scientists, Third Edition 17

Memory Allocation Definition statement: A declaration that defines how much memory is needed for data storage Three items associated with each variable: Data type Actual value stored in the variable (its contents) Memory address of the variable Address operator (&) provides the variable s address C++ for Engineers and Scientists, Third Edition 18

Memory Allocation (continued) Declaring a variable causes memory to be allocated based on the data type Figure 2.10b Defining the floating-point variable named slope C++ for Engineers and Scientists, Third Edition 19

Memory Allocation (continued) C++ for Engineers and Scientists, Third Edition 20

Program Input Using cin cin Object: Allows data entry to a running program Use of the cin object causes the program to wait for input from the keyboard When keyboard entry is complete, the program resumes execution, using the entered data An output statement preceding the cin object statement provides a prompt to the user C++ for Engineers and Scientists, Third Edition 21

Program Input Using cin (continued) C++ for Engineers and Scientists, Third Edition 22

Program Input Using cin (continued) cin can accept multiple input values to be stored in different variables Multiple numeric input values must be separated by spaces Example: cin >> num1 >> num2 with keyboard entry: 0.052 245.79 C++ for Engineers and Scientists, Third Edition 23

Program Input Using cin (continued) C++ for Engineers and Scientists, Third Edition 24

Program Input Using cin (continued) User-input validation: The process of ensuring that data entered by the user matches the expected data type Robust program: One that detects and handles incorrect user entry C++ for Engineers and Scientists, Third Edition 25

Common Programming Errors Omitting the parentheses after main() Omitting or incorrectly typing the opening brace, {, or the closing brace, }, that signifies the start and end of a function body Misspelling the name of an object or function Forgetting to enclose a string sent to cout with quotation marks Omitting a semicolon at end of statement C++ for Engineers and Scientists, Third Edition 26

Common Programming Errors (continued) Adding a semicolon at end of #include statement Missing \n to indicate new line Substituting letter O for zero and vice versa Failing to declare all variables C++ for Engineers and Scientists, Third Edition 27

Common Programming Errors (continued) Storing an incorrect data type into a variable Attempting to use a variable with no value Dividing integer values incorrectly Mixing data types in the same expression C++ for Engineers and Scientists, Third Edition 28