1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Programming in C++

Slides:



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

Dale/Weems/Headington
Dale/Weems/Headington
1 9/10/07CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
1 Lecture-4 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
© 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.
1 Chapter 3 Topics Constants of Type int and float l Evaluating Arithmetic Expressions l Implicit Type Coercion and Explicit Type Conversion l Calling.
CS150 Introduction to Computer Science 1
Chapter 2: Introduction to C++.
Chapter 2: Basic Elements of C++
Admin Office hours 2:45-3:15 today due to department meeting if you change addresses during the semester, please unsubscribe the old one from the.
Data Types, Expressions and Functions (part I)
Basic Elements of C++ Chapter 2.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
CSC 125 Introduction to C++ Programming Chapter 2 Introduction to C++
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects Seventh.
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.
1 Numeric Types, Expressions, and Output. 2 Chapter 3 Topics  Constants of Type int and float  Evaluating Arithmetic Expressions  Declaration for Numeric.
Numeric Types, Expressions, and Output 1. Chapter 3 Topics Constants of Type int and float Evaluating Arithmetic Expressions Implicit Type Coercion and.
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems.
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems.
1 Chapter 3 Numeric Types, Expressions, and Output CS185/09 - Introduction to Programming Caldwell College.
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.
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
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/Headington.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems.
C++ Programming: Basic Elements of C++.
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
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.
1 Chapter 3 Numeric Types, Expressions, and Output.
THE BASICS OF A C++ PROGRAM EDP 4 / MATH 23 TTH 5:45 – 7:15.
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.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects Sixth.
CHAPTER 2 C++ SYNTAX & SEMANTICS #include using namespace std; int main() { cout
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
Programming Fundamentals
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.
C++ Data Types Check sample values of? ‘4’
Chapter 3 Numeric Types, Expressions, and Output.
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++
1 A Simple “Hello World” Example #include // input-output library using namespace std; int main() // function main { cout
Introduction to Algorithmic Processes CMPSC 201C Fall 2000.
1 C Syntax and Semantics Dr. Sherif Mohamed Tawfik Lecture Two.
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
Bill Tucker Austin Community College COSC 1315
Chapter 3 Numeric Types, Expressions, and Output
Chapter Topics The Basics of a C++ Program Data Types
Chapter 2: Introduction to C++
BASIC ELEMENTS OF A COMPUTER PROGRAM
Chapter 2 Topics Programs Composed of Several Functions
Basic Elements of C++.
Basic Elements of C++ Chapter 2.
2.1 Parts of a C++ Program.
Chapter 2: Introduction to C++.
C++ Programming Basics
Subject:Object oriented programming
Presentation transcript:

1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Programming in C++

2 End of Line Manipulator l Two ways of doing this l cout <<“The State of Pennsylvania\n” << “is where Penn State University is located.\n”; l cout <<“The State of Pennsylvania” << endl <<“is where Penn State University is located.” << endl; l Good programming is to always end your program with a \n or endl

3 Identifiers l Identifiers (i.e. variables) are the very core of programming languages l Programs are constructed so as to manipulate numbers, letter, and characters l Identifiers (variables) hold numbers usually

4 Identifiers l The strength of a identifier is that its value is easily changed l Identifiers are case sensitive n forest is different from forEst l Good programming uses identifiers that relate to the data and the program l Goof programming uses a consistent method of creating identifiers

5 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 age_of_dogTaxRate98 PrintHeading AgeOfHorse NOT VALID (Why?) age#98TaxRateAge-Of-Cat

6 More about Identifiers l Some C++ compilers recognize only the first 32 characters of an identifier as significant. l Then these identifiers are considered the same: Age_Of_This_Old_Rhinoceros_At_My_Zoo Age_Of_This_Old_Rhinoceros_At_My_Safari l What about these? Age_Of_This_Old_Rhinoceros_At_My_Zoo AgE_Of_This_Old_Rhinoceros_At_My_Zoo

7 More about Identifiers l C++ has certain reserved words that have a predetermined meaning and are not be be used as identifiers n e.g. int, long, float, return, short n see Appendix A of text for a list of these reserved words n “do not” use reserved words as identifiers, else there will be a programming problem

8 Identifiers l Every identifier (variable) in a C++ program must be declared before it is used n before being used n at the start of the “main” part of the program l Separate declarations of variables by a comma when they are in the same statement

9 C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double

10 C++ Simple Data Types Simple types Integral Floating char short int long enum float double long double unsigned

11 Fundamental 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 Type n can be letters, single digits, symbols, punctuation n declared as char

12 Samples of C++ Data Values int sample values double sample values E-3-95E E2 char sample values ‘ B ’ ‘ d ’ ‘ 4 ’‘ ? ’‘ * ’

13 Scientific Notation 2.7E4 means 2.7 x 10 4 = = E-4 means 2.7 x = =

14 Size of Varibles l Program to calculate size of allowed variables l short int 2 bytes -32,768 to 32,767 l long int 4 bytes -2,147,483,648 to 2,147,483,648 l float 4 bytes 1.2e-38 to 3.4e38 l double 8 bytes 2.2e-308 to 1.8e308

15 Program Variable Sizes l //Finding the size of varible types used by your computer l #include l int main() l { l cout << "The size of an int is:\t\t" << sizeof(int) l << " bytes.\n"; l cout << "The size of a short int is:\t\t" << sizeof(short) l << " bytes.\n"; l cout << "The size of a long int is:\t\t" << sizeof(long) l << " bytes.\n"; l cout << "The size of a char is:\t\t" << sizeof(char) l << " bytes.\n"; l cout << "The size of a float is:\t\t" << sizeof(float) l << " bytes.\n"; l cout << "The size of a double is:\t\t" << sizeof(double) l << " bytes.\n"; l return 0;

