PROGRAM ESSENTIALS - PowerPoint PPT Presentation

1 / 20
About This Presentation
Title:

PROGRAM ESSENTIALS

Description:

Title: REFRESHER Subject: PASCAL REFRESHER Author: MARK SANDERS Last modified by: Sanders, Mark S Created Date: 4/24/1996 1:42:48 PM Document presentation format – PowerPoint PPT presentation

Number of Views:92
Avg rating:3.0/5.0
Slides: 21
Provided by: MARKS309
Category:

less

Transcript and Presenter's Notes

Title: PROGRAM ESSENTIALS


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.4E38 to 3.4E38)
  • Double (-1.7E308 to 1.7E308)
  • 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 gtgt variable gtgt . )
  • cin gtgt hours
  • cin gtgt hours gtgt payrate
  • Data Must Be Separated By One Space Or Tab

9
ASSIGNMENT / INPUT / OUTPUT
  • Output uses cout (cout ltlt variable ltlt )
  • cout ltlt Enter The Pay Rate.\n
  • cout ltlt gross_pay ltlt endl

Escape Name ASCII Comments
\a Bell 7 Sounds an audible alarm (rings the bell) at the console.
\b Backspace 8 Moves the cursor one space to the left
\t Tab 9 Inserts a tab at the current cursor position
\n Linefeed 10 Moves the cursor to the next line
\r Return 13 Moves the cursor to the beginning of the same line
\ Double Quote 34 Inserts a double quote at the cursor position
\ Quote 39 Inserts a single quote at the cursor position
\\ Backslash 92 Inserts a back slash at the current cursor
10
OUTPUT FORMATTING
  • Numeric Manipulators
  • Must Include ltiomanipgt
  • 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 ltiostreamgt //Preprocessor Directive
  • using namespace std //An ISO C Compiler
    Declaration
  • const float PI 3.14159 //A Global Constant
  • int main( )
  • cout ltlt fixed ltlt showpoint ltlt setprecision(2)
    // Floating Point Setup
  • cout ltlt Hello World! ltlt endl
    //Displays Hello World
  • cout ltlt Pi ltlt PI ltlt endl //
    Displays Value Of PI
  • cout ltlt 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 ltdata type namegt expression
  • static_castltintgt(7.9) Evaluates To 7
  • static_castltdoublegt(25) Evaluates To 25.0
  • static_castltintgt(A) Evaluates To 65
  • static_castltchargt(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
Write a Comment
User Comments (0)
About PowerShow.com