Bill Tucker Austin Community College COSC 1315

Slides:



Advertisements
Similar presentations
Lecture 2 Introduction to C Programming
Advertisements

Introduction to C Programming
Introduction to C Programming
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Chapter 2: Introduction to C++.
Introduction to C Programming
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.
Introduction To C++ Programming 1.0 Basic C++ Program Structure 2.0 Program Control 3.0 Array And Structures 4.0 Function 5.0 Pointer 6.0 Secure Programming.
Basic Elements of C++ Chapter 2.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
CSC 125 Introduction to C++ Programming Chapter 2 Introduction to C++
Input & Output: Console
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Chapter 2. C++ Program Structure C++ program is a collection of subprograms Subprograms in C++ are called FUNCTIONS Each function performs a specific.
C++ Programming: Basic Elements of C++.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
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.
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 3: Input/Output Samples.
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 of C++ How to Program, 10/e © by Pearson Education, Inc. All Rights Reserved.
© by Pearson Education, Inc. All Rights Reserved.
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
Chapter 1.2 Introduction to C++ Programming
Chapter 1: Introduction to computers and C++ Programming
Introduction to C++ Programming
Introduction to C++ Programming
Chapter 2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 2: Introduction to C++
Computing and Statistical Data Analysis Lecture 2
Completing the Problem-Solving Process
Chapter 2 - Introduction to C Programming
Bill Tucker Austin Community College COSC 1315
Chapter 2 part #1 C++ Program Structure
Basic Elements of C++.
Chapter 2 - Introduction to C Programming
Chapter 2: Basic Elements of C++
Chapter 2: Introduction to C++
Basic Elements of C++ Chapter 2.
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
2.1 Parts of a C++ Program.
Introduction to C++ Programming
Programming Funamental slides
Chapter 2 - Introduction to C Programming
Programming Funamental slides
Chapter 2 - Introduction to C Programming
CS150 Introduction to Computer Science 1
Chapter 2: Introduction to C++.
Programs written in C and C++ can run on many different computers
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Chapter 2 - Introduction to C Programming
C++ Programming Basics
Subject:Object oriented programming
Introduction to C Programming
Chapter 1 c++ structure C++ Input / Output
Chapter 2 part #1 C++ Program Structure
Presentation transcript:

Bill Tucker Austin Community College COSC 1315 Chapter 2 Lecture Notes Bill Tucker Austin Community College COSC 1315 Copyright © 2002 W. A. Tucker Copyright © 2002 W. A. Tucker

C++ Syntax C++ is case sensitive This means that “Include” is not the same as “include” All C++ statements end with a semi-colon Copyright © 2002 W. A. Tucker

Reserved Words There are approximately 50 reserved words within C++ All reserved words are lowercase A reserved word will usually show up on the screen in blue Reserved words will be introduced throughout this course Copyright © 2002 W. A. Tucker

C++ Syntax Comments The compiler ignores all comments Comments are usually shown in green on the screen // Everything after the // until the line return is a comment This is the preferred method for single line comments /* */ Everything between the /* and */ is a comment This work well for multiple line comments, to comment out a block of code when debugging a program, and to add sample output at the end of a program Copyright © 2002 W. A. Tucker

C++ Syntax compiler directive #include <iostream> begins with a # and is processed by the pre-processor #include <iostream> This directive tells the pre-processor to go find a library called iostream The iostream include library provides the necessary code that supports the console input and output The iostream include library is the 1998 ANSI standard for console input and output and is NOT the same as iostream.h (which some tutors might tell you to use) NOTE: include is a reserved word Copyright © 2002 W. A. Tucker

C++ Syntax using statement using namespace std; This statement provides access to what is essentially a library of code that supports the standard names supported by the 1998 ANSI standards. This statement should follow all of the #include compiler directives NOTE: using and namespace are both reserved words. Copyright © 2002 W. A. Tucker

