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

Lecture 3

The document covers fundamental programming concepts in C++, including identifiers, variables, data types, and expressions. It explains the significance of unique identifiers, variable declarations, and the use of the assignment operator. Additionally, it provides examples of reading input from the keyboard and tracing program execution.

Uploaded by

mirzaali56178
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Lecture 3

The document covers fundamental programming concepts in C++, including identifiers, variables, data types, and expressions. It explains the significance of unique identifiers, variable declarations, and the use of the assignment operator. Additionally, it provides examples of reading input from the keyboard and tracing program execution.

Uploaded by

mirzaali56178
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

Programming Fundamentals

By
Anam Javaid

COMSATS University Islamabad, Wah Campus


Previous Lecture
Computer Programming
Algorithm

Pseudo code

Flow Chart

Program Development Cycle


Programming Style and Documentation
Programming Errors
Today’s Lecture

A Simple C++ Program


Reading Input from the Keyboard

Identifiers

Variables

Assignment Statements and Assignment Expressions


What is an Identifier?
• An identifier is the name to denote labels, types, variables, constants or
functions, in a C++ program.
• C++ is a case-sensitive language.
• Work is not work

• Identifiers should be descriptive


• Using meaningful identifiers is a good programming practice
Identifier
• Identifiers must be unique
• Identifiers cannot be reserved words (keywords)
• double main return
• Identifier must start with a letter or underscore, and be followed by zero or
more letters (A-Z, a-z), digits (0-9), or underscores
• VALID
age_of_dog _taxRateY2K
PrintHeading ageOfHorse
• NOT VALID
age# 2000TaxRate Age-Of-Dog main
Variables

14
15
Numerical Data Types
Name Range Storage Size

byte –27 (-128) to 27–1 (127) 8-bit signed

short –215 (-32768) to 215–1 (32767) 16-bit signed

int –231 (-2147483648) to 231–1 (2147483647) 32-bit signed

long –263 to 263–1 64-bit signed


(i.e., -9223372036854775808
to 9223372036854775807)
float Negative range: 32-bit IEEE 754
-3.4028235E+38 to -1.4E-45
Positive range:
1.4E-45 to 3.4028235E+38
double Negative range: 64-bit IEEE 754
-1.7976931348623157E+308 to
-4.9E-324
Positive range:
4.9E-324 to 1.7976931348623157E+308
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
• 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’ ‘?’ ‘*’
ASCI Table

19
What is a Variable?
• A variable is a memory address where data can be stored and
changed.

• Declaring a variable means specifying both its name and its data type.
What Does a Variable Declaration Do?
• A declaration tells the compiler to allocate enough memory to hold a
value of this data type, and to associate the identifier with this
location.

• int ageOfDog;→

• char middleInitial; →
• float taxRate;→
Variable Declaration
• All variables must declared before use.
• At the top of the program
• Just before use.
• Commas are used to separate identifiers of the same
type.
int count, age;
• Variables can be initialized to a starting value when
they are declared
int count = 0;
int age, count = 0;
Trace a Program Execution
#include <iostream> allocate memory
using namespace std; for radius

int main() { radius no value


double radius;
double area;

// Step 1: Read in radius


radius = 20;

// Step 2: Compute area


area = radius * radius * 3.14159;

// Step 3: Display the area


cout << "The area is ";
cout << area << endl;
}

5
Trace a Program Execution
#include <iostream> memory
using namespace std;
radius no value
int main() { area no value
double radius;
double area;
allocate memory
// Step 1: Read in radius for area
radius = 20;

// Step 2: Compute area


area = radius * radius * 3.14159;

// Step 3: Display the area


cout << "The area is ";
cout << area << std::endl;
}
6
Trace a Program Execution
assign 20 to radius
#include <iostream>
using namespace std;
radius 20
int main() {
double radius; area no value
double area;

// Step 1: Read in radius


radius = 20;

// Step 2: Compute area


area = radius * radius * 3.14159;

// Step 3: Display the area


cout << "The area is ";
cout << area << std::endl;
}
7
Trace a Program Execution
#include <iostream> memory
using namespace std;
radius 20
int main() {
double radius;
area 1256.636
double area;

// Step 1: Read in radius compute area and assign it


radius = 20; to variable area

// Step 2: Compute area


area = radius * radius * 3.14159;

// Step 3: Display the area


cout << "The area is ";
cout << area << std::endl;
}
8
Trace a Program Execution
#include <iostream>
memory
using namespace std;
radius 20
int main() {
double radius; area 1256.636
double area;

// Step 1: Read in radius


radius = 20; print a message to the
console
// Step 2: Compute area
area = radius * radius * 3.14159;

// Step 3: Display the area


cout << "The area is ";
cout << area << std::endl;
}
9
Reading Input from the Keyboard
You can use the cin object to read input from the
keyboard. #include <iostream>
int main()
{
// Step 1: Read in radius
double radius;
std::cout << "Enter a radius: ";
std::cin >> radius;
ComputeArea1.cpp
// Step 2: Compute area
double area = radius * radius * 3.14159;

// Step 3: Display the area


std::cout << "The area is " << area << std::endl;

system("PAUSE");
return 0;
}

10
Reading Multiple Input in One Statement
Example : #include <iostream>
Compute average
int main()
of three numbers. {
double x1, x2, x3, average;

std::cin>> x1>> x2>> x3;


average=(x1 + x2 + x3) / 3 ;
std::cout << "The average is " << average << std::endl;

system("PAUSE");
return 0;
}

11
What is an Expression in C++?
• An expression is a valid arrangement of variables, constants, and
operators.
• In C++, each expression can be evaluated to compute a value of a
given type

• In C++, an expression can be:


• A variable or a constant (count, 100)
• An operation (a + b, a * 2)
• Function call (getRectangleArea(2, 4))
What Does a
Variable Declaration Do?
• A declaration tells the compiler to allocate enough memory to hold a
value of this data type, and to associate the identifier with this
location.

• int ageOfDog;→

• char middleInitial; →
• float taxRate;→
Variable Declaration
• All variables must declared before use.
• At the top of the program
• Just before use.
• Commas are used to separate identifiers of the same
type.
int count, age;
• Variables can be initialized to a starting value when
they are declared
int count = 0;
int age, count = 0;
What is an Expression in C++?
• An expression is a valid arrangement of variables, constants, and
operators.
• In C++, each expression can be evaluated to compute a value of a
given type

• In C++, an expression can be:


• A variable or a constant (count, 100)
• An operation (a + b, a * 2)
• Function call (getRectangleArea(2, 4))
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:
0
int count = 0;
int starting; 12345 (garbage)
starting = count + 5;
5
• Expression evaluation:
• Get value of count: 0
• Add 5 to it.
• Assign to starting
Next Lecture

Operators
Evaluating Mathematical Expression

Increment Decrement Operators

You might also like