0% found this document useful (0 votes)
8 views7 pages

TUT 1

Uploaded by

ralirendy89
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views7 pages

TUT 1

Uploaded by

ralirendy89
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Basic Elements of C++

Computer program
– Sequence of statements whose objective is to accomplish a
task

• A C++ program is a collection of functions, one of which is the


function main
• The first line of the function main is called the heading of the function:
– int main()

Programming
– Process of planning and creating a program

Algorithm
• An algorithm is a step-by-step procedure or a set of rules designed
to solve a specific problem or accomplish a particular task.( It is like a
recipe that outlines a clear and finite series of actions to achieve a
desired outcome.)

Function
• Collection of statements when executed accomplishes something.

Syntax rules: rules that specify which statements (instructions)


are legal or valid

Semantic rules: determine the meaning of the instructions


Programming language: a set of rules, symbols, and
special words

Prompt lines: executable statements that inform the user what


to do
cout << "Please enter a number between 1 and 10 and "
<< "press the return key" << endl;
cin >> num;

Variable: a memory location whose contents can be changed


Named constant: memory location whose content can’t
change during execution

Data type: set of values together with a set of operations


• C++ data types fall into three categories:
– Simple data type
– Structured data type
– Pointers
• Three categories of simple data

– Integral: integers (numbers without a decimal)


• Can be further categorized:
• char, short, int, long, bool, unsigned char, unsigned
short, unsigned int, unsigned long

– Floating-point: decimal numbers


– Enumeration type: user-defined data type

• int
• float
• double
• char
• const
• void
• return

• Identifier: the name of something that appears in a program


– Consists of letters, digits, and the underscore character (_)

– Must begin with a letter or underscore


• C++ is case sensitive
– NUMBER is not the same as number
• Two predefined identifiers are cout and cin
• A C++ program contains two types of statements:
– Declaration statements: declare things, such as variables
– Executable statements: perform calculations, manipulate data,
create output, accept input, etc.
• C++ program has two parts:
– Preprocessor directives
– The program
• Preprocessor directives and program statements constitute C++
source code (.cpp)
• Compiler generates object code (.obj)
• Executable code is produced and saved in a file with the file
extension .exe
• Syntax rules: indicate what is legal and what is not legal
• Errors in syntax are found in compilation
int x; //Line 1
int y //Line 2: error
double z; //Line 3
y = w + x; //Line 4: error

• Comments are for the reader, not the compiler


• Two types:
– Single line: begin with //
// This is a C++ program.
// Welcome to C++ Programming.
– Multiple line: enclosed between /* and */
/*
You can include comments that can
occupy several lines.
*/

• Token: the smallest individual unit of a program written in any


language
• C++ tokens include special symbols, word symbols, and identifiers
• Special symbols in C++ include:

• Precision: maximum number of significant digits


– Float values are called single precision
– Double values are called double precision
• Maximum number of significant digits (decimal places) for float
values: 6 or 7
• Maximum number of significant digits for double: 15

• Arithmetic expressions: contain values and arithmetic


operators

• Operands: the number of values on which the operators will


work
• Operators can be unary (one operand) or binary (two operands)
• C++ arithmetic operators:
– + addition
– - subtraction
– * multiplication
– / division
– % modulus (or remainder) operator
• +, -, *, and / can be used with integral and floating-point data types
• Use % only with integral data types

• Implicit type coercion: when value of one type is


automatically changed to another type

• Cast operator: provides explicit type conversion


static_cast<dataTypeName>(expression)
• Increment operator: increase variable by 1
– Pre-increment: ++variable
– Post-increment: variable++

• Decrement operator: decrease variable by 1


– Pre-decrement: --variable
– Post-decrement: variable--

x = 5; x = 5;
y = x++; y = ++x;

• The syntax of cout and << is:


– Called an output statement
The stream insertion operator

You might also like