1 / 21

Introducing C++ Elements

Introducing C++ Elements. Outline. Main algorithms’ constructs General form of a C++ program {section 2.5} C++ language elements {section 2.1} Executable statements {section 2.4} Reserved words and symbols {section 2.2} Data types {section 2.3}. Main Algorithms’ Constucts.

Download Presentation

Introducing C++ Elements

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. Introducing C++ Elements

  2. Outline • Main algorithms’ constructs • General form of a C++ program {section 2.5} • C++ language elements {section 2.1} • Executable statements {section 2.4} • Reserved words and symbols {section 2.2} • Data types {section 2.3} CSCE 106

  3. Main Algorithms’ Constucts • An algorithm is written as a step-by-step procedure (sequence) in which choices can be made where necessary (selection), and all or part of the algorithm can be repeated (repetition). • Thus, the basic control structures of algorithms are: 1- Sequence 2- Selection 3- Repetition CSCE 106

  4. Sequence • An algorithm is based on the notion of sequence, which is the ordering of steps/statements. • Step n cannot be started until step n-1 is complete. CSCE 106

  5. General Form of a C++ Program // File: filename // Program description: …. #include directives using namespace std; void main() { Variables declaration section Executable statements section } CSCE 106

  6. General Form of a C++ Program (cont’d) • Function (a collection of related statements) is the basic unit in C++ • A C++ program must contain a main function void main () • void - function returns no value • main - lower case followed by () • { } - braces define the function body CSCE 106

  7. General Form of a C++ Program (cont’d) • General form of function body parts • Declaration statements • Variables and constants • Executable statements • C++ statements CSCE 106

  8. Comments • Comments make a program easier to understand • They are ignored (i.e. not translated) by the compiler • // used to signify a comment on a single line • /* Text text */ used for commentson multiplelines CSCE 106

  9. Compiler Directives • #include • Compiler directive • Processed during compilation process • Instructs on what you want in the program • #include <iostream> • Adds library class/file called iostream to program • Used with < > • Also “ “ user defined CSCE 106

  10. Program Processing Diagram CSCE 106

  11. Program Processing Diagram (2) CSCE 106

  12. <iostream> Included in iostream • cout refers to the standard output device; i.e. the screen cout << "Hello!"; • << output operator (insertion operator) • cin refers to the standard input device; i.e. the keyboard cin >> N1 >> N2; • >> input operator (extraction operator) directs input to variable CSCE 106

  13. Executable Statements • cout displays output on the screen cout << “Enter the distance in miles: ”; • cin gets input from the keyboard cin >> miles; • Assignment kms = KM_PER_MILES * miles; CSCE 106

  14. Reserved Words and Symbols • Reserved words have special meanings • Can NOT be used for other purposes (const, float and void are some examples) • Special symbols / delimiters • C++ has rules for special symbols = * ; { } ( ) // << >> [ ] , + - CSCE 106

  15. Data Types • Predefined data types • int (integer) • Positive or negative whole number • 1000 12 199 100000 • The size of an int depends on the machine and the compiler. On a PC it is usually a word (16 bits). • Other integers types are: • short: uses less bits (usually a byte) • long: typically uses more bits (usually 2 words) CSCE 106

  16. Data Types (cont’d) • float (floating point / real number) • Positive or negative decimal number • 10.5 1.2 100.02 99.88 • Integer part and fraction part • The number108.1517 breaks down into the following parts • 108 - integer part • 1517 - fractional part • Other floating-point data types are: • double • long double • bool (boolean) • true • false CSCE 106

  17. Data Types (cont’d) • char (character) • Represents a character • Individual character value (letter or number) • Character literal enclosed in single quotes ‘A’ • Characters are encoded using a scheme where an integer represents a particular character CSCE 106

  18. Exercise • Problem Analyse, design (using a flow chart), and implement (using C++) an algorithm that calculates, and outputs the sum (sum) of three numbers (n1, n2 & n3) input by the user. • Analysis • Input float n1: first variable float n2: second variable float n3: third variable • Output float sum: sum of n1, n2, & n3 CSCE 106

  19. Implementation (C++) #include <iostream> using namespace std; void main () { float n1, n2, n3, sum; cout << “Please input three numbers”; cin >> n1 >> n2 >> n3; sum = n1 + n2 +n3; cout << “The sum is” << sum; } Exercise (cont’d) START • Design Processing INPUT n1, n2, n3 sum = n1 + n2 + n3 OUTPUT sum STOP CSCE 106

  20. Addition.cpp /* FILE: Addition.cpp PROGRAM: Adds three numbers input by the user */ #include <iostream> using namespace std; void main () { float n1, n2, n3, sum; // declaring variables cout << “Please input three numbers”; cin >> n1 >> n2 >> n3; // inputting 3 variables sum = n1 + n2 + n3; // adding the 3 variables cout << “The sum is:” << sum; // outputting sum } CSCE 106

  21. Next lecture will be about Arithmetic Expressions CSCE 106

More Related