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

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

LECTURE 02 INTRODUCTION TO C++

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

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

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

Chapter 2: 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

The C++ Language. Arizona State University 1

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

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

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

Chapter 2: Basic Elements of C++

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++ Objectives. Objectives (cont d.) A C++ Program. Introduction

CHAPTER 3 BASIC INSTRUCTION OF C++

BITG 1233: Introduction to C++

Objectives. In this chapter, you will:

Lecture 2 Tao Wang 1

CSc Introduction to Computing

Computer Programming : C++

Starting Out with C++: Early Objects, 9 th ed. (Gaddis, Walters & Muganda) Chapter 2 Introduction to C++ Chapter 2 Test 1 Key

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

Getting started with C++ (Part 2)

CS242 COMPUTER PROGRAMMING

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

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

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

2 nd Week Lecture Notes

Week 3: File I/O and Formatting 3.7 Formatting Output

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

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

Chapter 2. C++ Basics

Chapter 2 Basic Elements of C++

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

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

Fundamentals of Programming CS-110. Lecture 2

CS101: Fundamentals of Computer Programming. Dr. Tejada www-bcf.usc.edu/~stejada Week 1 Basic Elements of C++

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

UNIT- 3 Introduction to C++

Programming. C++ Basics

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-

Chapter 2: Overview of C++

BTE2313. Chapter 2: Introduction to C++ Programming

Introduction to Programming EC-105. Lecture 2

Program Organization and Comments

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.

UEE1302 (1102) F10: Introduction to Computers and Programming

Object-oriented Programming for Automation & Robotics Carsten Gutwenger LS 11 Algorithm Engineering

Your First C++ Program. September 1, 2010

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

Maciej Sobieraj. Lecture 1

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

Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Chapter Two MULTIPLE CHOICE

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

Engineering Problem Solving with C++, Etter/Ingber

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

Your first C++ program

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

Creating a C++ Program

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

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

6.096 Introduction to C++ January (IAP) 2009

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

Chapter 2 C++ Fundamentals

Introduction to Programming

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

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

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

Introduction to the C++ Programming Language

CSI33 Data Structures

Full file at

Exercise: Using Numbers

Chapter 2. C++ Syntax and Semantics, and the Program Development Process. Dale/Weems 1

Programming with C++ as a Second Language

4. Structure of a C++ program

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

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

CS1500 Algorithms and Data Structures for Engineering, FALL Virgil Pavlu, Jose Annunziato,

Week 3. Function Definitions. Example: Function. Function Call, Return Statement. Functions & Arrays. Gaddis: Chapters 6 and 7.

C++ Programming Basics

Introduction to C++ (Extensions to C)

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

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

VARIABLES & ASSIGNMENTS

String Variables and Output/Input. Adding Strings and Literals to Your Programming Skills and output/input formatting

A First Program - Greeting.cpp

DECLARATIONS. Character Set, Keywords, Identifiers, Constants, Variables. Designed by Parul Khurana, LIECA.

Manual. Subject Code: CS593. Computer Science and Engineering

Input And Output of C++

Engineering Problem Solving with C++, 3e Chapter 2 Test Bank

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

The cin Object. cout << "Enter the length and the width of the rectangle? "; cin >> length >> width;

Exercise 1.1 Hello world

CSCI 123 Introduction to Programming Concepts in C++

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

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

Unit 3. Constants and Expressions

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

Transcription:

Week 1: Introduction to C++ Gaddis: Chapter 2 (excluding 2.1, 2.11, 2.14) CS 1428 Fall 2014 Jill Seaman Literals A literal represents a constant value used in a program statement. Numbers: 0, 34, 3.14159, -1.8e12, etc. Characters: 'A', 'z', '', '5', etc. Strings (sequence of characters): Hello, This is a string 100 years, 100, Y, etc. NOTE: These are all different: 5, '5', 5 1 2 Special characters Newline: \n' Double quote: \ '. These can occur in strings: Hello\nthere she said \ boo\ very quietly See textbook for more It s a backslash (\), not a slash (/) 2.2 The cout Object cout: short for console output a stream object: represents the contents of the screen <<: the stream insertion operator use it to send data to cout (to be output to the screen) cout << This is an example. when this instruction is executed, the console (screen) looks like this: This is an example. 3 4

The endl manipulator more examples endl: short for end line send it to cout when you want to start a new line of output. cout << Hello << endl << there ; or you can use the newline character: \n cout << Hello \nthere ; Either way the output to the screen is: cout << Hello << there ; Hello there cout << Hello ; cout << there ; Hello there Hello there 5 cout << The best selling book on Amazon\n is \ The Help\ ; The best selling book on Amazon is The Help 6 2.5 Identifiers 2.4 Variables (and Literals) An identifier is a name for some program element (like a variable). Rules: May not be a keyword (see Table 2.4 in the book) First character must be a letter or underscore Following characters must be letters, numbers or underscores. Identifiers are case-sensitive: myvariable is not the same as MyVariable 7 Variable: named location in main memory Has a name and a datatype <datatype> <identifier> The data type indicates the kind of data it can contain. A variable must be defined before it can be used Examples: int somenumber; char firstletter; 8

