[go: up one dir, main page]

0% found this document useful (0 votes)
6 views40 pages

Week 3 FOP

The document provides an overview of fundamental programming concepts, specifically focusing on variables and their characteristics in C++. It explains the components of a computer, types of data, variable declaration, initialization, and the use of input/output streams. Additionally, it covers primitive data types, assignment operators, and naming conventions for identifiers.
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)
6 views40 pages

Week 3 FOP

The document provides an overview of fundamental programming concepts, specifically focusing on variables and their characteristics in C++. It explains the components of a computer, types of data, variable declaration, initialization, and the use of input/output streams. Additionally, it covers primitive data types, assignment operators, and naming conventions for identifiers.
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/ 40

FUNDAMENTALS OF

PROGRAMMING
CS-114
Lecture 2: Variables
Basic Program (main Function)
2
What is a computer?
3
 Components:
 input (mouse, keyboard)

 output (display, printer)

 memory (disk drives, DRAM, SRAM, CD)

 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>

• Using Directives include a collection of defined names

- To make the names cin and cout available to our program:

using namespace std;


Input and Output
• A data stream is a sequence of data
- Typically in the form of characters or numbers

• An input stream is data for the program to use


- Typically originates
• at the keyboard
• at a file

• An output stream is the program’s output


- Destination is typically
• the monitor
• a file
Output using cout
• cout is an output stream sending data to the monitor
• The insertion operator "<<" inserts data into cout

• Example: cout << number_of_bars << " candy bars\n";


- This line sends two items to the monitor
• The value of number_of_bars
• The quoted string of characters " candy bars\n"
- Notice the space before the ‘c’ in candy
- The ‘\n’ causes a new line to be started following the ‘s’ in bars
• A new insertion operator is used for each item of output
Receiving Information from the user

 We can receive input from the user by using the cin (Standard input
stream

Example
int ftemp;
cin>>ftemp;

 causes the program to wait for the user to type in a number.


 The resulting number is placed in the variable ftemp.
 The keyword cin (pronounced “C in”) is an object, predefined in C++ to
correspond to the standard input stream.
 This stream represents data coming from the keyboard
 The >> is the extraction or get from operator.
 It takes the value from the stream object on its left and places it in the
variable on its right.
Designing Input and Output
• Prompt the user for input that is desired
- cout statements provide instructions

cout << "Enter your age: ";


cin >> age;
• Notice the absence of a new line before using cin
Simplistic View of a Computer
Very Simplistic View of a Computer

Location 0
Location 1
Location 2 Each location is
Location 3 1 byte of memory
CPU Location 4
1 byte = 8 bits
Location 5

Each bit is an electric


impulse carrying 1 or 0.

This simplistic view is enough to explain the basic concepts


of programming to students
5
Value
• The only task a computer can do is arithmetic e.g.
multiplying, dividing, subtracting, etc.
• Therefore, everything in the computer is represented as a
value
- Numbers, letters, characters, etc are all represented as values
• Values could change depending on their nature. For
example
- the temperature today is different from the temperature yesterday
- The number of cars inside Lahore is different then the number of in
cars Islamabad.
Identifiers
 C++ programs can be written using many English
words
 It is useful to think of words found in a program as
being one of three types:
 Reserved Words: words such as if, int and else etc are
predefined, their meanings can not be changed
 Library Identifiers: These words are supplied default
meanings by the programming environment
cin, cout and sqrt
 Programmer-supplied Identifiers: words "created" by
the programmer, called variables
Variable
• To store a value inside a computer a‘variable’ is used.
• A variable is a space in the memory to store
a value.
• This space is reserved until the variable is
required.

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

Type of the variable is integer (written as “int” in C++)

int temperature = 35

A name of the variable


An initial value of the variable

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++.

1 byte = 8 bits=>4 bytes=32 bits


232 =4294967296
• Therefore, it can hold maximum of

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

double mpg; // declare the variable


mpg = 26.3; // initialize the variable

• Declaration and initialization can be combined using two methods


- Method 1
double mpg = 26.3, area = 0.0 , volume;
- Method 2
double mpg(26.3), area(0.0), volume;
Variable for Real Numbers
•int cannot hold a real value.
• Therefore, a type “double” is used to hold real values.

• Double takes 8 bytes of memory instead of 4 bytes.


Out of the 8 bytes in a double,

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;

Reserves 32 bits (4 bytes)


and sets the value stored
in that space to 2. The name
‘numPeople’ is associated with
this space.

double bill = 32.45;

Reserves 64 bits (8 bytes)


and sets the value stored
in that space to 32.45. The name
‘bill’ is associated with this space.

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

float sample values


95.274 95.0 .265

char sample values


‘B’ ‘d’ ‘4’ ‘?’ ‘*’
Variable types

Variable type Keyword used in Size in bits Range


declaration
integer int 32 bits -2147483648 to
2147483647
Short integer short int 16 bits -32768 to 32767

Long integer long int 32 bits -2147483648 to


2147483647
Floating point data float 32 bits -1.0x1038 to
1.0x1038
Floating point data double 64 bits -1.0x10308 to
(with large fraction) 1.0x10308
Text type data char 8 bits -128 to 127
Boolean data bool 1 bit 1 or 0
(True or False)
Session 2 16
Assignment Operator
 An operator to give (assign) a value to a variable.
 Denote as ‘=‘
 Only variable can be on the left side.
 An expression is on the right side.
 Variables keep their assigned values until changed
by another assignment statement or by reading in
a new value.
Assignment Operator Syntax
 Variable = Expression
 First,
expression on right is evaluated.
 Then the resulting value is stored in the memory location
of Variable on left.

NOTE: An automatic type coercion occurs after


evaluation but before the value is stored if the
types differ for Expression and Variable
Assignment Operator Mechanism
 Example:
int count = 0; 0

int starting; 12345 (garbage)


starting = count + 5;
 Expression evaluation:
 Get value of count: 0
 Add 5 to it.
 Assign to starting
5
Assignment Statement

x = 5671

5671 is written at the memory location reserved for x

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

keyword “const” before the variable type.

const double pi = 3.14;

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

space for storing values.

27
char constants
• Character constants are enclosed in single quotes

char letter = 'a';

• Strings of characters, even if only one character


is enclosed in double quotes
- "a" is a string of characters containing one character
- 'a' is a value of type character
Reading Character Data
• cin skips blanks and line breaks looking for data
• The following reads two characters but skips
any space that might be between

char symbol1, symbol2;


cin >> symbol1 >> symbol2;

• User normally separate data items by spaces


J D
• Results are the same if the data is not separated
by spaces
JD
Type bool
• bool is a new addition to C++
- Short for boolean
- Boolean values are either true or false

• To declare a variable of type bool:

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

2. Robert Lafore: Object-Oriented Programming in C++, Fourth Edition,


December 2001,Sams Publishing .
3. A Structured Programing Approach Using C++ by Behrouz A. Forouzan
4. www.cplusplus.com

40

You might also like