PROGRAM ESSENTIALS. TOKENS SMALLEST UNITS OF A PROGRAM LANGUAGE Special Symbols Mathematical...

20
PROGRAM ESSENTIALS

description

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”)

Transcript of PROGRAM ESSENTIALS. TOKENS SMALLEST UNITS OF A PROGRAM LANGUAGE Special Symbols Mathematical...

Page 1: PROGRAM ESSENTIALS. TOKENS  SMALLEST UNITS OF A PROGRAM LANGUAGE  Special Symbols  Mathematical Operators  Punctuation  Word Symbols  Key Words.

PROGRAM ESSENTIALS

Page 2: PROGRAM ESSENTIALS. TOKENS  SMALLEST UNITS OF A PROGRAM LANGUAGE  Special Symbols  Mathematical Operators  Punctuation  Word Symbols  Key Words.

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)

Page 3: PROGRAM ESSENTIALS. TOKENS  SMALLEST UNITS OF A PROGRAM LANGUAGE  Special Symbols  Mathematical Operators  Punctuation  Word Symbols  Key Words.

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”)

Page 4: PROGRAM ESSENTIALS. TOKENS  SMALLEST UNITS OF A PROGRAM LANGUAGE  Special Symbols  Mathematical Operators  Punctuation  Word Symbols  Key Words.

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

Page 5: PROGRAM ESSENTIALS. TOKENS  SMALLEST UNITS OF A PROGRAM LANGUAGE  Special Symbols  Mathematical Operators  Punctuation  Word Symbols  Key Words.

OPERATORS HIERARCHY

( ) PRIORITY 1* , / , % PRIORITY 2

+ , - PRIORITY 3

EVALUATION Parentheses (From Inside Out) All Others (From Left To Right)

Page 6: PROGRAM ESSENTIALS. TOKENS  SMALLEST UNITS OF A PROGRAM LANGUAGE  Special Symbols  Mathematical Operators  Punctuation  Word Symbols  Key Words.

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 = ‘ ‘;

Page 7: PROGRAM ESSENTIALS. TOKENS  SMALLEST UNITS OF A PROGRAM LANGUAGE  Special Symbols  Mathematical Operators  Punctuation  Word Symbols  Key Words.

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

Page 8: PROGRAM ESSENTIALS. TOKENS  SMALLEST UNITS OF A PROGRAM LANGUAGE  Special Symbols  Mathematical Operators  Punctuation  Word Symbols  Key Words.

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

Page 9: PROGRAM ESSENTIALS. TOKENS  SMALLEST UNITS OF A PROGRAM LANGUAGE  Special Symbols  Mathematical Operators  Punctuation  Word Symbols  Key Words.

ASSIGNMENT / INPUT / OUTPUT

Output uses cout (cout << variable << ……) cout << “Enter The Pay Rate.\n”; cout << gross_pay << 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

Page 10: PROGRAM ESSENTIALS. TOKENS  SMALLEST UNITS OF A PROGRAM LANGUAGE  Special Symbols  Mathematical Operators  Punctuation  Word Symbols  Key Words.

OUTPUT FORMATTING Numeric Manipulators

Must Include <iomanip> 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)

Page 11: PROGRAM ESSENTIALS. TOKENS  SMALLEST UNITS OF A PROGRAM LANGUAGE  Special Symbols  Mathematical Operators  Punctuation  Word Symbols  Key Words.

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 ASY = X + ZX = X + 1

Pre Increments Before Expression Is Evaluated Y = ++X + Z

X = X + 1Y = X + Z

Decrement Works The Exact Same Way

Page 12: PROGRAM ESSENTIALS. TOKENS  SMALLEST UNITS OF A PROGRAM LANGUAGE  Special Symbols  Mathematical Operators  Punctuation  Word Symbols  Key Words.

PROGRAM STRUCTURE COMPLETE PROGRAM

#include <iostream> //Preprocessor Directiveusing 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 Worldcout << “Pi = “ << PI << endl; // Displays Value Of PIcout << endl; //Cursor Moved Down One system (“pause”); //Stops window closingreturn 0;}

Page 13: PROGRAM ESSENTIALS. TOKENS  SMALLEST UNITS OF A PROGRAM LANGUAGE  Special Symbols  Mathematical Operators  Punctuation  Word Symbols  Key Words.

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)

Page 14: PROGRAM ESSENTIALS. TOKENS  SMALLEST UNITS OF A PROGRAM LANGUAGE  Special Symbols  Mathematical Operators  Punctuation  Word Symbols  Key Words.

TYPE CONVERSION CONVERT ONE DATA TYPE TO ANOTHER

Use Cast Operator static_cast <data type name> expression

– static_cast<int>(7.9) Evaluates To 7– static_cast<double>(25) Evaluates To 25.0– static_cast<int>(‘A’) Evaluates To 65– static_cast<char>(65) Evaluates To ‘A’

Using Cast Operator To Convert FP Values Truncates Decimal portion

Page 15: PROGRAM ESSENTIALS. TOKENS  SMALLEST UNITS OF A PROGRAM LANGUAGE  Special Symbols  Mathematical Operators  Punctuation  Word Symbols  Key Words.

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)

Page 16: PROGRAM ESSENTIALS. TOKENS  SMALLEST UNITS OF A PROGRAM LANGUAGE  Special Symbols  Mathematical Operators  Punctuation  Word Symbols  Key Words.

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

Page 17: PROGRAM ESSENTIALS. TOKENS  SMALLEST UNITS OF A PROGRAM LANGUAGE  Special Symbols  Mathematical Operators  Punctuation  Word Symbols  Key Words.

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.

Page 18: PROGRAM ESSENTIALS. TOKENS  SMALLEST UNITS OF A PROGRAM LANGUAGE  Special Symbols  Mathematical Operators  Punctuation  Word Symbols  Key Words.

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

Page 19: PROGRAM ESSENTIALS. TOKENS  SMALLEST UNITS OF A PROGRAM LANGUAGE  Special Symbols  Mathematical Operators  Punctuation  Word Symbols  Key Words.

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

Page 20: PROGRAM ESSENTIALS. TOKENS  SMALLEST UNITS OF A PROGRAM LANGUAGE  Special Symbols  Mathematical Operators  Punctuation  Word Symbols  Key Words.

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