Chapter 2: Introduction to C++. Language Elements Keywords Programmer-defined symbols (identifiers) Operators Punctuation Syntax Lines and Statements.

Slides:



Advertisements
Similar presentations
Chapter 2: Basic Elements of C++
Advertisements

Announcements Quiz 1 Next Week. int : Integer Range of Typically -32,768 to 32,767 (machine and compiler dependent) float : Real Number (i.e., integer.
1 9/1/06CS150 Introduction to Computer Science 1 What Data Do We Have? CS 150 Introduction to Computer Science I.
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
Correction of the Handout #include //Preprocessor using namespace std; int main (){ ………….. return 0; } A namespace is a named group of definitions. When.
CS150 Introduction to Computer Science 1
Chapter 2: Introduction to C++.
Introduction to C Programming
Basic Elements of C++ Chapter 2.
Introduction to C++ Programming
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
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.
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.
Chapter 2. C++ Program Structure C++ program is a collection of subprograms Subprograms in C++ are called FUNCTIONS Each function performs a specific.
Basic Elements of C++ Chapter 1.
Lecture 3: The parts of a C++ program Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Week 1 Algorithmization and Programming Languages.
C++ Programming: Basic Elements of C++.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Introduction to C++ Basic Elements of C++. C++ Programming: From Problem Analysis to Program Design, Fourth Edition2 The Basics of a C++ Program Function:
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 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 C++ SYNTAX & SEMANTICS #include using namespace std; int main() { cout
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.
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++
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Lecture 5 Computer programming -1-. Input \ Output statement 1- Input (cin) : Use to input data from keyboard. Example : cin >> age; 2- Output (cout):
Literals A literal (sometimes called a constant) is a symbol which evaluates to itself, i.e., it is what it appears to be. Examples: 5 int literal
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –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 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.
1 8/30/06CS150 Introduction to Computer Science 1 Your First C++ Program.
A Sample Program #include using namespace std; int main(void) { cout
Intro. to Computer Programming Eng. Nehal A. Mohamed Spring Semester-2016.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
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
Bill Tucker Austin Community College COSC 1315
Chapter 1.2 Introduction to C++ Programming
Chapter 2: Basic Elements of C++
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1.2 Introduction to C++ Programming
Basic Elements of C++ Chapter 1.
Chapter 1.2 Introduction to C++ Programming
What Actions Do We Have Part 1
Chapter 2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 2: Introduction to C++
Basic Elements of C++.
Basic Elements of C++ Chapter 2.
Chapter 2 Elementary Programming
CS 1430: Programming in C++ Turn in your Quiz1-2 No time to cover HiC.
2.1 Parts of a C++ Program.
Introduction to C++ Programming
Variables, Identifiers, Assignments, Input/Output
Chapter # 2 Part 2 Programs And data
Chapter 2: Introduction to C++.
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Presentation transcript:

Chapter 2: Introduction to C++

Language Elements Keywords Programmer-defined symbols (identifiers) Operators Punctuation Syntax Lines and Statements Variables (Memory locations)

Parts of a C++ Program Comments//A simple program Preprocessor directives #include Namespacesusing namespace std; Main functionint main ( ) Braces{ and } Output statementscout << “Programming is fun”; String literals (constants) “Programming is fun”

Parts of a C++ Program Semicolons (end statements) ; Return statementreturn 0; Special characters// # ( ) { } “ “ ; Must put the parts together correctly!

// A simple C++ program #include using namespace std; int main() { cout << "Programming is great fun!"; return 0; }

The cout Object cout outputs to standard output (monitor) cout is a stream object – works with streams of data –data flows from your program to the monitor Uses the << operator (inserts data into the stream cout) Examples: –cout << “Programming is great fun. “; –cout << “Programming is “ << “great fun.”; –cout << “Programming is “; cout << “great fun.”;

More Examples using cout –Using endl cout << “Select an option below” << endl; cout << “Ham” << endl; cout << “Tuna” << endl; –Using escape sequences cout << “Select an option below\n” ; cout << “\tHam\n” ; cout << “\tTuna\n” ;

2.4 Standard and PreStandard C++ We’ll use standard C++ We’ll use namespaces

2. 5 Variables, Constants and Assignment Variables represent storage locations in computer’s memory whose values can vary (or change). Constants represent storage locations in computer’s memory whose values cannot change (but must stay constant). An assignment statement is ONE way we can store values in variables.

// This program has a variable. #include using namespace std; int main() { int number; number = 5; cout << "The value of number is " << "number" << endl; cout << "The value of number is " << number << endl; number = 7; cout << "Now the value of number is " << number << endl; return 0; } Variable definition An assignment of a value to a variable

Variable definition –Tells the computer the name and type of data a memory location will hold. –Usually place at the beginning of the main function –Note: They end with a ; Variables have several attributes –Name –Type –Value –purpose

Assignment Statement Made up three parts –Variable on the left-hand side that will get a new value –Assignment operator (=) –Expression on the right-hand side that is evaluated Examples: number = 5; average = sum / 10;

Input cin inputs from standard input (keyboard) cin is a stream object – works with streams of data –data flows from the keyboard to the program Uses the >> operator (removes data from the stream cin to store in a variable) Usually preceded by a prompt. Example: –cout << “Enter a number “; //note: no endl –cin >> number;

Constants Literal constants –“Programming is fun.” –5 –10 – Defined constants –const double rate = ; –These go after using namespace std;

Another Simple Program

Identifiers Identifier is a programmer-defined name that represents some element of a program. Name should indicate what the element is is used for! int itemOrdered; Rules: –Cannot use a keyword (Table 2-4) –1 st character is a letter or underscore ( _ ) –Other characters are letters, digits or _ –Case sensitive –Style issue: start with lowercase, successive words capitalized.

Data Types All variables have a type –int itemsOrdered; –double interestEarned; Many different types in C++ –int, double, char, bool, etc. –Ranges of values they hold in Table 2-6 –Also a string, but will need to add directive #include

Examples char letter; letter = ‘A’ double average; average = sum /10; cout << “The average is “ << average << endl; string name; name = “Stringfellow”; cout << “My name is << name << endl;