Introduction to Computer Programming Basic Elements of C++ C++ Programming: From Problem Analysis to Program Design 1
Chapter Topics • The Basics of a C++ Program • Data Types • Arithmetic Operators and Operator Precedence • Expressions • Input • Increment and Decrement Operators • Output • Preprocessor Directives • Program Style and Form 2
The Basics of a C++ Program • A C++ program is a collection of one or more subprograms (functions) • Function – Collection of statements – Statements accomplish a task • Every C++ program has a function called: main 3
Example Program #include <iostream> using namespace std; int main() { cout<<"Welcome to C++ Programming"<<endl; return 0; } Welcome to C++ Programming Program Output: 4
The Basics of a C++ Program • Programming language – a set of rules, symbols, special words • Rules – syntax – specifies legal instructions • Symbols – special symbols ( + - * ! … ) • Special words – reserved words – (int, float, double, char …) 5
Identifiers • Rules for identifiers – must begin with letter or the underscore “_” – followed by any combination of numerals or letters – recommend meaningful identifiers • Evaluate the following ElectricCharge 23Skidoo _snarFbLat 6
Data Types • Definition: – a set of values – combined with a set of operations 7 Simple: int, float, char, bool, etc… Structured: A collection of simple data types Pointers: Points to address
Data Types • Simple data types include – Integers – Floating point – Enumeration • Integer data types include char short int long bool Numbers, symbols, letters Values true and false only 8 Numbers without decimals
Number Data Types Integer & Decimal 9
Floating‐Point Types (Decimal) • Stored using scientific notation – the sign of the number, – the significant digits of the number – the sign of the power of 10 – the power of 10 10
Data Types • Different floating‐ point types • Note that various types will – have different ranges of values – require different amounts of memory 11
Data Types • The string Type – a programmer‐defined type – requires #include <string> • A string is a sequence of characters "Hi Mom" "We're Number 1!" "75607" 12
Arithmetic Operators and Operator Precedence • Common operators for calculations + - * / % • Precedence same as in algebraic usage – Inside parentheses done first – Next * / % from left to right – Then + and - from left to right 13
Expressions • An expression includes – constants – variables – function calls – combined with operators 3 / 2 + 5.0 Ans: 6 sin(x) + sqrt(y) 14
Expressions • Expressions can include – values all of the same type 3 + 5 * 12 – 7 Ans: 56 – values of different (compatible) types 1.23 * 18 / 9.5 Ans: 2.33053 • An operation is evaluated according to the types of the operands – if they are the same, the result is the type of the operands – if the operands are different (int and float) then the result is float 15
Type Casting • Implicit change of type can occur – when operands are of different type • It is possible to explicitly specify that an expression be converted to a different type static_cast < type > (expression) static_cast <int> (3.5 * 6.9 / 2.1) Ans: 11 16
Input • Storing data in the computer's memory requires two steps 1. Allocate the memory by declaring a variable 2. Have the program fetch a value from the input device and place it in the allocated memory location x 123 cin >> x 17 int x; 123 will be stored in x
Allocating Memory • Variable – A memory location whose content may change during program execution • Declaration: – Syntax: type identifier; – Example: double x; (declaration) int y = 45; (declaration and initialization) Note optional initialization of the variable 18
Allocating Memory • Named Constant – A memory location whose content cannot be changed • Declaration – Syntax: const type identifier = value; – Example const double PI = 3.14159; Note required initialization of the named constant 19
Putting Data Into Variables Data can be put into a variable through either: 1. At initialization time (by programmer) int x=5; 2. Assignment statement (by programmer) – Syntax: variable = expression; – Example x = 1.234; volume = sqr (base) * height; 3. Input (read) statement (by user) – Syntax: cin >> variable ; – Example cin >> height; 20
Increment and Decrement Operators • Pre‐increment ++x; equivalent to x = x + 1; • Pre‐decrement --x; equivalent to x = x - 1; – Pre‐(increment/decrement): Changes the value before execution of a statement – Post‐(increment/decrement): Changes the value after execution of the statement 21 Post‐increment x++; equivalent to x = x + 1; Post‐decrement x--; equivalent to x = x - 1;
Output • Values sent to an output device – Usually the screen – Can also be a file or some device • Syntax for screen output: cout << expression << … • Example cout << "The total is "<< sum << endl; Output command Insertion operator Values to be printed New line 22 Text to be displayed
Output • Escape sequences also used to manipulate output cout << "The total ist "<< sum << endl; 23
Preprocessor Directives • Commands supplied to the preprocessor – Runs before the compiler – Modifies the text of the source code before the compiler starts • Syntax – start with # symbol – #include <headerFileName> • Example: #include <iostream> 24
Preprocessor Directives • Note the preprocessor step in the sequence 25
Namespace • The #include <iostream> command is where cin and cout are declared • They are declared within a namespace called std • When we specify using namespace std; – Then we need not preface the cin and cout commands with std::cin and std::cout 26
Program Style and Form • Every program must contain a function called main int main () { … return 0; } • The int specifies that it returns an integer value • Also you can use void main( ) { … } • The void specifies there will be no return value 27
Program Style and Form • Variables usually declared – inside main – at beginning of program • Use blanks and space to make the program easy for humans to read • Semicolons ; required to end a statement • Commas used to separate things in a list int x,y,z; 28
Program Style and Form • Documentation – Comments specified between /* this is a comment */ and following // also a comment – Always put at beginning of program /* name, date, cpo, purpose of program */ 29
Program Style and Form • Names of identifiers should help document program double electricCharge; // instead of ec • Prompt keyboard entry cout << "Enter the value for x -> "; cin >> x; 30

