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

Slides:



Advertisements
Similar presentations
Chapter 3 Assignment and Interactive Input. 2 Objectives You should be able to describe: Assignment Operators Mathematical Library Functions Interactive.
Advertisements

Chapter 2: Basic Elements of C++
Data types and variables
Chapter 2: Basic Elements of C++
Chapter 2 Data Types, Declarations, and Displays
Chapter 2: Basic Elements of C++
Chapter 2: Introduction to C++.
Chapter 3 Assignment and Interactive Input
Chapter 2: Basic Elements of C++
Basic Elements of C++ Chapter 2.
CHAPTER 2 BASIC ELEMENTS OF C++. In this chapter, you will:  Become familiar with the basic components of a C++ program, including functions, special.
1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral.
CSC 125 Introduction to C++ Programming Chapter 2 Introduction to C++
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Input, Output, and Processing
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
Computer Programming TCP1224 Chapter 4 Variables, Constants, and Arithmetic Operators.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
Chapter 3 Assignment, Formatting, and Interactive Input C++ for Engineers and Scientists Third Edition.
C++ Programming: Basic Elements of C++.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Java Programming: From Problem Analysis to Program Design, 5e Chapter 2 Basic Elements of Java.
Chapter 2 Overview of C++. 2 Overview  2.1 Language Elements  2.2 Reserved Words & Identifiers  2.3 Data Types & Declarations  2.4 Input/Output 
Introduction to C++ Basic Elements of C++. C++ Programming: From Problem Analysis to Program Design, Fourth Edition2 The Basics of a C++ Program Function:
THE BASICS OF A C++ PROGRAM EDP 4 / MATH 23 TTH 5:45 – 7:15.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
A FIRST BOOK OF C++ CHAPTER 3 ASSIGNMENT AND INTERACTIVE INPUT.
Chapter 3: Assignment, Formatting, and Interactive Input.
C++ for Engineers and Scientists Second Edition Chapter 3 Assignment, Formatting, and Interactive Input.
A first program 1. #include 2. using namespace std; 3. int main() { 4. cout
Chapter 3 – Variables and Arithmetic Operations. Variable Rules u Must declare all variable names –List name and type u Keep length to 31 characters –Older.
Chapter 2 Variables.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Basic Elements of C++
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 2: Basic Elements of C++
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Chapter 3 Assignment, Formatting, and Interactive Input C++ for Engineers and Scientists Third Edition.
Chapter 2: Basic Elements of C++
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Chapter 2: Basic Elements of C++. Introduction Computer program – Sequence of statements whose objective is to accomplish a task Programming – Process.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 2: Basic Elements of C++
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
Chapter 2: Basic Elements of C++. Objectives In this chapter, you will: – Become familiar with the basic components of a C++ program, including functions,
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
Java Programming: Guided Learning with Early Objects Chapter 1 Basic Elements of Java.
Bill Tucker Austin Community College COSC 1315
TK1913 C++ Programming Basic Elements of C++.
Chapter 2: Basic Elements of C++
Chapter Topics The Basics of a C++ Program Data Types
Computer Programming BCT 1113
BASIC ELEMENTS OF A COMPUTER PROGRAM
Basic Elements of C++.
Java Programming: From Problem Analysis to Program Design, 4e
Chapter 2: Basic Elements of C++
Basic Elements of C++ Chapter 2.
2.1 Parts of a C++ Program.
Introduction to C++ Programming
Chapter 2: Basic Elements of Java
Chapter 2: Introduction to C++.
Engineering Problem Solving with C++ An Object Based Approach
Presentation transcript:

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

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)

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

PROGRAM STRUCTURE  COMPLETE PROGRAM #include //Preprocessor Directive using namespace std;//An ISO C++ Compiler Declaration const float PI = ;//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 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

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