Lecture 2 Tao Wang 1

Similar documents
CSc Introduction to Computing

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

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

UEE1302 (1102) F10: Introduction to Computers and Programming

Chapter 2: Introduction to C++

LECTURE 02 INTRODUCTION TO C++

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

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

Creating a C++ Program

CHAPTER 3 BASIC INSTRUCTION OF C++

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

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

6.096 Introduction to C++ January (IAP) 2009

BITG 1233: Introduction to C++

Today. o main function. o cout object. o Allocate space for data to be used in the program. o The data can be changed

2 nd Week Lecture Notes

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

Computer Programming : C++

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

UNIT- 3 Introduction to C++

Intro to Programming & C Why Program? 1.2 Computer Systems: Hardware and Software. Why Learn to Program?

The C++ Language. Arizona State University 1

Lab # 02. Basic Elements of C++ _ Part1

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

Fundamental of Programming (C)

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

Chapter 2 Basic Elements of C++

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

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

Chapter 2. C++ Basics

Chapter 1 INTRODUCTION

ARG! Language Reference Manual

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

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

Exercise: Using Numbers

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++

! A literal represents a constant value used in a. ! Numbers: 0, 34, , -1.8e12, etc. ! Characters: 'A', 'z', '!', '5', etc.

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

Relational Operators and if. Class 10

Basics of Java Programming

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

VARIABLES AND CONSTANTS

Chapter 1 Getting Started Structured Programming 1

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

Standard 11. Lesson 9. Introduction to C++( Up to Operators) 2. List any two benefits of learning C++?(Any two points)

4 Programming Fundamentals. Introduction to Programming 1 1

Visual C# Instructor s Manual Table of Contents

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

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

Lecture 3 Tao Wang 1

Full file at

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

Fundamentals of Programming

Objectives. In this chapter, you will:

.. Cal Poly CPE 101: Fundamentals of Computer Science I Alexander Dekhtyar..

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Fundamentals of Programming Session 4

Java Notes. 10th ICSE. Saravanan Ganesh

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

Programming for Engineers Introduction to C

BTE2313. Chapter 2: Introduction to C++ Programming

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

4. Structure of a C++ program

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

Programming and Data Structures

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

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

A First Program - Greeting.cpp

Key Differences Between Python and Java

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

Intro to Computer Programming (ICP) Rab Nawaz Jadoon

Getting started with C++ (Part 2)

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

Number Systems, Scalar Types, and Input and Output

Language Reference Manual

C: How to Program. Week /Mar/05

Integer Data Types. Data Type. Data Types. int, short int, long int

Variables. Data Types.

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

CS242 COMPUTER PROGRAMMING

Data Types and Variables in C language

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

INTRODUCTION 1 AND REVIEW

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

Fundamentals of Programming. Lecture 3: Introduction to C Programming

9/5/2018. Overview. The C Programming Language. Transitioning to C from Python. Why C? Hello, world! Programming in C

The Java Language Rules And Tools 3

Chapter 2 - Introduction to C Programming

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

The C Programming Language. (with material from Dr. Bin Ren, William & Mary Computer Science)

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

Fundamentals of Programming CS-110. Lecture 2

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

Chapter 2. Lexical Elements & Operators

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

Differentiate Between Keywords and Identifiers

Chapter 2, Part I Introduction to C Programming

The Warhol Language Reference Manual

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

Transcription:

Lecture 2 Tao Wang 1

Objectives In this chapter, you will learn about: Modular programs Programming style Data types Arithmetic operations Variables and declaration statements Common programming errors C++ for Engineers and Scientists, Third Edition 2

Introduction to C++ Modular program: A program consisting of interrelated segments arranged in a logical and understandable form Easier to develop, correct, and modify than other kinds of programs Module: A small segment which is designed to perform a specific task A group of modules is used to construct a modular program C++ for Engineers and Scientists, Third Edition 3

Introduction to C++ (continued) Figure 2.1 A well-designed program is built using modules. C++ for Engineers and Scientists, Third Edition 4

