Week 2
Week 2
CC111
Saleha Batool
Lecturer KUST
UMT Sialkot
Adding Integers
Programming Fundamentals 2
Variable Declarations
A variable is a location in the computer’s memory where a value can
be stored for use by a program.
These declarations specify that the variables number1, number2 and
sum are data of type int, meaning that these variables will hold
integer values.
The identifiers number1, number2 and sum are the names of
variables.
Can also be declared as
int number1, number2, sum;
Programming Fundamentals 3
Data Types
Four fundamental types
Int
Float
Double
Character
Fundamental types names are keywords and therefore must appear in
all lowercase letters.
Programming Fundamentals 4
Identifiers
Identifiers are the names you assign to variables, functions, classes,
objects, etc. They are user-defined names used to identify various
program elements.
Purpose: They provide unique names for entities like variables,
functions, arrays, etc., in the code.
Examples: x, myVariable, getData, myClass, totalSum, etc.
Programming Fundamentals 5
Keywords
Keywords are reserved words predefined by the C++ language that
have specific meanings and purposes. You cannot use them for
anything other than their intended use.
Purpose: Keywords are part of the C++ syntax and are used to define
the structure and behavior of programs.
Examples: int, float, if, else, while, return, class, public, private,
namespace, etc.
Programming Fundamentals 6
Variable Declaration rules
Start with a letter or underscore: Variable names must begin with a letter (a-z, A-
Z) or an underscore (_).
Can contain letters, digits, and underscores: After the first character, variable
names can include letters, digits (0-9), and underscores.
Case-sensitive: Variable names are case-sensitive (e.g., score and Score are
different variables).
No special characters or spaces: Variable names cannot contain special
characters (e.g., @, #, $, etc.) or spaces.
Cannot be a keyword: Variable names cannot be C++ keywords (e.g., int, return,
class).
Length limit: While there is no strict limit, it's best to keep variable names
reasonably short and meaningful for readability.
Descriptive names: Use descriptive names to make your code more
understandable (e.g., totalScore instead of ts).
Placement of variable: Declarations of variables can be placed almost anywhere
in a program, but they must appear before their corresponding variables are used
in the program 7
Variable Initialization
Initialization ensures that a variable has a defined value, which
prevents issues that can arise from using uninitialized variables (like
garbage values).
Syntax: In C++, you can initialize a variable using the assignment
operator (=) at the time of declaration.
Example:
int x = 10;
float y = 5.5;
Programming Fundamentals 8
Class Practice
Write a program to add three integers.
Write program to calculate area of circle.
Programming Fundamentals 9
Arithmetic Operators
The arithmetic operators are all binary operators, i.e., operators that
take two operands.
Programming Fundamentals 10
Equality and Relational Operators
Programming Fundamentals 11
Operator precedence and associativity
Programming Fundamentals 12
Operator associativity
Previous table]
Programming Fundamentals 13