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.

Slides:



Advertisements
Similar presentations
Computer Programming Basics Assistant Professor Jeon, Seokhee Assistant Professor Department of Computer Engineering, Kyung Hee University, Korea.
Advertisements

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.
© 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.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
CS150 Introduction to Computer Science 1
Chapter 2: Introduction to C++.
Basic Elements of C++ Chapter 2.
Introduction to C++ Programming
VARIABLES, TYPES, INPUT/OUTPUT, ASSIGNMENT OPERATION Shieu-Hong Lin MATH/CS Department Chapel.
CSC 125 Introduction to C++ Programming Chapter 2 Introduction to C++
# include using namespace std; int Square ( int ); int Cube ( int ); int main () { cout
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.
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.
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.
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.
Basic Elements of C++ Chapter 1.
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.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) CSC 1201 Course at KSU1.
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
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
Today’s Lecture  Literal  Constant  Precedence rules  More assignment rules  Program Style.
General Computer Science for Engineers CISC 106 Lecture 12 James Atlas Computer and Information Sciences 08/03/2009.
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.
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 st Semester Module2 Basic C# Concept อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
1 Structure of Simple C++ Program Chapter 1 09/09/13.
1 A more complex example Write a program that sums a sequence of integers and displays the result. Assume that the first integer read specifies the number.
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
LESSON 2 Basic of C++.
Chapter 2: Introduction to C++
Bill Tucker Austin Community College COSC 1315
Chapter 2 Topics Programs Composed of Several Functions
Basic Elements of C++.
Chapter 2: Introduction to C++
Basic Elements of C++ Chapter 2.
Chapter 2 Elementary Programming
2.1 Parts of a C++ Program.
Introduction to C++ Programming
Chapter 2: Introduction to C++.
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Variables and Constants
Presentation transcript:

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 2.Selection 3.Looping 4.Sub-program

Basics All programs must have at least one function All programs must have at least one function We will learn to write many functions We will learn to write many functions We will build more complex programs from simple functions We will build more complex programs from simple functions

Parts of the Programs Identifiers Identifiers –A name associated with a function or data object –It is used to reference that function or data object

How to Name Identifiers Must start with a letter or underscore Must start with a letter or underscore Followed by a letter, number or underscores Followed by a letter, number or underscores Cannot be a reserved word Cannot be a reserved word

Quiz Write true if the identifier’s name good and false otherwise: Write true if the identifier’s name good and false otherwise:_Fred3DArrayR2D2Silly_Name Good Name xBadNameId3nt1f13rBonus:intconst_cast

Examples of Reserved Words int int double double const const return return bool bool if if while while for for

Data and Data Types Data type Data type –Determines how the data is represented in the computer and how the computer can operate on it Data Data –What all computers use to compute

Data Types Two Kinds Two Kinds –Built-in –User Defined Example of Built-in Example of Built-in –int –double –bool –char

char Data Type Any single alphanumeric character Any single alphanumeric character –Example –‘A’ –‘a’ –‘B’ –‘1’ –‘^’

string Data Type Not a built-in type Not a built-in type Any sequence of characters Any sequence of characters –Example –“Dave” “C++” “ 9 “ Strings cannot span more than one line Strings cannot span more than one line Null String Null String –“”

Naming Elements Use a Declaration Use a Declaration int empNum; int empNum; Variables Variables –A location in memory, referenced by an identifier, that contains a data value that can be changed Constants Constants –A location in memory, referenced by an identifier, that contains a data value that cannot be changed

Examples int Counter; int Counter; double taxRate, Percent; double taxRate, Percent; const char MIDDLEINITIAL = ‘P’; const char MIDDLEINITIAL = ‘P’; const bool NOTTRUE = “false”; const bool NOTTRUE = “false”;

Executable Statements Assignment Statements Assignment Statements –lastName = “Roszak”; Output Statements Output Statements –cout << “Hello World”;

Non-executable Statements Comments Comments –//This is how you indicate a comment –/*Or you can do it this way*/

Blocks Groups of lines that are to be grouped together between curly braces Groups of lines that are to be grouped together between curly braces Example Example –int main() { cout << “Hello World; }

Pre-processor Before the program is compiled, a program called a pre-processor parsers the code looking for directives; things to do Before the program is compiled, a program called a pre-processor parsers the code looking for directives; things to do Example Example –#include –#include –#ifndef –#endif

Introduction to Namespaces int main() { cout << “Happy Birthday” << endl; return 0; }

More Namespaces #include #include int main() { cout << “Happy Birthday” << endl; return 0; }

Still More #include #include int main() { std::cout << “Happy Birthday” << std::endl; return 0; }

Yet More #include #include using std::cout; using std::endl; int main() { cout << “Happy Birthday” << endl; return 0; }

One More #include #include using namespace std; int main() { cout << “Happy Birthday” << endl; return 0; }

More Output What will the following lines produce? What will the following lines produce? –cout << “Hi there. “ << endl; –cout << endl; –cout <<“What are you doing?” << endl;

More Output What will these produce? What will these produce? –cout << “Hi there. “ << endl << endl <<“What are you doing?” << endl; How about these? How about these? –cout <<“Hi there. \n\nWhat are you doing?\n”