Introduction to C++ (continued) Modules in C++ can be classes or functions Function: Accepts an input and produces an output by processing the input in some fashion A function s processing is encapsulated and hidden within the function C++ for Engineers and Scientists, Third Edition 5

Introduction to C++ (continued) Figure 2.2 A multiplying function. C++ for Engineers and Scientists, Third Edition 6

Introduction to C++ (continued) Class: Contains both data and functions used to manipulate the data Function: Encapsulates a set of operations A class encapsulates data plus one or more sets of operations Identifier: A name given to an element of the language, such as a class or function C++ for Engineers and Scientists, Third Edition 7

Introduction to C++ (continued) Rules for forming identifier names: First character must be a letter or underscore Only letters, digits, or underscores may follow the initial letter (no blanks allowed) Keywords cannot be used as identifiers Maximum length of an identifier = 1024 characters C++ for Engineers and Scientists, Third Edition 8

Introduction to C++ (continued) Keyword: A reserved name that represents a built-in object or function of the language Table 2.1: Keywords in C++ C++ for Engineers and Scientists, Third Edition 9

Introduction to C++ (continued) Examples of valid C++ identifiers: degtorad intersect addnums slope bessell multtwo findmax density Examples of invalid C++ identifiers: 1AB3 (begins with a number) E*6 (contains a special character) while (this is a keyword) C++ for Engineers and Scientists, Third Edition 10

Introduction to C++ (continued) Function names Require a set of parentheses at the end Can use mixed upper and lower case Should be meaningful, or be a mnemonic Mnemonic: A word designed as a memory aid Examples of function names: easy() c3po() r2d2() theforce() Note that C++ is a case-sensitive language! C++ for Engineers and Scientists, Third Edition 11

The main() Function Overall structure of a C++ program contains one function named main(), called the driver function All other functions are invoked from main() C++ for Engineers and Scientists, Third Edition 12

The main() Function (continued) Figure 2.3 The main() function directs all other functions. C++ for Engineers and Scientists, Third Edition 13

The main() Function (continued) Function header line: First line of a function, which contains: The type of data returned by the function (if any) The name of the function The type of data that must be passed into the function when it is invoked (if any) Arguments: The data passed into a function Function body: The statements inside a function (enclosed in braces) C++ for Engineers and Scientists, Third Edition 14

The main() Function (continued) Each statement inside the function must be terminated with a semicolon return: A keyword causing the appropriate value to be returned from the function The statement return 0 in the main() function causes the program to end C++ for Engineers and Scientists, Third Edition 15

The main() Function (continued) Figure 2.4 The structure of a main() function C++ for Engineers and Scientists, Third Edition 16

The cout Object cout object: An output object that sends data to a standard output display device C++ for Engineers and Scientists, Third Edition 17

The cout Object (continued) Preprocessor command: Starts with a # Causes an action before the source code is compiled into machine code #include <file name>: Causes the named file to be inserted into the source code C++ provides a standard library with many pre-written classes that can be included Header files: Files included at the head (top) of a C++ program C++ for Engineers and Scientists, Third Edition 18

The cout Object (continued) using namespace <namespace name> : Indicates where header file is located Namespaces qualify a name A function name in your class can be the same as one used in a standard library class String: Any combination of letters, numbers, and special characters enclosed in double quotes (a delimiter) Delimiter: A symbol that marks the beginning and ending of a string; not part of the string C++ for Engineers and Scientists, Third Edition 19

The cout Object (continued) C++ for Engineers and Scientists, Third Edition 20

The cout Object (continued) Escape sequence: One or more characters preceded by a backslash, \ C++ for Engineers and Scientists, Third Edition 21

Programming Style Although more than one C++ statement can be on a single line, good style calls for one statement per line Opening and closing braces {} for the function body should each be on separate lines Statements in the function body should be indented C++ for Engineers and Scientists, Third Edition 22

