Presentation is loading. Please wait.

Presentation is loading. Please wait.

PROGRAM ESSENTIALS. TOKENS  SMALLEST UNITS OF A PROGRAM LANGUAGE  Special Symbols  Mathematical Operators  Punctuation  Word Symbols  Key Words.

Similar presentations


Presentation on theme: "PROGRAM ESSENTIALS. TOKENS  SMALLEST UNITS OF A PROGRAM LANGUAGE  Special Symbols  Mathematical Operators  Punctuation  Word Symbols  Key Words."— Presentation transcript:

1 PROGRAM ESSENTIALS

2 TOKENS  SMALLEST UNITS OF A PROGRAM LANGUAGE  Special Symbols  Mathematical Operators  Punctuation  Word Symbols  Key Words or Reserved Words  Identifiers / Operations(functions)  Predefined Operations(functions) –Can Be Redefined By Programmer (Will Get You Into Trouble)  User Defined Identifiers / Operations(functions) –Non-Reserved Word –First Character Must Be Alpha –No Embedded Blanks –No Special Characters (*,-,+,/….) (Only Underscore Is Allowed)

3 DATA TYPES  SIMPLE DATA TYPES  Integral (ordered data types)  Whole Numbers (int)  Characters (char)  Boolean (bool)  Floating-Point (real numbers)  Float (-3.4E+38 to 3.4E+38)  Double (-1.7E+308 to 1.7E+308)  String (Must Include String Library)  Sequence of zero or more characters  Strings are enclosed in double quote marks(“Mark”)

4 OPERATORS  ARITHMETIC OPERATOR  +(Addition)  -(Subtraction)  *(Multiplication)  /(Division)  %(Modulus) (Can Only Be Used With Integrals)  EVALUATION RULES  Evaluates To An Integer With Integer Operands  Evaluates To Floating-Point(FP) With FP Operands  Evaluates To FP With Mixed Operands  Modulus Always Evaluates To Integer

5 OPERATORS  HIERARCHY ( )PRIORITY 1 *, /, %PRIORITY 2 +, - PRIORITY 3  EVALUATION  Parentheses (From Inside Out)  All Others (From Left To Right)

6 CONSTANTS  DECLARED PRIOR TO MAIN  const (datatype) (identifier) = (value);  VALUE IS SET WHEN PROGRAM RUNS  CANNOT CHANGE WHILE RUNNING Examples; const double RATE = 0.065; const int MIN_ORDER = 20; const char BLANK = ‘ ‘;

