PROGRAM ESSENTIALS

advertisement
PROGRAM ESSENTIALS
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)
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”)
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
OPERATORS
HIERARCHY
( )
*, /,%
+,-
PRIORITY 1
PRIORITY 2
PRIORITY 3
EVALUATION
Parentheses (From Inside Out)
All Others (From Left To Right)
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 = ‘ ‘;
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
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
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
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)
INCREMENT / DECREMENT
Types:
X++ (Post Increment)
++X (Pre Increment)
X- - (Post Decrement)
- - 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
PROGRAM STRUCTURE
COMPLETE PROGRAM
#include <iostream>
using namespace std;
//Preprocessor Directive
//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;
}
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)
TYPE CONVERSION
CONVERT ONE DATA TYPE TO ANOTHER
Use Cast Operator
static_cast <data type name> expression
– static_cast<int>(7.9)
Evaluates To
– static_cast<double>(25)
Evaluates To
– static_cast<int>(‘A’)
Evaluates To
– static_cast<char>(65)
Evaluates To
7
25.0
65
‘A’
Using Cast Operator To Convert FP Values Truncates
Decimal portion
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)
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
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.
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
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
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