CHAPTER 2 C++ SYNTAX & SEMANTICS #include using namespace std; int main() { cout << “Hello World!!”; return 0; } See \cp1\cpp\skeleton.cpp C++ Hello.

Slides:



Advertisements
Similar presentations
1 C++ Syntax and Semantics The Development Process.
Advertisements

Dale/Weems/Headington
1 Lecture-4 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
Chapter 2: Basic Elements of C++
© 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5/e Starting Out with C++: Early Objects 5 th Edition Chapter 2 Introduction.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
Data types and variables
Chapter 2: Basic Elements of C++
CS150 Introduction to Computer Science 1
Chapter 2 Data Types, Declarations, and Displays
Chapter 2: Introduction to C++.
Chapter 2: Basic Elements of C++
Basic Elements of C++ Chapter 2.
CHAPTER 2 BASIC ELEMENTS OF C++. In this chapter, you will:  Become familiar with the basic components of a C++ program, including functions, special.
Chapter 2 Data Types, Declarations, and Displays.
Objectives You should be able to describe: Data Types
CSC 125 Introduction to C++ Programming Chapter 2 Introduction to C++
Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems.
Elements of a C++ program 1. Review Algorithms describe how to solve a problem Structured English (pseudo-code) Programs form that can be translated into.
CS1 Lesson 2 Introduction to C++ CS1 Lesson 2 -- John Cole1.
IXA 1234: C++ PROGRAMMING CHAPTER 2: PROGRAM STRUCTURE.
1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
1 C++ Syntax and Semantics, and the Program Development Process.
Chapter 2. C++ Program Structure C++ program is a collection of subprograms Subprograms in C++ are called FUNCTIONS Each function performs a specific.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems.
C++ Programming: Basic Elements of C++.
Knowledge Base C++ #include using std namespace; int main(){} return 0 ; cout
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems.
Chapter 2 C++ Syntax and Semantics, and the Program Development Process.
Introduction to C++ Basic Elements of C++. C++ Programming: From Problem Analysis to Program Design, Fourth Edition2 The Basics of a C++ Program Function:
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process.
Data Types Declarations Expressions Data storage C++ Basics.
Chapter 2: Introduction to C++. Outline Basic “Hello World!!” Variables Data Types Illustration.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 2: Basic Elements of C++
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
1 What is a Named Constant? A named constant is a location in memory that we can refer to by an identifier, and in which a data value that cannot be changed.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Programming in C++
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Chapter 2: Basic Elements of C++
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
Chapter 2 C++ Syntax and Semantics, and the Program Development Process Topics – Programs Composed of Several Functions – Syntax Templates – Legal C++
Chapter 2: Basic Elements of C++. Introduction Computer program – Sequence of statements whose objective is to accomplish a task Programming – Process.
Chapter 2: Basic Elements of C++. Objectives In this chapter, you will: – Become familiar with the basic components of a C++ program, including functions,
1 A Simple “Hello World” Example #include // input-output library using namespace std; int main() // function main { cout
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
Chapter 2: Basic Elements of C++
Chapter 2: Basic Elements of C++
Chapter Topics The Basics of a C++ Program Data Types
Computer Programming BCT 1113
Chapter 2: Introduction to C++
Documentation Need to have documentation in all programs
Chapter 2 Topics Programs Composed of Several Functions
Basic Elements of C++.
Chapter 2: Basic Elements of C++
Chapter 2: Introduction to C++
Basic Elements of C++ Chapter 2.
2.1 Parts of a C++ Program.
Chapter 2: Introduction to C++.
C++ Programming Basics
Presentation transcript:

CHAPTER 2 C++ SYNTAX & SEMANTICS

#include using namespace std; int main() { cout << “Hello World!!”; return 0; } See \cp1\cpp\skeleton.cpp C++ Hello World Program

Syntax vs. Semantics n Syntax n language rules n how valid instructions are written in a programming language n GRAMMAR English

n Semantics n rules determining MEANING of instructions in programming language

Identifiers