7 VARIABLES  MUST BE DECLARED BEFORE USED(at top of function)  IDENTIFIES NAME TO MEMORY LOCATION  IDENTIFIES NAME TO TYPE  CAN BE INITIALIZED AT DECLARATION int main( ) { int age; char sex; float rate = 0.065; //Initialized

8 ASSIGNMENT / INPUT / OUTPUT  Assignment uses ‘=‘ operator  Only One Token Left Of Operator Allowed  test = 0.0;  count = count + value;  x = y = z;  Input uses cin (cin >> variable >> ……. )  cin >> hours;  cin >> hours >> payrate;  Data Must Be Separated By One Space Or Tab

9 ASSIGNMENT / INPUT / OUTPUT  Output uses cout (cout << variable << ……)  cout << “Enter The Pay Rate.\n”;  cout << gross_pay << endl; EscapeNameASCIIComments \aBell7Sounds an audible alarm (rings the bell) at the console. \bBackspace8Moves the cursor one space to the left \tTab9Inserts a tab at the current cursor position \nLinefeed10Moves the cursor to the next line \rReturn13Moves the cursor to the beginning of the same line \”Double Quote34Inserts a double quote at the cursor position \’Quote39Inserts a single quote at the cursor position \\Backslash92Inserts a back slash at the current cursor

10 OUTPUT FORMATTING  Numeric Manipulators  Must Include  fixed or scientific – Sets Fixed Decimal Format or Scientific Format  showpoint or noshowpoint – Ensures Decimal Point Is Displayed or Not  setprecision – Sets Number of Decimal Places Works On All Floating Point Numbers After It Is Invoked Or Until A Subsequent Statement Changes The Precision  setw – Sets Number Of Characters To Display Only Works On First Expression After It Is Invoked Must Invoke For Each Expression To Be Output  right or left – Sets justification (right is default) Remains in effect until changed to opposite justification  setfill – Sets the fill character used (default ‘ ‘ a space)

11 INCREMENT / DECREMENT  Types:  X++ (Post Increment)X- - (Post Decrement)  ++X (Pre Increment)- - X (Pre Decrement)  Both Produce The Result X = X + 1 or X = X - 1;  Post Increments After Expression Is Evaluated  Y = X++ + Z Is The Same AS Y = X + Z X = X + 1  Pre Increments Before Expression Is Evaluated  Y = ++X + Z X = X + 1 Y = X + Z  Decrement Works The Exact Same Way

12 PROGRAM STRUCTURE  COMPLETE PROGRAM #include //Preprocessor Directive using namespace std;//An ISO C++ Compiler Declaration const float PI = 3.14159;//A Global Constant int main( ) { cout << fixed << showpoint << setprecision(2) // Floating Point Setup cout << “Hello World! ” << endl; //Displays Hello World cout << “Pi = “ << PI << endl; // Displays Value Of PI cout << endl; //Cursor Moved Down One system (“pause”); //Stops window closing return 0; }

13 COMMENTS  COMMENTS  Use /* …..*/ Multi-line Comments  Use // Single Line Comments  Used To Explain Major Code Processes And Unique Program Solutions  Good Comments Provide Much Needed Help When Debugging or Altering Existing Code (which just might be you)

14 TYPE CONVERSION  CONVERT ONE DATA TYPE TO ANOTHER  Use Cast Operator  static_cast expression –static_cast (7.9)Evaluates To7 –static_cast (25)Evaluates To25.0 –static_cast (‘A’)Evaluates To 65 –static_cast (65)Evaluates To‘A’  Using Cast Operator To Convert FP Values Truncates Decimal portion

15 STATEMENTS  PROGRAMS ARE BUILT OF STATEMENTS  OUTPUT STATEMENT  Used To Display Or Store Data  INPUT STATEMENT  Used To Accept Or Retrieve Data  ASSIGNMENT STATEMENT  Used To Store Data To Memory  CONTROL STATEMENT  Use To Control The Program Flow –SELECTION(Making Choices) –ITERATION(Repeating Processes)

16 DEBUGGING  Debugging Your Code  A "bug" is a mistake in a program. So debugging is the process of eliminating errors in programs.  Three types of program errors:  Compile-Time Errors  Run-Time Errors  Logical Errors

17 DEBUGGING  Compile Time Errors  Occurs when you try to compile your code  If Compiler is not able to compile the code it issues an error  Compile-time errors listed under the compiler tab  Compiler finds most of the problems with syntax  Examples: –Missing semi-colon –Undeclared variable  Error message does not always show correct location of error  Error will always be at or above the line the message refers to  If errors exist an executable file is not created.

18 DEBUGGING  Runtime Errors  Run time errors occur during program execution  The program terminates abnormally (crash or hang).  Examples: –Trying to divide by zero –Trying to access memory you are not allowed to access  Can be difficult to find  Typically requires the use of debugger to find error

19 DEBUGGING  Logical Errors  Program runs but does not produce correct results  Examples: –Missing { or } brace caused faulty logic –Misunderstanding of the formulas used by the code –Overall program flow  These are often the most difficult to diagnose  May require use of debugger to find error

20 IN THE END  The Programmers Job Is To:  Design the program  Write the code  Debug the code to eliminate all errors  Test the code to validate the output


Download ppt "PROGRAM ESSENTIALS. TOKENS  SMALLEST UNITS OF A PROGRAM LANGUAGE  Special Symbols  Mathematical Operators  Punctuation  Word Symbols  Key Words."

Similar presentations


Ads by Google