1 Lecture-4 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 6 Chapter 3 Numeric Types, Expressions, and Output 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: 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.
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.
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.
Lecture 3: The parts of a C++ program Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems.
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
Chapter 2: Introduction to C++. Outline Basic “Hello World!!” Variables Data Types Illustration.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
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++
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++
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
Chapter 2 Creating a C++ Program. Elements of a C++ Program Four basic ways of structuring a program Four basic ways of structuring a program 1.Sequencing.
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.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
Chapter 2: Basic Elements of C++
Chapter Topics The Basics of a C++ Program Data Types
Topic Pre-processor cout To output a message.
Chapter 2 Topics Programs Composed of Several Functions
Basic Elements of C++.
Chapter 2: Basic Elements of C++
Basic Elements of C++ Chapter 2.
C++ fundamentals Lecture 1, Chapter 2 – pp /22/2018 Y K Choi.
2.1 Parts of a C++ Program.
Programming Funamental slides
Programming Funamental slides
Chapter 3: Input/Output
CS150 Introduction to Computer Science 1
Chapter 2: Introduction to C++.
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Subject:Object oriented programming
Chapter 1 c++ structure C++ Input / Output
Presentation transcript:

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

2 Revision of Topics Covered l What is included in the heading of a program? l How can we avoid a return statement in our program? l What are the rules for identifiers in C++? l What does declaration of a variable include? l What does a compiler do when it sees declaration of a variable?

3 Revision Continued l What is wrong in this program? –void main(void) { int k=45; return k; } l Can we carry out this assignment? int myTableLength; float myTableWidth; int area; area = myTableLength*myTableWidth;

4 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 “”

5 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

6 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. 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 ;

7 Giving a Value to a Variable You can assign (give) a value to a variable by using the assignment operator = VARIABLE DECLARATIONS string firstName ; char middleInitial ; char letter ; int ageOfDog; VALID ASSIGNMENT STATEMENTS firstName = “Fido” ; middleInitial = ‘X’ ; letter = middleInitial ; ageOfDog = 12 ;

8 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. 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

10 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

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

12 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

13 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... ;

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

16 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.

17 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

18 // ****************************************************** // 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

19 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; }

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