What is an identifier? n A name associated with a process or object used to refer to that process or object n John, Sue, Larry n mass, volume, pay

What are rules for valid identifiers? n Identifiers are made of n letters n digits n underscore n The first character must be a n letter n underscore

n VALID IDENTIFIERS n sum_of_squaresJ9GetData n box_22AcountBin3D4 n INVALID n 40HoursGet DataBox-22 n cost_in_$intreturn n RESERVED WORDS n word that has special meaning in C++ n cannot be used as identifier (if)

Why is it important to use meaningful identifier names? n Easier to understand n Self documenting n Compare printtopportion PRINTTOPPORTION PrintTopPortion n Last is best

Case Sensitive n C++ identifiers with different capitalization are entirely distinct. n Total total n Two different identifiers.

Data Types

What is a data type? n A specific set of data values with operations on those values n C++ has a number of built-in data types.

C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long double

C++ Simple Data Types simple types integralfloating char short int long bool enum float double long double unsigned

Standard Data Types in C++ n Integral Types n represent whole numbers and their negatives n declared as int, short, or long n Floating Types n represent real numbers with a decimal point n declared as float, or double n Character Types n represent single characters n declared as char

n Integral Constants n Positive or negative whole numbers n n n Commas are NOT allowed n Value ranges n char-128 to +127 n short-32,768 to +32,767 n int +/-2,147,483,648 n long+/-2,147,483,648

n char n One alphanumeric character n letter, number, special symbol n 'A' 'q' '8' n A number is actually stored (ASCII code)

Unsigned Integral Types n You may prepend "unsigned” before any integral type n Value ranges n unsigned char0 to 256 n unsigned short0 to n unsigned int0 to 4,294,967,295 unsigned long0 to 4,294,967,295

n Floating n integer and fraction n scientific notation n n E E4 72E0 n Types n float n double(double precision) n long double(more significant digits)

Sample Program //**************************************************************** // FreezeBoil program // This program computes the midpoint between // the freezing and boiling points of water //**************************************************************** #include int main() { const float FREEZE_PT = 32.0; // Freezing point of water const float BOIL_PT = 212.0; // Boiling point of water float avgTemp; // Holds the result of averaging // FREEZE_PT and BOIL_PT cout << "Water freezes at " << FREEZE_PT << endl; cout << " and boils at " << BOIL_PT << " degrees." << endl; avgTemp = FREEZE_PT + BOIL_PT; avgTemp = avgTemp / 2.0; cout << "Halfway between is "; cout << avgTemp << " degrees." << endl; return 0; }

Variable Declarations

What is a declaration? n Associating identifier with n data object n function n data type n So programmer n can refer to that item n by name (naming object)

What is a variable? n A location in memory n referenced by an identifier n in which a data value is stored n value can change

Example Declarations n DataType Identifier [, Identifier, …]; n int empNum; int studentCount, maxScore, sumOfScores ; n or better n int studentCount;// Description n int maxScore; // Description 2 n int sumOfScores;// etc. n best, declare when needed

What Does a Variable Declaration Do? Tells the compiler to allocate enough memory to hold value of this type associate the identifier with this location. int ageOfDog; float taxRateY2K; char middleInitial ; 4 bytes for taxRateY2K1 byte for middleInitial

When to Declare? n Declare vars when needed n NOT at top of the main block

Assignment Statements

What is an assignment statement? n A statement n gives the value of an expression n to a variable.

Valid Assignment Statements n Variable = Expression; n int num;int alpha; n double rate;char ch; n alpha = 2856; n rate = 0.36; n ch = ‘B’; n num = alpha;

Valid Expressions n constantvariable n constant and/or variables combined with operators n Examples n alpha + 2rate n 4 - alpharate n alpha * num12

Unary and Binary Operators n Unary Operator n an operator that has just one operand n +- n Binary Operator n an operator that has two operands n +-*/%

Division n Floating point division n when two real float values are divided n the result is a float n 5.0 / 3.0 = n Integer division n when two integer values are divided n the result is an integer n 5 / 3 = 1

