Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introducing C++ Elements. CSCE 1062 Outline Main algorithms’ constructs General form of a C++ program {section 2.5} C++ language elements {section 2.1}

Similar presentations


Presentation on theme: "Introducing C++ Elements. CSCE 1062 Outline Main algorithms’ constructs General form of a C++ program {section 2.5} C++ language elements {section 2.1}"— Presentation transcript:

1 Introducing C++ Elements

2 CSCE 1062 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}

3 CSCE 1063 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

4 CSCE 1064 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.

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

6 CSCE 1066 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

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

8 CSCE 1068 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 comments on multiple lines

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

10 CSCE Program Processing Diagram

11 CSCE Program Processing Diagram (2)

12 CSCE 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

13 CSCE 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;

14 CSCE 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 = * ; { } ( ) // > [ ], + -

15 CSCE Data Types Predefined data types  int(integer)  Positive or negative whole number   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)

16 CSCE Data Types (cont’d)  float(floating point / real number)  Positive or negative decimal number   Integer part and fraction part  The number breaks down into the following parts integer part fractional part  Other floating-point data types are: double long double  bool(boolean)  true  false

17 CSCE 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

18 CSCE 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

19 CSCE Exercise (cont’d) INPUT n1, n2, n3 OUTPUT sum START STOP sum = n1 + n2 + n3 Design Implementation (C++) #include 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; } Processing

20 CSCE Addition.cpp /* FILE: Addition.cpp PROGRAM: Adds three numbers input by the user */ #include 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 }

21 CSCE Next lecture will be about Arithmetic Expressions


Download ppt "Introducing C++ Elements. CSCE 1062 Outline Main algorithms’ constructs General form of a C++ program {section 2.5} C++ language elements {section 2.1}"

Similar presentations


Ads by Google