1 C++ Syntax and Semantics The Development Process.

Slides:



Advertisements
Similar presentations
Variables in C Amir Haider Lecturer.
Advertisements

C++ Basics Variables, Identifiers, Assignments, Input/Output.
Introduction to C++ September 12, Today’s Agenda Quick Review Check your programs from yesterday Another Simple Program: Adding Two Numbers Rules.
Dale/Weems/Headington
Types and Variables. Computer Programming 2 C++ in one page!
Structure of a C program
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
Data types and variables
Chapter 2: Introduction to C++.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the structure of a C-language program. ❏ To write your first 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.
Basic Elements of C++ Chapter 2.
CSC 125 Introduction to C++ Programming Chapter 2 Introduction to C++
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.
Input & Output: Console
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
1 C++ Syntax and Semantics, and the Program Development Process.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Course Title: Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 03 Conditional statement 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER)
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems.
BASICS CONCEPTS OF ‘C’.  C Character Set C Character Set  Tokens in C Tokens in C  Constants Constants  Variables Variables  Global Variables Global.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
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.
CONSTANTS Constants are also known as literals in C. Constants are quantities whose values do not change during program execution. There are two types.
CHAPTER 2 C++ SYNTAX & SEMANTICS #include using namespace std; int main() { cout
1 Week 5 l Primitive Data types l Assignment l Expressions l Documentation & Style Primitive Types, Assignments, and Expressions.
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.
1 09/03/04CS150 Introduction to Computer Science 1 What Data Do We Have.
Types Chapter 2. C++ An Introduction to Computing, 3rd ed. 2 Objectives Observe types provided by C++ Literals of these types Explain syntax rules for.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
Introduction to Algorithmic Processes CMPSC 201C Fall 2000.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
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 1.2 Introduction to C++ Programming
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Variables A variable is a placeholder for a value. It is a named memory location where that value is stored. Use the name of a variable to access or update.
Basic Elements of C++.
Chapter 2: Introduction to C++
Basic Elements of C++ Chapter 2.
IDENTIFIERS CSC 111.
Chapter 2 Elementary Programming
Tejalal Choudhary “Computer Programming” Fundamentals of “C”
2.1 Parts of a C++ Program.
Introduction to C++ Programming
Variables in C Topics Naming Variables Declaring Variables
CS111 Computer Programming
C++ Data Types Data Type
Chapter # 2 Part 2 Programs And data
Chapter 2: Introduction to C++.
Variables in C Topics Naming Variables Declaring Variables
Variables in C Topics Naming Variables Declaring Variables
Variables and Constants
Variables in C Topics Naming Variables Declaring Variables
Presentation transcript:

1 C++ Syntax and Semantics The Development Process

2 Why Study This Chapter? To be able to create identifiers To declare variables and constants To write assignment and I/O statements To design and write simple programs

3 C++ Program Structure 8All sub programs are called functions 8Every program has a function called main( ) 8Other modules (sub programs or functions) are declared by the programmer

4 Syntax & Semantics 8Syntax The formal rules governing how valid instructions are written in a programming language 8Semantics the set of rules determining the meaning of instructions written in the programming language

5 Naming Identifiers 8Identifiers are used to name things 8Made up of 8letters (upper, lower case) 8numerals (0-9) 8under score _ 8Must begin with letter or underscore

6 Style Issues 8Use meaningful identifiers 8makes the program more self documenting 8Use readable identifiers

7 Identifiers 8Check whether legal and/or readable

8 Data and Data Types 8Distinction made between integers, characters, rationals, etc. 8Data type specific set of data values along with a set of operations on those values Simple - integers - float - char Structured - struct - array - union - class Address - reference - pointers

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

10 Integer Types 8Whole numbers with no fractional part No commas allowed 8Other variations of integer 8short, long 8different sizes available (for memory use) 8Also char ‘a’ ‘Z’ ‘8’ ‘%’ ‘$’

11 Floating Point Types 8Represent rational numbers 8float, double, long double 8Stored in the computer in scientific notation +3.94x10 -3 Leading sign Significant digits Sign of power of ten Power of ten

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

13 Declarations 8Statement in a program that associates a name (an identifier) with a memory location 8Use the name to access or alter the contents of the memory location

14 Variables 8Characteristics 8a location in memory 8referenced by an identifier 8contents of the location can be changed 8Example: 8int x, y = 0; x : ? y : 0 Unknown or “garbage” value for x Value for y initialized at declaration

15 Variables 8Characteristics 8a location in memory 8referenced by an identifier 8contents of the location can be changed 8Example: 8int x, y = 0; x = 5; x : 5 y : 0

16 Variables 8Characteristics 8a location in memory 8referenced by an identifier 8contents of the location can be changed 8Example: 8int x, y = 0; x = 5; y = 3; x : 5 y : 3 Old value of 0 lost

17 Variables 8Characteristics 8a location in memory 8referenced by an identifier 8contents of the location can be changed 8Example: 8int x, y = 0; x = 5; y = 3; x = y + 7; x : 10 y : 3 Value stored in y accessed, added to 7, result stored in x

18 Using Named Constants 8Characteristics 8a location in memory 8referenced by an identifier 8value cannot be changed 8Example const int lines_per_page = 66;

19 Using Named Constants 8Characteristics 8a location in memory 8referenced by an identifier 8value cannot be changed 8Example const int lines_per_page = 66; lines_per_page = 123; ERROR  Cannot alter a constant ERROR  Cannot alter a constant

20 Style : Capitalization of Identifiers 8Used as a visual clue to what an identifier represents 8Standard for our text Variables: begin with lower case, cap successive words Functions: begin with upper case, cap successive words Named Constants: all caps, use underscore between words

21 Executable Statements 8Output : send results of calculations, etc. to screen, printer, or file 8Example:

22 Executable Statements 8Output : send results of calculations, etc. to screen, printer, or file 8Alternate Example: Only a single cout statement used. Repeat use of << operator

23 8Variable cout is predefined to denote an output stream that goes to the standard output device (display screen). 8The insertion operator << called “put to” takes 2 operands. 8The left operand is a stream expression, such as cout. The right operand is an expression of simple type or a string constant. Insertion Operator ( << )

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

25 Program Comments 8Purpose 8for the human reader 8the compiler ignores 8Your programs should contain info as shown: /* comments between */ // Comments follow

26 Program Construction 8Shown is a typical program with one function, the main( ) function Variable declaration Assignment statement Output statement Return for int function main( )

27 Compound Statements 8Body of a function is an example of a block or compound statement 8They are enclosed between a pair of curly brackets { }

28 The C++ Preprocessor  #include statements tell the preprocessor where to get certain declarations 8Preprocessor runs before the compiler 8All preprocessor commands preceded by # sign

29 Program Entry and Execution 8Source code is a text file created by text editor 8Program is compiled 8Compiler notifies you of syntax (and some semantic) errors 8Program is linked 8Code from #include’s is linked to your compiled code 8Program is then run. You must check for remaining semantic, logic errors

30 Testing and Debugging Hints 8Watch for misspelled or undeclared identifiers 8C++ is case sensitive, watch for improper Caps 8Watch for integer division int_x / int_y yields an integer result 8Don’t confuse 0’s (zeros) with O’s in your source code 8Make sure statements end with semicolons ;