Week 3 FOP
Week 3 FOP
PROGRAMMING
CS-114
Lecture 2: Variables
Basic Program (main Function)
2
What is a computer?
3
Components:
input (mouse, keyboard)
network
Computer
CPU Memory Devices
Control Input
Datapath Output
Components of a Computer
The BIG Picture Same components for
all kinds of computer
Desktop, server,
embedded
Input/output includes
User-interface devices
Display, keyboard, mouse
Storage devices
Hard disk, CD/DVD, flash
Network adapters
For communicating with other
computers
Below Your Program
Application software
Written in high-level language
System software
Compiler: translates HLL code to machine
code
Operating System: service code
Handling input/output
Managing memory and storage
Scheduling tasks & sharing resources
Hardware
Processor, memory, I/O controllers
Last Lab
// Example program
#include <iostream>
using namespace std;
int main()
{
cout << "Hello, World!" << endl;
cout << "I am a master programmer.";
}
Include Directives
• Include Directives add library files to our programs
- To make the definitions of the cin and cout available to the program:
#include <iostream>
We can receive input from the user by using the cin (Standard input
stream
Example
int ftemp;
cin>>ftemp;
Location 0
Location 1
Location 2 Each location is
Location 3 1 byte of memory
CPU Location 4
1 byte = 8 bits
Location 5
Session 2 7
What Makes a Variable
• Variable has three important characteristics:
- Type
• How much memory do a variable need.
- This information is determined by a type.
- Name
• How to differentiate a variable with another variable of the
same type.
- Name refers to the memory location assigned to this variable.
- Value
• What is the value?
- The actual value contained by a variable.
Session 2 8
An Example of a Variable
int temperature = 35
Session 2 9
Type of a Variable
• Among other advantages a ‘type’ binds the
memory to a variable name.
• The type int is of 4 bytes in C++.
2,147,483,647 value.
• It can also hold values in negative down to
-2,147,483,648.
Session 2 12
Example of a Variable
(Memory View)
int temperature = 35
00000000 Location 0
Locations 0 - 3 are collectively 00000000 Location 1
called as ‘temperature’ 00000000 Location 2
00100011 Location 3
Location 4
100011 is the binary equivalent of 35 Location 5
Session 2 10
Changing the Value of Variable
• Lets change the value of ‘temperature’.
temperature = 45902
00000000 Location 0
Locations 0 - 3 are collectively 00000000 Location 1
called as ‘temperature’ 10110011 Location 2
01001110 Location 3
Location 4
Location 5
1011001101001110 is the binary equivalent of 45902
Session 2 11
Initializing Variables
• Declaring a variable does not give it a value
- Giving a variable its first value is initializing the variable
• Variables are initialized in assignment statements
4 bytes are used to hold the value before the decimal point
4 bytes for the value after the decimal point.
Session 2 14
Relative Comparison of int and double
int numPeople = 2;
Session 2 15
Premitive Data Types in C++
Integral Types
represent whole numbers and their negatives
declared as int, short, or long
Character Types
represent single characters
declared as char
Stored by ASCII values
Boolean Type
declared as bool
has only 2 values true/false
will not print out directly
Floating Types
represent real numbers with a decimal point
declared as float, or double
Scientific notation where e (or E) stand for “times 10 to the ” (.55-e6)
Samples of C++ Data Values
int sample values
4578 -4578 0
bool values
true false
x = 5671
Session 2 18
Constants
• Constants are values which cannot be
modified e.g. the value of Pi
• To declare a constant in C++, we write a
Session 2 19
Variable Types
To use a variable in our code
First, we must have to declare it, variable are known with its
keywords. “Use the name of keyword” for declaration of a
variable”.
Example: int number1 = 10;
Example: float floatData = 20.93;
Example: long int myData = -10;
Example: char textData = ‘A’;
Example: bool boolData = true;
#include <iostream>
#include <iostream>
using namespace std;
using namespace std;
void main ()
void main ()
{
{
int number1 = 10;
int number1 = 20, number2 = 10;
float floatData = 20.93;
}
}
Points to remeber in naming the
variables
The names given to variables (and other program
features) are called identifiers.
What are the rules for writing identifiers?
You can use upper- and lowercase letters, and the
digits from 1 to 9.
You can also use the underscore (_).
The first character must be a letter or underscore.
(it cannot be a digit)
No spaces allowed in a variable name
Primitive Data Types
• So far the variable types that we have
studied are primitive data types.
• Primitive data types only have a memory
27
char constants
• Character constants are enclosed in single quotes
bool old_enough;
Acknowledgements
1. Deitel and Deitel: C++ How to Program, 7th Edition, Prentice Hall Publications
Material in these slides has been taken from, the following resources
40