Basic Elements of C++

  • 1.
    Introduction to ComputerProgramming Basic Elements of C++ C++ Programming: From Problem Analysis to Program Design 1
  • 2.
    Chapter Topics • TheBasics of a C++ Program • Data Types • Arithmetic Operators and Operator Precedence • Expressions • Input • Increment and Decrement Operators • Output • Preprocessor Directives • Program Style and Form 2
  • 3.
    The Basics ofa C++ Program • A C++ program is a collection of one or more subprograms (functions) • Function – Collection of statements – Statements accomplish a task • Every C++ program has a function called: main 3
  • 4.
    Example Program #include <iostream> usingnamespace std; int main() { cout<<"Welcome to C++ Programming"<<endl; return 0; } Welcome to C++ Programming Program Output: 4
  • 5.
    The Basics ofa C++ Program • Programming language – a set of rules, symbols, special words • Rules – syntax – specifies legal instructions • Symbols – special symbols ( + - * ! … ) • Special words – reserved words – (int, float, double, char …) 5
  • 6.
    Identifiers • Rules foridentifiers – must begin with letter or the underscore “_” – followed by any combination of numerals or letters – recommend meaningful identifiers • Evaluate the following ElectricCharge 23Skidoo _snarFbLat 6
  • 7.
    Data Types • Definition: –a set of values – combined with a set of operations 7 Simple: int, float, char, bool, etc… Structured: A collection of simple data types Pointers: Points to address
  • 8.
    Data Types • Simpledata types include – Integers – Floating point – Enumeration • Integer data types include char short int long bool Numbers, symbols, letters Values true and false only 8 Numbers without decimals
  • 9.
  • 10.
    Floating‐Point Types (Decimal) •Stored using scientific notation – the sign of the number, – the significant digits of the number – the sign of the power of 10 – the power of 10 10
  • 11.
    Data Types • Differentfloating‐ point types • Note that various types will – have different ranges of values – require different amounts of memory 11
  • 12.
    Data Types • Thestring Type – a programmer‐defined type – requires #include <string> • A string is a sequence of characters "Hi Mom" "We're Number 1!" "75607" 12
  • 13.
    Arithmetic Operators andOperator Precedence • Common operators for calculations + - * / % • Precedence same as in algebraic usage – Inside parentheses done first – Next * / % from left to right – Then + and - from left to right 13
  • 14.
    Expressions • An expressionincludes – constants – variables – function calls – combined with operators 3 / 2 + 5.0 Ans: 6 sin(x) + sqrt(y) 14
  • 15.
    Expressions • Expressions caninclude – values all of the same type 3 + 5 * 12 – 7 Ans: 56 – values of different (compatible) types 1.23 * 18 / 9.5 Ans: 2.33053 • An operation is evaluated according to the types of the operands – if they are the same, the result is the type of the operands – if the operands are different (int and float) then the result is float 15
  • 16.
    Type Casting • Implicitchange of type can occur – when operands are of different type • It is possible to explicitly specify that an expression be converted to a different type static_cast < type > (expression) static_cast <int> (3.5 * 6.9 / 2.1) Ans: 11 16
  • 17.
    Input • Storing datain the computer's memory requires two steps 1. Allocate the memory by declaring a variable 2. Have the program fetch a value from the input device and place it in the allocated memory location x 123 cin >> x 17 int x; 123 will be stored in x
  • 18.
    Allocating Memory • Variable –A memory location whose content may change during program execution • Declaration: – Syntax: type identifier; – Example: double x; (declaration) int y = 45; (declaration and initialization) Note optional initialization of the variable 18
  • 19.
    Allocating Memory • NamedConstant – A memory location whose content cannot be changed • Declaration – Syntax: const type identifier = value; – Example const double PI = 3.14159; Note required initialization of the named constant 19
  • 20.
    Putting Data IntoVariables Data can be put into a variable through either: 1. At initialization time (by programmer) int x=5; 2. Assignment statement (by programmer) – Syntax: variable = expression; – Example x = 1.234; volume = sqr (base) * height; 3. Input (read) statement (by user) – Syntax: cin >> variable ; – Example cin >> height; 20
  • 21.
    Increment and DecrementOperators • Pre‐increment ++x; equivalent to x = x + 1; • Pre‐decrement --x; equivalent to x = x - 1; – Pre‐(increment/decrement): Changes the value before execution of a statement – Post‐(increment/decrement): Changes the value after execution of the statement 21 Post‐increment x++; equivalent to x = x + 1; Post‐decrement x--; equivalent to x = x - 1;
  • 22.
    Output • Values sentto an output device – Usually the screen – Can also be a file or some device • Syntax for screen output: cout << expression << … • Example cout << "The total is "<< sum << endl; Output command Insertion operator Values to be printed New line 22 Text to be displayed
  • 23.
    Output • Escape sequencesalso used to manipulate output cout << "The total ist "<< sum << endl; 23
  • 24.
    Preprocessor Directives • Commandssupplied to the preprocessor – Runs before the compiler – Modifies the text of the source code before the compiler starts • Syntax – start with # symbol – #include <headerFileName> • Example: #include <iostream> 24
  • 25.
    Preprocessor Directives • Notethe preprocessor step in the sequence 25
  • 26.
    Namespace • The #include<iostream> command is where cin and cout are declared • They are declared within a namespace called std • When we specify using namespace std; – Then we need not preface the cin and cout commands with std::cin and std::cout 26
  • 27.
    Program Style andForm • Every program must contain a function called main int main () { … return 0; } • The int specifies that it returns an integer value • Also you can use void main( ) { … } • The void specifies there will be no return value 27
  • 28.
    Program Style andForm • Variables usually declared – inside main – at beginning of program • Use blanks and space to make the program easy for humans to read • Semicolons ; required to end a statement • Commas used to separate things in a list int x,y,z; 28
  • 29.
    Program Style andForm • Documentation – Comments specified between /* this is a comment */ and following // also a comment – Always put at beginning of program /* name, date, cpo, purpose of program */ 29
  • 30.
    Program Style andForm • Names of identifiers should help document program double electricCharge; // instead of ec • Prompt keyboard entry cout << "Enter the value for x -> "; cin >> x; 30