Modulo Arithmetic (%) n % operator n yields remainder of integer division n 5 % 3 = 2 n 9 % 4 = 1 n 8 % 2 = 0 n 3 % 5 = 3

n = ? n 9n 9 n = ? n -2.7 n 2 * 3 = ? n 6n 6 n 8 / 2 = ? n 4n 4 n 8.0 / 2.0 = ? n 4.0 n 8 / 8 = ? n1n1

n 8 / 9 = ? n0n0 n 8 / 7 = ? n1n1 n 8 % 8 = ? n0n0 n 8 % 9 = ? n8n8 n 8 % 7 = ? n1n1 n 0 % 7 = ? n0n0

Valid Assignment Statements n Variable = Expression; n int num = i; int alpha = j; n alpha = num + 6; n alpha = num / 2; n num = alpha * 2; n num = 6 % alpha;

Named Constants

What is a literal value? n Any constant value written in a program. n Integers, real numbers, characters, strings of characters: n n 'D'"Hi There”

What is a named constant? n A location in memory n referenced by an identifier n in which a data value is stored n value cannot change n const DataType Identifier = LiteralValue;

n const char BLANK = ' '; n const float INTEREST_RATE = 0.12; n const int MAX = 20; n const float PI = ;

n Named constants n make program more readable n documents expressions n program easier to modify n Change tax rates in a program only once.

Output

How do you output data? n cout << "Hello”; n cout n special variable representing std output n an output stream n << n insertion operator n direction from string to output stream

n Items are separated by << n literals are in quotes, n variable names or expressions allowed. n cout << ExprOrString << ExprOrString …;

n Examples: n int x; double y; char z; n x = 1; y = 2.3; z = ‘A’; n cout << "x ="; n cout << x << "y =" << y << "z =" << z; n cout << "***"; n Output: n x =1y =2.3z =A*** n Insert white spaces in the output where needed!

x = 1; y = 2.3; z = ‘A’; cout << "x = "; cout << x << " y = " << y << " z = " << z; cout << " ***"; Output: x = 1 y = 2.3 z = A ***

n x = 1; y = 2.3; z = ‘A’; n cout << "x = " << x << endl << "y = " << y n << endl << "z = " << z << endl; Output: x = 1 y = 2.3 z = A n x = ; y = ; z = ‘B’; n cout << x << " " << y << " " << z; n Output: B

The assignment statement is not an equality. n Var = Expr; n The assignment statement n takes the value of the expression n and assigns it to the variable. n It does NOT determine if the values are equal.

Assignment Statement not equality n alpha = alpha + 1; n --Legal assignment, not equality n num = num + alpha;

Increment and Decrement n ++-- n num++; n ++num;===> num = num + 1; n num--; n --num;===> num = num - 1; n Use as standalone statements.

Strings

C++ Data Type String n a string is a sequence of characters enclosed in double quotes n string sample values “Hello” “Year 2000” “1234” the empty string (null string) contains no characters and is written as “”

More About Type String n string is not a built-in (standard) type n it is a programmer-defined data type n it is provided in the C++ standard library n #include n string operations include n comparing 2 string values n searching a string for a particular character n joining one string to another

String Concatenation (+) n concatenation is a binary operation that uses the + operator n at least one of the operands must be a string variable or named constant--the other operand can be string type or char type

Concatenation Example const string WHEN = “Tomorrow” ; const char EXCLAMATION = ‘!’ ; string message1 ; string message2 ; message1 = “Yesterday “ ; message2 = “and “ ; message1 = message1 + message2 + WHEN + EXCLAMATION ;

How does one add comments to a program? n Comments giving explanatory notes n They may appear anywhere in a program. n /* */ around text. n // Comment the rest of a line n It is good programming style to add descriptive comments to your program. n Paste document.cpp to top of program

What is a block (compound statement)? n Group of statements enclosed in { } n separated by semicolons n Used anywhere a statement is used { stmt 1; stmt 2; : stmt n; }

C++ Preprocessor n #include n Preprocessor handles include n iostream.h file is inserted into source n iostream defines n cout n endl n <<