IXA 1234: C++ PROGRAMMING CHAPTER 2: PROGRAM STRUCTURE.

Slides:



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

C++ Basics Variables, Identifiers, Assignments, Input/Output.
Dale/Weems/Headington
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.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Chapter 2: Introduction to C++.
Chapter 2: Basic Elements of C++
1 Chapter 2 Java Syntax and Semantics, Classes, and Objects.
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.
COMPUTER PROGRAMMING. Data Types “Hello world” program Does it do a useful work? Writing several lines of code. Compiling the program. Executing the program.
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
C Tokens Identifiers Keywords Constants Operators Special symbols.
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.
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.
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.
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++.
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.
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.
CC112 Structured Programming Lecture 2 1 Arab Academy for Science and Technology and Maritime Transport College of Engineering and Technology Computer.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process.
A first program 1. #include 2. using namespace std; 3. int main() { 4. cout
Chapter 3 – Variables and Arithmetic Operations. Variable Rules u Must declare all variable names –List name and type u Keep length to 31 characters –Older.
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
Chapter 2: Introduction to C++. Language Elements Keywords Programmer-defined symbols (identifiers) Operators Punctuation Syntax Lines and Statements.
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.
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++
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
1 CSC 1111 Introduction to Computing using C++ C++ Basics (Part 1)
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 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.
CS221 C++ Basics. C++ Data Types structured array struct union class address pointer reference simple integral char short int long bool floating float.
1 A Simple “Hello World” Example #include // input-output library using namespace std; int main() // function main { cout
A Sample Program #include using namespace std; int main(void) { cout
1 C Syntax and Semantics Dr. Sherif Mohamed Tawfik Lecture Two.
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
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 Topics The Basics of a C++ Program Data Types
Chapter 1.2 Introduction to C++ Programming
BASIC ELEMENTS OF A COMPUTER PROGRAM
Documentation Need to have documentation in all programs
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.
Pointers, Dynamic Data, and Reference Types
Variables T.Najah Al_Subaie Kingdom of Saudi Arabia
Chapter 2: Introduction to C++.
Subject:Object oriented programming
Presentation transcript:

IXA 1234: C++ PROGRAMMING CHAPTER 2: PROGRAM STRUCTURE

PROGRAM STRUCTURE- S HORTEST C++ P ROGRAM 2 int main() { return 0; } type of returned value name of function

PROGRAM STR-CONT’ W HAT IS IN A HEADING ? int main( ) type of returned value name of function says no parameters

PROGRAM STR-CONT’ B LOCK (C OMPOUND S TATEMENT ) A block is a sequence of zero or more statements enclosed by a pair of curly braces { } SYNTAX { Statement (optional). }

PROGRAM STR-CONT’ E VERY C++ FUNCTION HAS 2 PARTS int main() heading { body block return 0; }

IDENTIFIER-WHAT IS IT? An identifier is the name used for a data object(a variable or a constant), or for a function, in a C++ program Beware: C++ is a case-sensitive language Using meaningful identifiers is a good programming practice

IDENTIFIERS –CONT’ 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 VALID age_of_dogtaxRateY2K PrintHeading ageOfHorse NOT VALID (Why?) age# 2000TaxRate Age-Of-Cat

C++ BASIC DATA TYPES 8 structured array struct union class address pointer reference simple integral enumfloating float double long double char short int long bool

S IMPLE DATA TYPE simple types integralfloating char short int long bool enum float double long double unsigned

DATA TYPE – CONT’ S AMPLES OF C++ D ATA V ALUES int sample values float sample values char sample values ‘ B ’ ‘ d ’ ‘ 4 ’ ‘ ? ’‘ * ’

VARIABLES – W HAT IS A V ARIABLE A variable is a location in memory that can be referred to by an identifier and in which a data value that can be changed is stored Declaring a variable means specifying both its name and its data type

V ARIABLE – CONT ’ W HAT D OES A V ARIABLE D ECLARATION D O ? int ageOfDog; float taxRate; char middleInitial ; 4 bytes for taxRateY2K1 byte for middleInitial 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

DATA TYPE-CONT’ C++ D ATA T YPE S TRING A string is a sequence of characters enclosed in double quotes Sample string values “Hello” “Year 2000” “1234” The empty string(null string)contains no characters and is written as “”

VARIABLE – CONT’ NAMED CONSTANT A named constant is a location in memory that can be referred 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;

VARIABLE-GIVING A VALUE 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;

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

INSERTION OPERATOR- O UTPUT S TATEMENTS SYNTAX These examples yield the same output: cout << “The answer is “; cout << 3 * 4; cout << “The answer is “ << 3 * 4; cout << Expression << Expression...;

E XAMPLES – EXERCISE // ****************************************************** // 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

CODING-CONT’ 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; }

OUTPUT Name in first-last format is Herman Smith Name in last-first-initial format is Smith, Herman G.