0% found this document useful (0 votes)
3 views

lecture 2

The document provides an overview of computer programming concepts, focusing on variables, identifiers, and data types in C++. It explains how to declare variables, assign values, and the importance of using meaningful names, as well as the different data types and operators available in C++. Additionally, it covers input/output streams, namespaces, and escape sequences.

Uploaded by

samy2mexa
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)
3 views

lecture 2

The document provides an overview of computer programming concepts, focusing on variables, identifiers, and data types in C++. It explains how to declare variables, assign values, and the importance of using meaningful names, as well as the different data types and operators available in C++. Additionally, it covers input/output streams, namespaces, and escape sequences.

Uploaded by

samy2mexa
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/ 23

Computer programming

Introduction
Variables and assignment
• variable are used to name and store data.
• value is any type of data held by the variable.
• Variables are implemented as Memory locations assigned by the compiler
• The value of the variable Is kept in the memory location assigned to that variable.
Identifiers
• Identifier : name of the variable
• An identifier must start with a letter or an underscore
• Other characters can be letters, digits or the underscore symbol
• C++ is a case-sensitive language;
• eg Rate | RATE | rate => are different variables
Rules for writing an identifier

1. The first letter should be either be a letter or underscore

2. Identifiers shouldn’t be keywords


Keywords or Reserved words are words that have predefined meaning in c++ and they’re
defined in the libraries.

Example: cin , cout, int, include

3. There shall be no space between words in the identifier

4. To make your program easy to understand, u should always use


meaningful names for variables.
Variable declarations

• Declaring a variable is telling the complier the kind of data to be stored in the
variable.
• For example:
int number_of_bars; => integer type

double one_weight , total_weight; => double type

• When there is more than one declarations they are separated by comma.

• Each declarations ends with a semicolon.

• All variables must be declared before use


Assignment Statements
• Assignment statement : a direct way to change the value of a
• For example:
total_weight = one_weight * number_of_bars;

• This assignment statement sets the value of total_weight to the value of one_weight multiplied by the
value of number_of_bars.

• An assignment statement always consists of


• a variable on the left-side of the equal sign
• an expression on the right-side
• ends with a semicolon.
• Example of assignment
• total_weight = one_weight;
• weight = one_weight + 3;

• A variable can be initialized at the time of declaration


• E.g. int a = 6;
2.2 Input and output
An input stream : the stream of input that is being An output stream : the stream of output generated
by the program
fed to the program

• Output using cout


• Input using cin
• Output to the screen
• Input from user
• For example
• For example
• cout << ”Please enter your name: ”;
• string firstName
• cout << “the total cost is $” << (price + tax);
cin >> firstName
• cout << first name << “ “ << second number;

• The arrow notation << is often called the


insertion operation.
INCLUDE DIRECTIVES AND NAMESPACES
#include <iostream> - These two lines make the library iostream available.

Using namespace std; - iostream library includes the definition of cin and cout and others

• C++ divides names into namespaces.

• A namespace is a collection of names, such as the names cin and cout.

• Using directive : a statement that specifies a namespace by “using”.


• i.e. using namespace std;

• This statement says that your program is using the std (standard) namespace.
Escape sequences
• The \ preceding a character tells the compiler that the character
following the \ doesn’t have the same meaning as the character itself.

• Such a sequence is called an escape sequence


Data type
• Data-types of a variable restricts the type of data to be stored on the variable at declaration.

• At variable declaration in C++, the compiler allocates memory based on the variable’s data-type
• Every data type requires a different amount of memory.
Data type
• Primitive Data Types:

• are built-in or predefined data types

• can be used directly by the user to declare variables

• Derived Data Types:

• They are derived from the primitive or built-in data types

• Abstract or User-Defined Data Types:

• are defined by user


Primitive Data type
• Integer:
• Keyword used for integer data types is int
• Integers typically requires 4 bytes of memory space
• ranges from -2147483648 to 2147483647

• Character:
• Keyword used for character data type is char
• Characters typically requires 1 byte of memory space
• ranges from -128 to 127 or 0 to 255

• Boolean:
• used for storing boolean or logical values (true or false)
• Keyword used for boolean data type is bool
Primitive Data type …
• Floating Point:
• used for storing single precision floating point values or decimal values

• Keyword used for floating point data type is float

• Float variables typically requires 4 byte of memory space

• Double Floating Point:


• used for storing double precision floating point values or decimal values

• Keyword used for double floating point data type is double

• Double variables typically requires 8 byte of memory space

• Valueless or Void:
• Void means without any value.

• void datatype represents a valueless entity.

• Void data type is used for those function which does not returns a value.
Data type Modifiers
Operators
• An operator is a symbol that tells the compiler to perform specific
mathematical or logical manipulations.
• Arithmetic Operators
• Assignment Operators
• Relational Operators
• Logical Operators
• Bitwise Operators
• Misc Operators
Operators …
Arithmetic Operators
Operators …
Assignment Operators
Operators …
Assignment Operators …
Operators …
Relational Operators
Operators …
Logical Operators
Operators …
Bitwise Operators
Operators …
Postfix vs Prefix

• Postfix - returns the value and then performs the operation


• Eg int a = 2;
• Cout <<a; => this gives out 2
• Cout<<a++; => this gives out 2
• Cout<<a; => this gives out 3
• Prefix - performs the operation and returns the value
• Eg int a = 2;
• Cout <<a; => this gives out 2
• Cout<<++a; => this gives out 3
• Cout<<a; => this gives out 3
Precedence of operators

You might also like