1 / 15

Announcements

The CS150 course website is up and running. Access lecture slides, assignments, and lab notes in Microsoft PowerPoint, Word, or PDF format through the provided link.

peterbell
Download Presentation

Announcements

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Announcements • Website is up! • http://zeus.pacificu.edu/shereen/CS150 • All lecture slides, assignments, lab notes will be available on this site in two forms: • Microsoft PowerPoint or Word • PDF (Acrobat Reader) CS150 Introduction to Computer Science 1

  2. Implementation //Program purpose: converts distance in miles to kilometers //Author: Friedman & Koffman //Date: August 30, 2000 #include <iostream> int main() { using namespace std; const float KM_PER_MILE = 1.609; float miles, kms; //Get the distance in miles cout << “Enter the distance in miles”; cin >> miles; //Convert the distance to kilometers kms = KM_PER_MILE*miles; //Display the distance in kilometers cout << “The distance in kilometers is” << kms << endl; } CS150 Introduction to Computer Science 1

  3. C++ Language Elements • Comments are • how you explain in English what your program does • Ignored by the compiler • Very, very, very important • Format of comments: //comment /* comment */ CS150 Introduction to Computer Science 1

  4. Compiler directives • #include <iostream> • # signifies compiler directive • Processed BEFORE program translation • #include tells the compiler to look for libraries • <> signifies part of standard C++ libraries • We’ll see other examples later of compiler directives CS150 Introduction to Computer Science 1

  5. Namespace std • using namespace std; • Indicates that we will be using objects that are named in a region called namespace std. • The statement ends in a semicolon. • The statement appears in all our programs. CS150 Introduction to Computer Science 1

  6. Main function definition int main( ) { main program } • Your main program is where execution starts. • Every program has one! CS150 Introduction to Computer Science 1

  7. Program statements • Declaration Statements • What data is needed by the program? • const float KM_PER_MILE = 1.609; • float miles, kms; • Executable Statements • Everything else • Examples: • cout, cin • Assignment • All end with semicolon ; CS150 Introduction to Computer Science 1

  8. Identifiers • Names used in program • Examples: • Variables • Functions • Rules: • Begin with letter or underscore • Consist of letters, digits and underscore • Cannot use reserved word CS150 Introduction to Computer Science 1

  9. Identifiers, Contd. • Reserved Words examples • const, float, int, return, main • Complete list in Appendix B of text • Case sensitive • Valid or Invalid? Letter1 joe’s 1letter cent_per_inch Inches two-dimensional Inches*num hello CS150 Introduction to Computer Science 1

  10. Data Types and Declarations • A data type is a way to represent a particular set of values • Four types • Integers • Reals • Booleans • Characters CS150 Introduction to Computer Science 1

  11. Integers • Whole numbers, positive or negative • Stored as binary number • Datatype is called int • Operations? • Finite • Examples of integer literals are: 123, -23, 0, 32767 CS150 Introduction to Computer Science 1

  12. Reals • Real numbers can contain fractional parts • Stored in floating point format • Datatype is float • Operations? • Examples of float literals are: 1.0, -.1, 0., 12E5, -1E-2 CS150 Introduction to Computer Science 1

  13. Characters • Individual character--letter, digit, symbol • Characters stored as byte • Datatype is char • Operations? • Char literals are enclosed in single quotes and examples include: 'A' 'a' '?' CS150 Introduction to Computer Science 1

  14. Purpose of Datatypes • Different ones allow compiler to know how to represent value • Different datatypes can use different operations • The integer 2 is different from 2.0 and the character 2 (all stored differently) CS150 Introduction to Computer Science 1

  15. Declarations • Declarations are at the beginning of a program • They list the variables used • Format: datatype identifier; CS150 Introduction to Computer Science 1

More Related