2.12 Variable Assignments and Initialization An assignment statement uses the = operator to store a value in an already defined variable. somenumber = 12; When this statement is executed, the computer stores the value 12 in memory, in the location named somenumber. The variable receiving the value must be on the left side of the = (the following does NOT work): 12 = somenumber; //This is an ERROR Example program using a variable #include <iostream> using namespace std; int main() { int number; number = 100; cout << The value of the number is << number << endl; return 0; 9 output screen: The value of the number is 100 10 Variable Initialization To initialize a variable means to assign it a value when it is defined: int length = 12; You can define and initialize multiple variables at once (and change them later) : int length = 12, width = 5, area; area = 35; length = 10; area =40; Data Types Variables are classified according to their data type. The data type determines the kind of information that may be stored in the variable. A data type is a set of values. Generally two main (types of) data types: Numeric Character-based 11 12

C++ Data Types int, short, long whole numbers (integers) float, double real numbers (with fractional amounts, decimal points) bool logical values: true and false char a single character string any text, a sequence of characters 13 2.6 Integer Data Types Whole numbers such as 12, 7, and -99 Typical ranges (may vary on different systems): Data Type: Range of values: short -32,768 to 32,767 unsigned short 0 to 65,535 int -2,147,483,648 to 2,147,483,647 unsigned int 0 to 4,294,967,295 long -2,147,483,648 to 2,147,483,647 unsigned long 0 to 4,294,967,295 Example variable definitions: short dayofweek; unsigned long distance; int xcoordinate; 14 2.9 Floating-Point Data Types Real numbers such as 12.45, and -3.8 Typical ranges (may vary on different systems): Data Type: float double long double Range of values: +/- 3.4e +/- 38 (~7 digits of precision) +/- 1.7e +/- 308 (~15 digits of precision) +/- 1.7e +/- 308 (~15 digits of precision) Floating-point literals can be represented in Fixed point (decimal) notation: 31.4159 0.0000625 E (scientific) notation: 3.14159E1 6.25e-5 15 Example program using floatingpoint data types // This program uses floating point data types. #include <iostream> using namespace std; int main() { float distance; double mass; distance = 1.495979E11; mass = 1.989E30; cout << "The Sun is " << distance << " meters away.\n"; cout << "The Sun\'s mass is " << mass << " kilograms.\n"; return 0; output screen: The Sun is 1.49598e+11 meters away. The Sun's mass is 1.989e+30 kilograms. 16

2.10 The bool Data Type The values true and false. Literal values: true, false (false is equivalent to 0, true is equivalent to 1) int main() { bool boolvalue; boolvalue = true; cout << boolvalue << endl; boolvalue = false; cout << boolvalue << endl; return 0; output screen: 1 0 17 2.7 The char Data Type All the keyboard and printable symbols. Literal values: A 5? b see also: slides 2 and 3. Numeric value of character from the ASCII character set is stored in memory: CODE: char letter; letter = C'; cout << letter << endl; MEMORY: letter 67 Appendix B shows the ASCII code values OUTPUT: C 18 2.8 The C++ string class Sequences of characters Requires the string header file: To define string variables in programs: string firstname, lastname; To assign literals to variables: firstname = "George"; lastname = "Washington"; To display via cout cout << firstname << " " << lastname; OUTPUT: George Washington #include <string> See slides 2 and 3 for more examples of string literals. 19 2.13 Scope The scope of a variable is the part of the program in which the variable can be accessed. A variable cannot be used before it is defined. // This program can't find its variable. #include <iostream> using namespace std; int main() { cout << value; // ERROR value not defined yet int value = 100; return 0; 20

2.15 Comments Used to document parts of the program Intended for humans reading the source code of the program: Indicate the purpose of the program Describe the use of variables Explain complex sections of code Are ignored by the compiler 21 Single and Mult-Line Comments Single-Line comments begin with // through to the end of line: int length = 12; // length in inches int width = 15; // width in inches int area; // calculated area // calculate rectangle area area = length * width; Multi-Line comments begin with /*, end with */ /* this is a multi-line comment */ int area; /* calculated area */ 22 2.16 Named Constants Named constant : variable whose value cannot be changed during program execution Used for representing constant values with descriptive names: const double TAX_RATE = 0.0675; const int NUM_STATES = 50; Often named in uppercase letters (see style guidelines) Note: initialization required. 23 2.17 Programming Style The visual organization of the source code Includes the use of spaces, tabs, and blank lines Includes naming of variables, constants. Includes where to use comments. Purpose: improve the readability of the source code 24

Programming Style Common elements to improve readability: Braces { aligned vertically Indentation of statements within a set of braces Blank lines between declaration and other statements Long statements wrapped over multiple lines with aligned operators See the Style Guidelines on the class website. You must follow these in your programming assignments. 25