Introduction To: The ++ Language
Introduction To: The ++ Language
The ++
language
The C++ language
#include <iostream>
int main (void)
{
cout <<Hello World!! \n;
return 0;
} /* main */
Structure of a program in C
/* This program is to
introduce students to C++
Pre-processor directives for the first time */
Global declarations
#include <iostream>
The main( ) function int main (void)
Local declarations {
Statements cout <<Hello World!! \n;
return 0;
Other functions
} /* main */
Structure of a program in C++
Preprocessor Directives
Global Declarations
int main ( void)
{
Local declarations
Statements
}
Statements
}}
Preprocessor Directives
Starts with a pound sign (#)
Commands to prepare for translation into
machine language, to look for special code
libraries
Examples of directives:
#include - To access or link to functions from
selected libraries through header files
E.g. #include <stdio.h>
Attach header file called stdio.h
To allow use of standard I/O functions
#define to define data constants
Global Declarations
e.g. e.g.
names of e.g. ;
variables: int, return &&
sum, i
Identifiers in C++
Names used to represent data memory
areas
variables
constants
Identifiers are
names of
functions
data definition
Rules on Identifiers in C++
Variables Constants
Examples:
int Counter = 0;
Constants
Values that do not/cannot change during
execution of a program
Types of constants:
Named Constant
Defined constant
Memory constant
Literal constant
Defined Constants
Uses preprocessor directive #define:
Syntax:
#define ConstantName Value
Example
#define PI 3.14159256
Convention: use uppercase for constant
names
Notice: No semicolon
Memory Constants
Declared together with other variables
Syntax:
const datatype CONSTANTNAME = Value;
Example
const double PI = 3.14159256;
Array
Enumerated types
Derived Data Types
Pointer
Structure
Union
Statements in C
Causes an action to be performed by the
program
Terminated by semicolon ( ; )
Assignment statements
Function statements
Types of
Statements
Control statements
Null statements
Assignment statements
Syntax:
variable = operand;
(operand can be constant, a variable or a valid C expression)
Syntax:
VariableName = Value / literal;
VariableName = Variable;
VariableName = Expression;
VariableName = Value returned by function;
Example:
year = 1988;
sum = sales;
sum = sales + tax;
Use of Assignment statements
Counting/incrementing
variable = variable + fixedvalue;
i = i + 1;
Terminated by semicolon
Does nothing
Used where a statement is syntactically
required, but no action is called for
Expressions
Made of combination of :
operators
operands
Operators: Symbols used to represent a
certain operation
Operands: variable or constant name, value /
literal, value returned by function
Type of operands must match
Mathematical Expressions
Types of mathematical expression
Integer Expression
Floating-Point Expression
Mixed Mode Expression
Integer Expression
E.g. num = num + 10;
Floating-Point Expression
E.g. price = price * (1 0.2);
Mixed Mode Expression
E.g. num = num * 2.5;
Types of Expressions
Logical/Boolean Mathematical
Expressions Expressions
evaluated to evaluated to a
True or False numerical value
Operators
Symbols used to represent a certain
mathematical or logical operation
Operator symbols:
Mathematical: ( ), *, /, %, +,
Logical: >, <, >=, <=, ==, !=
Binary operators versus Unary operators
Binary: 2 + 5
Unary: -7
Precedence and Associativity:
The priority by which expressions are
evaluated in a compounded expression
Operators Precedence
*/%
+-
Brackets are used to override precedence
Expression of same precedence are
evaluated according to associativity
Left to right
Right to left
Exercises on Precedence
If x = 10, and y = 20, what are the values for
expressions below:
x + 5 * y 12 = ?
(x + 5) * y 12 = ?
x + 5 * (y 12) = ?
(x + 5) * (y 12) = ?
Exercises on Precedence
c+5*d2=?
(c + 5) * d 2 = ?
Use of Escape Sequence
Tells the compiler to escape from the
character normal interpretation
\n new line
\b backspace
\t tab
\f form feed
***To display Backslash(\) itself: use \\
Example:
printf (Number is %f \n, average);
Documentation in C programs
Example:
/* This is a comment
for my first program */
Example:
int firstnum; // declare first number
End of Slides