Chap2 Basic Elements
Chap2 Basic Elements
CHAPTER 2
BASIC ELEMENTS IN C++
Introduction to Computing 2
Outline
1. Program structures
2. Data types and operators
3. Variables and declaration statements
Introduction to Computing 3
Program structures
• A C++ program is a collection of functions.
• A function is a program segment that transforms the data
it receives into a finished result.
b result
axb
Program structures
• Module: a small segment which is designed to perform a
specific task
Modules can be functions
• Modular program: a program consisting of interrelated
segments (modules) arranged in a logical and
understandable form
Easy to develop, correct, and modify
Function
• Each function must have a name.
• Rules for names or identifiers in C++:
First character must be an uppercase or lowercase ASCII letter or
an underscore (_).
Only letters, digits, or underscores may follow the initial letter (no
blanks allowed)
Special characters, such as $, &, * or %, are not allowed
Reserved words – keywords cannot be used for variable names.
C++ is a case-sensitive language.
Introduction to Computing 7
Keyword
Example of identifiers
• Find invalid identifiers
if YouAreBeautiful while
_ intersect hello
[email protected]
Introduction to Computing 9
Example of identifiers
• Find invalid identifiers
if YouAreBeautiful while
_ intersect hello
[email protected]
Introduction to Computing 10
A simple program
#include <iostream> Header file
using namespace std; (Preprocessor directives)
int main()
{
cout << "Hello world!";
return 0;
}
• A header file is a file with an extension of .h that is
included as part of a program. It notifies the compiler that
a program uses run-time libraries.
• The iostream classes are used for giving C++ programs
input and output capabilities.
• The header file for the iostream class is iostream.h.
Introduction to Computing 15
Preprocessor directives
• Preprocessor directives: starts with a #. The preprocessor
is a program that runs before the compiler.
• #include <file name> : causes the named file to be
inserted into the source code
• C++ provides a standard library with many pre-written
classes that can be included
• Header files: files included at the head (top) of a C++
program
• Preprocessor directives and include statements allow the
current file to use any of the classes, functions, variables,
and other code contained within the included file.
Introduction to Computing 16
i/o manipulator
• An i/o manipulator is a special function that can be used
with an i/o statement.
• The endl i/o manipulator is part of iostream classes and
represents a new line character.
• Example:
cout << “Program type: console application” << endl;
cout << “Create with: Visual C++“ << endl;
cout << “Programmer: Don Gesselin” << endl;
Introduction to Computing 18
Comments
• Comments: explanatory remarks in the source code added
by the programmer
• Line comments: begin with two slashes (//) before the
comment text
Line comment can be on a line by itself, or at the end of a line of code
Line comment cannot be longer than one line
Introduction to Computing 19
Comments
• Block comments span multiple lines.
• Block comments begin with /* and end with */.
Ex:
Data Types
• A data type is the specific category of information that a
variable contains.
• There are three basic data types used in C++: integers,
floating point numbers and characters.
Introduction to Computing 21
Arithmetic Operators
• Arithmetic operators: perform mathematical calculations, such
as addition, subtraction, multiplication, and division.
Operator Description
+ Add two operands
- Subtracts one operand from another operand
* Multiplies one operand by another operand
/ Divides one operand by another operand
% Divides two operands and returns the remainder
#include <iostream>
using namespace std;
int main()
{
cout << "15.0 plus 2.0 equals" << (15.0 + 2.0) << '\n'
<< "15.0 minus 2.0 equals" << (15.0 - 2.0) << '\n'
<< "15.0 times 2.0 equals" << (15.0 * 2.0) << '\n'
<< "15.0 divided by 2.0 equals" << (15.0 / 2.0) << '\n';
return 0;
}
Introduction to Computing 26
Operator Associativity
unary - Right to left precedence
*/% Left to right
+- Left to right
• Example:
8+5*7%2*4
↓ ↓ ↓ ↓
4 1 2 3
Introduction to Computing 28
Variables
• One of the most important aspects of programming is storing
and manipulating the values stored in variables.
• Variable names are also selected according to the rules of
identifiers
• Example: Some valid identifiers
my_variable (python style)
MyVariable (pascal style)
myVariable (camel style , suggested style for C++)
Temperature
x1 x2
_my_variable
• Some invalid identifiers are as follows:
%x1 %my_var @x2
Introduction to Computing 29
Declaration Statements
• Declaration statement: specifies the data type and
identifier of a variable; sets up the memory location
• Syntax: <dataType> <variableName>;
• Data type is any valid C++ data type
• Example:
int count;
float sum;
• Declarations may be used anywhere in a function; usually
grouped at the opening brace
Introduction to Computing 30
Assignment statements
• Assignment statement: used to store a value into a
variable
• Value of the expression on the right side of the = is
assigned to the memory location of the variable on the left
side of the =
• Examples:
num1 = 45;
num2 = 12;
total = num1 + num2;
Introduction to Computing 32
Example
#include <iostream> The output of the program:
The average grade is 91.25
using namespace std;
int main()
{
float grade1 = 85.5;
float grade2 = 97.0;
float total, average;
total = grade1 + grade2;
average = total/2.0; // divide the total by 2.0
cout << "The average grade is " << average << endl;
return 0;
}