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

Slides:



Advertisements
Similar presentations
Dale/Weems/Headington
Advertisements

C++ Data Type String A string is a sequence of characters enclosed in double quotes. Examples of strings: “Hello” “CIS 260” “Students” The empty string.
1 Lecture-4 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
Chapter 2: Basic Elements of C++
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
Chapter 2: Basic Elements of C++
Chapter 2: Basic Elements of C++
Chapter 2: Introduction to C++.
How to Program in C++ CHAPTER 3: INPUT & OUTPUT INSTRUCTOR: MOHAMMAD MOJADDAM.
Chapter 2: Basic Elements of C++
Basic Elements of C++ Chapter 2.
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.
C++ Basics Structure of a Program. C++ Source Code Plain text file Typical file extension .CPP Must compile the C++ source code without errors before.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
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.
Chapter 2 Overview of C++. A Sample Program // This is my first program. It calculates and outputs // how many fingers I have. #include using namespace.
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.
Week 1 Algorithmization and Programming Languages.
C++ Programming: Basic Elements of C++.
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++
Syntax and Semantics, and the Program Development Process ROBERT REAVES.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process.
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
CHAPTER 2 C++ SYNTAX & SEMANTICS #include using namespace std; int main() { cout
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Basic Elements of C++
Chapter 2: Introduction to C++. Outline Basic “Hello World!!” Variables Data Types Illustration.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
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: 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.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 2: Basic Elements of C++
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
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
1 C Syntax and Semantics Dr. Sherif Mohamed Tawfik Lecture Two.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
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.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 3: Input/Output Samples.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
Bill Tucker Austin Community College COSC 1315
Chapter 2: Basic Elements of C++
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1.2 Introduction to C++ Programming
Chapter 2 Topics Programs Composed of Several Functions
Basic Elements of C++.
Chapter 2: Basic Elements of C++
Basic Elements of C++ Chapter 2.
2.1 Parts of a C++ Program.
Chapter 3: Input/Output
Chapter 2: Introduction to C++.
Subject:Object oriented programming
Presentation transcript:

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

2 A C++ program is a collection of one or more functions l Subprograms – small parts make up the big program – function – work together l there must be a function called main( ) – master – called by OS – return 0 l Value returning functions l execution always begins with the first statement in function main( ) l any other functions in your program are subprograms and are not executed until they are called – do a particular task

3 l Syntax and Semantics n Syntax – grammar, semantics - meaning n Set of rules, symbols, special words – construct a program n Metalanguage – simple, precise, formal n Constructs – basic building blocks of the language n Templates – generic example

4 Program With Several Functions main function square function cube function

5 Program With Three Functions #include int Square( int ); // declares these two int Cube( int ); // value-returning functions using namespace std ; int main( ) { cout << “The square of 27 is “ << Square(27) << endl;// function call cout << “The cube of 27 is “ << Cube(27) << endl; // function call return 0; }

6 Rest of Program int Square( int n ) { return n * n; } int Cube( int n ) { return n * n * n; }

7 Output of program The square of 27 is 729 The cube of 27 is 19683

8 Shortest C++ Program int main ( ) { return 0; } type of returned value name of function

What is in a heading? int main ( ) type of returned value name of function says no parameters

10 l a block is a sequence of zero or more statements enclosed by a pair of curly braces { } SYNTAX { Statement (optional)... } Block (Compound Statement)

Every C++ function has 2 parts int main ( ) heading { body block return 0; }

12 What is an Identifier? l An identifier is the name used for a data object (a variable or a constant), or for a function, in a C++ program. l C++ is a case-sensitive language. l using meaningful identifiers is a good programming practice l Declarations are not executed – help compiler translate into machine language

13 Identifiers l an identifier must start with a letter or underscore, and be followed by zero or more letters (A-Z, a-z), digits (0-9), or underscores l VALID iAge_of_dogfTaxRateY2K sPrintHeading iAgeOfHorse NOT VALID (Why?) iAge# 2000TaxRate iAge-Of-Cat

14 More About Identifiers l Meaningful, readable – meaningless to the computer, but not to us l First letter prefix to indicate the type l Capitalize the first letter of each word l Ie. cLetter, fBalanceSales, iCountStudents l Data types – variables change, constants do not l Different amounts of memory l Difference – 0 and ‘0’ – stored differently

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

16 Standard Data Types in C++ l Integral Types n represent whole numbers and their negatives n declared as int, short, or long l Floating Types n represent real numbers with a decimal point n declared as float, or double l Character Types n represent single characters – ‘L’ n declared as char n ASCII vs. Unicode

17 Samples of C++ Data Values int sample values float sample values char sample values ‘ B ’ ‘ d ’ ‘ 4 ’ ‘ ? ’‘ * ’

18 What is a Variable? l A variable is a location in memory which we can refer to by an identifier, and in which a data value that can be changed is stored. unique and represent one thing l declaring a variable means specifying both its name and its data type

19 What Does a Variable Declaration Do? A declaration tells the compiler to allocate enough memory to hold a value of this data type, and to associate the identifier with this location. int iAgeOfDog; float fTaxRateY2K; char cMiddleInitial ; 4 bytes for fTaxRateY2K1 byte for cMiddleInitial

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