Comments Comments: Explanatory remarks in the source code added by the programmer Line comment: Begins with // and continues to the end of the line Line comment can be on a line by itself, or at the end of a line of code Line comment cannot be longer than one line C++ for Engineers and Scientists, Third Edition 23

Comments (continued) C++ for Engineers and Scientists, Third Edition 24

Comments (continued) Block comments: Span across two or more lines Begin with /* and ends with */ Example: /* This is a block comment that spans across three lines */ C++ for Engineers and Scientists, Third Edition 25

Data Types Data type: A set of values and the operations that can be applied to these values Two fundamental C++ data groupings: Class data type (a class): Created by the programmer Built-in data type (primitive type): Part of the C++ compiler C++ for Engineers and Scientists, Third Edition 26

Data Types (continued) Figure 2.5 Built-in data types C++ for Engineers and Scientists, Third Edition 27

Data Types (continued) Table 2.2 Built-in Data Types Operations C++ for Engineers and Scientists, Third Edition 28

Data Types (continued) Literal (constant): An actual value Examples: 3.6 //numeric literal Hello Integer: A whole number //string literal C++ has nine built-in integer data types Each provides differing amounts of storage (compiler dependent) C++ for Engineers and Scientists, Third Edition 29

Integer Data Types Figure 2.6 C++ integer data types C++ for Engineers and Scientists, Third Edition 30

Integer Data Types (continued) int data type: Whole numbers, optionally with plus (+) or minus ( ) sign Example: 2 char data type: Individual character; any letter, digit, or special character enclosed in single quotes Example: A Character values are usually stored in ASCII code C++ for Engineers and Scientists, Third Edition 31

Integer Data Types (continued) Table 2.3 The ASCII Uppercase Letter Codes C++ for Engineers and Scientists, Third Edition 32

Integer Data Types (continued) When storing the ASCII codes shown in Table 2.3 to represent text, each letter takes one byte of memory and is represented by the associated number from the chart Figure 2.7 The letters BARTER stored inside a computer C++ for Engineers and Scientists, Third Edition 33

Integer Data Types (continued) Escape character: The backslash, \ Indicates an escape sequence Escape sequence: Tells compiler to treat the following characters as special instruction codes C++ for Engineers and Scientists, Third Edition 34

Integer Data Types (continued) Table 2.4 Escape sequences C++ for Engineers and Scientists, Third Edition 35

Integer Data Types (continued) Table 2.4 Escape sequences (continued) C++ for Engineers and Scientists, Third Edition 36

Integer Data Types (continued) bool data type: Represents Boolean (logical) data Restricted to two values: true or false Useful to indicate a condition and take a prescribed course of action C++ for Engineers and Scientists, Third Edition 37

Determining Storage Size A unique feature of C++ is that you can see where and how values are stored sizeof() operator provides the number of bytes used to store values of the data type names in the parenthesis Values returned by sizeof() are compiler dependent C++ for Engineers and Scientists, Third Edition 38

Determining Storage Size (continued) C++ for Engineers and Scientists, Third Edition 39

Signed and Unsigned Data Types Signed data type: One that permits negative, positive, and zero values Unsigned data type: Permits only positive and zero values An unsigned data type provides essentially double the range of its signed counterpart C++ for Engineers and Scientists, Third Edition 40

Signed and Unsigned Data Types (continued) Table 2.5 Integer Data Type Storage C++ for Engineers and Scientists, Third Edition 41

Floating-Point Types Floating-point number (real number): Zero or any positive or negative number containing a decimal point Examples: +10.625 5. -6.2 No special characters are allowed Three floating-point data types in C++: float (single precision) double (double precision) long double C++ for Engineers and Scientists, Third Edition 42

Floating-Point Types (continued) Table 2.6 Floating-Point Data Types C++ for Engineers and Scientists, Third Edition 43

Floating-Point Types (continued) float literal: Append an f or F to the number long double literal: Append an l or L to the number Examples: 9.234 // a double literal 9.234F // a float literal 9.234L // a long double literal C++ for Engineers and Scientists, Third Edition 44