16 More About Floating Point Values l Floating point numbers have an integer part and a fractional part, with a decimal point in between. Either the integer part or the fractional part, but not both, may be missing. EXAMPLES l Alternatively, floating point values can have an exponent, as in scientific notation. The number preceding the letter E doesn’t need to include a decimal point. EXAMPLES 1.84E1 5E2 8E E3

17 Understanding Types l Is 5,123 a valid integer constant? n No, commas are not allowed l Is 5.8e24 a valid floating point constant? n Yes l Is 0.0 a valid floating point constant? n Yes l Is “Float” a reserved word? n No, but “float” is

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.

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 Age_Of_Dog; float TaxRate98; char MiddleInitial ; 4 bytes for TaxRate981 byte for MiddleInitial

20 Giving a value to a variable In your program you can assign (give) a value to the variable by using the assignment operator = Age_Of_Dog = 12; or by another method, such as cout << “How old is your dog?”; cin >> Age_Of_Dog;

21 What is a Named Constant? l A named constant is a location in memory which we can refer to by an identifier, and in which a data value that cannot be changed is stored. VALID CONSTANT DECLARATIONS const char STAR = ‘*’ ; const float NORMAL_TEMP = 98.6 ; const float PI = ; const float TAX_RATE = ; const int MAX_SIZE = 50 ; const int VOTING_AGE = 18 ;

22 Simplest C++ Program int main (void) { return 0; } type of returned value name of function says no parameters

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

24 main Returns an Integer to the Operating System //*************************************************************************** // FreezeBoil program // This program computes the midpoint between // the freezing and boiling points of water //*************************************************************************** #include const float FREEZE_PT = 32.0 ; // Freezing point of water const float BOIL_PT = ; // Boiling point of water int main ( void ) { float avgTemp ; // Holds the result of averaging // FREEZE_PT and BOIL_PT

25 Function main Continued 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 ; }

26 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 include directory for the header file called iostream.h and insert its contents into your source code.

27 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 cin and cout.

28 Mileage Program /* This program computes miles per gallon given four amounts for gallons used, and starting and ending mileage. Constants: The gallon amounts for four fillups. The starting mileage. The ending mileage. Output (screen) The calculated miles per gallon */ #include

29 C++ Code Continued const float AMT1 = 11.7 ; // Number of gallons for fillup 1 const float AMT2 = 14.3 ; // Number of gallons for fillup 2 const float AMT3 = 12.2 ; // Number of gallons for fillup 3 const float AMT4 = 8.5 ; // Number of gallons for fillup 4 const float START_MILES = ; // Starting mileage const float END_MILES = ; // Ending mileage int main(void) { float mpg ; // Computed miles per gallon mpg = (END_MILES - START_MILES) / (AMT1 + AMT2 + AMT3 + AMT4) ;

30 main Returns an Integer Value to the Operating System cout << “For the gallon amounts “ << endl ; cout << AMT1 << ‘ ‘ << AMT2 << ‘ ‘ << AMT3 << ‘ ‘ << AMT4 << endl ; cout << “and a starting mileage of “ << START_MILES << endl ; cout << “and an ending mileage of “ << END_MILES << endl ; cout << “the mileage per gallon is “ << mpg << endl ; return 0; }

31 What is an Expression in C++? l An expression is any valid combination of operators and operands. l In C++ each expression has a value. l The value of the expression 9.3 * 4.5 is 41.85

32 Division Operator l The result of the division operator depends on the type of its operands. l If one or both operands has a floating point type, the result is a floating point type. Otherwise, the result is an integer type. l Examples 11 / 4 has value / 4.0 has value / 4.0 has value 2.75

33 Modulus Operator l The modulus operator % can only be used with integer type operands and always has an integer type result. l Its result is the integer type remainder of an integer division. l EXAMPLE 11 % 4 has value 3 because ) 4 11 R = ?

34 More C++ Operators 8 int Age; Age = 8; Age = Age + 1; Age 9

35 PREFIX FORM Increment Operator 8 int Age; Age = 8; ++Age; Age 9

36 POSTFIX FORM Increment Operator 8 int Age; Age = 8; Age++; Age 9

37 Decrement Operator 100 int Dogs; Dogs = 100; Dogs--; Dogs 99 Dogs

38 Which form to use?? l When the increment (or decrement) operator is used in a “stand alone” statement solely to add one (or subtract one) from a variable’s value, it can be used in either prefix or postfix form. Dogs-- ; --Dogs ; USE EITHER

39 BUT... l When the increment (or decrement) operator is used in a statement with other operators, the prefix and postfix forms can yield different results. WE’LL SEE HOW LATER...

40 Insertion Operator ( << ) l Variable cout 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.

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

42 Function Concept in Math f ( x ) = 5 x - 3 When x = 1, f ( x ) = 2 is the returned value. When x = 4, f ( x ) = 17 is the returned value. Returned value is determined by the function definition and by the values of any parameters. Name of function Parameter of function Function definition

43 A C++ program is a collection of one or more functions l There must be a function called main( ) l Execution 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.

44 Program with several functions main( ) function Square( ) function Cube( ) function

45 Program with Three Functions #include int Square ( int ) ; // declares these 2 functions int Cube ( int ) ; int main ( void ) { cout << “The square of 27 is “ << Square (27) << endl ; // function call cout << “The cube of 27 is “ << Cube (27) << endl ; // function call return 0 ; }

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

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

48 Before Next Class l Go to a computer lab and type in and run mileage program on page 49 of text l Try programming and running program exercise 3 on page 58 of text

49 What Next Class Will Cover l Any questions from last class session l Complete chapter 2 l Class discussion of review questions for better understanding of chapter 2 l Programming examples provided l Recommended Chapter 2 practice questions