21 More About Type String l 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 l string operations include n comparing 2 string values n searching a string for a particular character n joining one string to another - concatenation

22 What is a Named Constant? l 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 is stored. l ALL CAPS - name VALID CONSTANT DECLARATIONS const string STARS = “****” ; const float NORMAL_TEMP = 98.6 ; const char BLANK = ‘ ’ ; const int VOTING_AGE = 18 ; const float MAX_HOURS = 40.0 ;

23 Giving a Value to a Variable You can assign (give) a value to a variable by using the assignment operator = VARIABLE DECLARATIONS string sFirstName ; char cMiddleInitial ; char cLetter ; int iAgeOfDog; VALID ASSIGNMENT STATEMENTS sFirstName = “Fido” ; cMiddleInitial = ‘X’ ; cLetter = cMiddleInitial ; iAgeOfDog = 12 ;

24 What is an Expression in C++? l An expression is a valid arrangement of variables, constants, and operators. l in C++ each expression can be evaluated to compute a value of a given type l the value of the expression is 14

Variable = Expression First, Expression on right is evaluated. Then the resulting value is stored in the memory location of Variable on left. Left gets right NOTE: An automatic type coercion occurs after evaluation but before the value is stored if the types differ for Expression and Variable Assignment Operator Syntax

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

27 Concatenation Example const string WHEN = “Tomorrow” ; const char EXCLAMATION = ‘!’ ; string sMessage1 ; string sMessage2 ; sMessage1 = “Yesterday “ ; sMessage2 = “and “ ; sMessage1 = sMessage1 + sMessage2 + WHEN + EXCLAMATION ;

28 Insertion Operator ( << ) l variable cout (see out) is predefined to denote an output stream that goes to the standard output device (display screen) l the insertion operator << called “put to” takes 2 operands l the left operand is a stream expression, such as cout. The right operand is an expression of simple type or a string constant being output l Endl – end of line – to start a new line

29 Output Statements SYNTAX These examples yield the same output: cout << “The answer is “ ; cout << 3 * 4 ; cout << “The answer is “ << 3 * 4 ; cout << Expression << Expression... ;

30 Is compilation the first step? No. Before your source program is compiled, it is first examined by the preprocessor to n remove all comments from source code n handle all preprocessor directives--they begin with the # character such as #include –tells preprocessor to look in the standard included directory for the header file called iostream and insert its contents into your source code

No I/O is built into C++ l Instead, a library provides an output stream Screen executing program ostream

32 Using Libraries l A library has 2 parts Interface (stored in a header file) tells what items are in the library and how to use them. Implementation (stored in another file) contains the definitions of the items in the library. l #include Refers to the header file for the iostream library needed for use of cout and endl.

33 l Comments – documentation – 2 ways l Program construction n comments n First line starts with # - preprocessor – before compiled – header files, C++ library –Insert the two files: l Iostream – for output, ie. cout l String – programmer-defined string data type n Blocks – compound statements enclosed in { }, always indent, semi-colon at end

34 l C++ preprocessor, compiler n Inserts contents of header files into program – acts as a filter during compilation –Line starting with # - not C++ statement but a preprocessor directive – - tell the preprocessor which file n Source program passes through preprocessor l Namespaces – declares all identifiers in std n can be accessed only by statements in that block – two ways

35 l Output – horizontal and vertical control n Blank lines – endl endl n Horizontal spacing l Entering a program – create, save, and modify l Compile and run – translate and execute n Syntax errors n Logic errors l Testing and debugging

36 // ****************************************************** // PrintName program // This program prints a name in two different formats // ****************************************************** #include // for cout and endl #include // for data type string using namespace std; const string FIRST = “Herman”; // Person’s first name const string LAST = “Smith”; // Person’s last name const char MIDDLE = ‘G’; // Person’s middle initial C++ Program

37 C++ Code Continued int main( ) { string firstLast; // Name in first-last format string lastFirst; // Name in last-first format firstLast = FIRST + “ “ + LAST ; cout << “Name in first-last format is “ << endl << firstLast << endl; lastFirst = LAST + “, “ + FIRST + ’ ’ ; cout << “Name in first-last format is “ << endl << lastFirst << MIDDLE << ’.’ << endl; return 0; }

38 Output of Program Name in first-last format is Herman Smith Name in last-first-initial format is Smith, Herman G.

39 l Define constants n MSG1 = In my grandmother’s trunk, I packed a flower n MSG2 = and a shirt n MSG3 = and a cup n MSG4 = and a blue marble n MSG5 = and a ball l Start with null string n sMsgTotal = “” l First turn n sMsgTotal = MSG1 n Display sMsgTotal and.

40 l Second turn n sMsgTotal = sMsgTotal + “ “ + MSG2 n Display sMsgTotal and. l Third turn n sMsgTotal = sMsgTotal + “ “ + MSG3 n Display sMsgTotal and. l Fourth turn n sMsgTotal = sMsgTotal + “ “ + MSG4 n Display sMsgTotal and. l Fifth turn n sMsgTotal = sMsgTotal + “ “ + MSG5 n Display sMsgTotal and.

41 Assignment #3 l #7-pg compile and run - hand in code and printscreen; Exam Prep Exercises, pg ,2,4,6,8,9,

42 Assignment #4 l Finish program #2 – pg. 67 – design, code, output