C++ Syntax Every C++ program begins execution at a function called “main” Function definitions have two parts Function header EX: int main() Body of the function All of the code that is contained between the open and close curly braces EX: int main() { // body of the function } Copyright © 2002 W. A. Tucker

KEY Programming Concepts Every value that a programmer uses within a program falls into one three categories Named Variable Named Constant Literal Constant Every value has a data type associated with that value This association is either declared or implied Every value is stored in the memory of the computer at some memory address Copyright © 2002 W. A. Tucker

C++ Syntax Declaration statements Every value that a programmer wants to refer to by a name must first be declared with a declaration statement Declaration statements have the ability to perform three tasks They define a name (called an identifier) that the compiler will recognize They allocate the proper amount of memory for each identifier and map the contents to that specific address Optionally, they may initialize (at program load time) the contents of the identifier Copyright © 2002 W. A. Tucker

Identifier Names Rules Names in C++ are case sensitive Names may be very long (255 characters) Names may contain letters, numbers and only the special character underscore Names may NOT begin with a number Names must begin with a letter (at this stage in your C++ knowledge) Names may NOT contain blanks Names may not be a reserved word Copyright © 2002 W. A. Tucker

Identifier Names Style Conventions Use names that are descriptive Standard abbreviations are fine Begin all variables with a lower case letter For variable names that contain more than one word, capitalize the first letter of every word after the first word (this is called the “Hungarian notation”) EX: a variable called miles per hour would be named milesPerHour For named constants, capitalize the entire name, and use the underscore to separate each word EX: a constant named kilometers per mile would be named KM_PER_MILE Copyright © 2002 W. A. Tucker

Data Types There are more than 10 different data types supported by C++ For this course you only need to use FIVE data types whole numbers (no decimal point) real numbers (with a decimal point) character (single character) strings of characters boolean values (true or false) Copyright © 2002 W. A. Tucker

Specific Data Types int is the data type (reserved word) to define a whole number double is the data type (reserved word) to define a real number char is the data type (reserved word) to define a single character string is the user defined data type (class) to define a string of characters bool is the data type (reserved word) to define a boolean value Copyright © 2002 W. A. Tucker

C++ Syntax More on Declaration Statements The general format of a declaration statement is data_type identifier; EX: double miles; Optionally, it may initialize the value (at load time) [NOTE: This is NOT an executable instruction.] data_type identifier = initial_value; EX: double miles = 0; Copyright © 2002 W. A. Tucker

Literal Values Every literal value has an implied data type int – whole numbers EX: 25 double – real numbers EX: 3.14159 char – characters EX: ‘A’ string – strings of characters EX: “This is a string of characters” bool – boolean value EX: true or false Copyright © 2002 W. A. Tucker

Named Variables The following are examples of declaring named variables: int loopCount = 0; double hourlyWage = 0; char courseGrade = ‘A’; string message = “Test Message”; bool raining = false; NOTE: These are all variables and their initial values may be changed once the program begins execution. Copyright © 2002 W. A. Tucker

Named Constants The following are examples of declaring named constants: const int MONTHS_PER_YR = 12; const double PI = 3.14159; const char YES = ‘Y’; const string MSG1 = “This is message one”; const bool TRUE = true; NOTE: These are all constants and their initial values may NOT be changed once the program begins execution. Copyright © 2002 W. A. Tucker

Executable Instructions Executable instructions are instructions that are executed while the program is running. They may use variables, named constants and literal constants but they can only change the value of a variable. The miles.cpp program contains several executable instructions Assignment statements Input / Output statements return statement Copyright © 2002 W. A. Tucker

Assignment Statement Assignment statements Assignment statements have a variable name on the left hand side of an equals sign and some expression or value on the right hand side of the equals sign. Assignment statements are NOT mathematical identities. First, the right hand side is evaluated based upon the value of the expression AT THAT MOMENT IN TIME. Next, the result is assigned to the variable name that is on the left hand side of the equals sign. Copyright © 2002 W. A. Tucker

Input / Output Statements Console input (cin) EX: cin >> miles; The value entered by the user on the keyboard (console input) is stored into the variable named miles Console output (cout) EX: cout << kms; The current value of kms is displayed on the monitor (console output) NOTE: Both cin and cout require the #include <iostream> compiler directive Copyright © 2002 W. A. Tucker

Stream Input/Output All input and output is treated like a stream (or flow) of characters. Output Stream The characters that are inserted into the output stream are added to what was there already. Copyright © 2002 W. A. Tucker

Output Formatting A line return may be added to the output stream using endl (an I/O manipulator inside iostream) The following two sets of instructions produce the same output. cout << “The distance in kilometers is “; cout << kms; cout << endl; OR cout << “The distance in kilometers is “ << kms << endl; NOTE: Without the blank space after “is”, the phrase and number will run together on the output. Copyright © 2002 W. A. Tucker

return statement The return statement is used to return a value to the program that “called” the current program being executed. In the case of the main function, the “calling program” is the operating system. Returning an integer value of zero indicates that the program had a normal execution. EX: return 0; // program ended normally There will be more information about the return statement when we cover functions. Copyright © 2002 W. A. Tucker

Putting it all Together A C++ program will have the following general format // header block // compiler directives #include <iostream> using namespace std; int main () { // body of main function return 0; } // end of main function Copyright © 2002 W. A. Tucker

Arithmetic Expressions The mathematical operators in C++ are Addition (+) Subtraction (-) Multiplication (*) Division (/) Modulus (%) Copyright © 2002 W. A. Tucker

Integer Arithmetic Division of two integers will create a result that is an integer 6 / 3 will evaluate to an integer 2 7 / 3 will also evaluate to an integer 2 Modulus operator calculates the integer remainder 6 % 3 will evaluate to an integer 0 7 % 3 will evaluate to an integer 1 The combination of these two operations is very useful in certain applications (ie: ATM machine) Copyright © 2002 W. A. Tucker

Mixed Type Arithmetic Expressions containing both integer and double values will evaluate to a double Order of operations (simplified) is Parenthesis Unary operation (+ and – of just one term) Multiplication, Division and Modulus Addition and Subtraction (of two or more terms) Expressions are evaluated left to right for the same order of operations (except unary operations) Copyright © 2002 W. A. Tucker

Evaluate the Following 3 * 6 / 8 + 3 * 10 / 4 3 * 6 / 8.0 + 3 * 10 / 4 3 * 6 / 8.0 + 3 * 10.0 / 4 (3 * 6 / 8) + (3 * 10 ) / 4 3 * (6 / 8 + 3) * 10 / 4 NOTE: When in doubt about how to write an expression, use extra parenthesis Copyright © 2002 W. A. Tucker