Lecture 2 of Programming
Lecture 2 of Programming
Lecture 2 of Programming
Lecture 2
Introduction to Programming
Outside of a Computer
Session 2 2
Inside of a Computer
Session 2 3
Simplistic View of a Computer
Session 2 4
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
Session 2 6
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
int temperature = 35
Session 2 9
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
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
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
ession 2 14
Relative Comparison of int and double
int numPeople = 2;
Session 2 15
Session 2 16
Manipulating Variables
• Assignment Statement
- In Mathematics the value x = x + 1 is not
possible why?
- In C++ x = x +1 is possible because “=” is an
assignment operator and not an equality
operator.
- Assignment operator means that the contents of
the right hand side is transferred to the memory
location of the left hand side.
Sessio 2 17
Assignment Statement
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
Reserved Words
•Some names cannot be declared as variable names
because they are reserved words in C++
Session 2 20
Manipulating Values
• Mathematical Operators
- Common mathematical operators are available
in C++ for manipulating values e.g. addition(+),
subtraction(-), multiplication(*), division(/),
and modulus (%).
•C++ has many other operators also which
we will study in due course.
Sessio 2 21
FEEDBACK QUIZ
Session 2 22
What is result of the following
expression?
4*3+8/2+2*4
1a) 2.75
1b) 24
1c) 96
1d) 36
fq:1?
Arithmetic Expression Evaluation
ession 2 25
Evaluating an Expression
6+2*3/6
• Three operators are in this expression.
• However, * and / both have the same precedence and + has
lower precedence then these two.
• * and / will be evaluated first but both have the same
precedence level.
• Therefore, operator associatively will be used here to
determine the first to get evaluated i.e. left to right.
• The left most sub expression will be evaluated followed by
the next right one and so on.
Session 2 26
Primitive Data Types
• So far the variable types that we have
studied are primitive data types.
• Primitive data types only have a memory
Session 2 27
Type char
• Computers process character data too
• char
Slide 2- 29
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
bool old_enough;
Manipulator
•Manipulators are instructions to the output
stream that modify the output in various ways
-endl is a manipulator that causes a linefeed to
be inserted into the stream
-cout<<“Sample text”<<endl;
-setw is another manipulator that sets the field
width of the output
Casting
•Converting a value of one type into another
type
•Manual Casting
• static_cast<double> (intVar)
•Automatic Casting
• Automatic conversion of lower order type to higher
order type when two operands of different types are
encountered in the same expression
Operator Shorthand
• Some expressions occur so often that C++
contains to shorthand operators for them
• All arithmetic operators can be used this way
- += count = count + 2; becomes
count += 2;
- *= bonus = bonus * 2; becomes
bonus *= 2;
- /= time = time / rush_factor; becomes
time /= rush_factor;
- %= remainder = remainder % (cnt1+ cnt2); becomes
remainder %= (cnt1 + cnt2);
INPUT AND OUTPUT
33
Input and Output
• A data stream is a sequence of data
- Typically in the form of characters or numbers
#include <iostream>
• Example:
cin >> v1 >> v2 >> v3;
• Flow of control
- The order in which statements are executed
• Branch
- Lets program choose between two alternatives
Branching Example
Slide 2- 49
Implementing the Branch
• if-else statement is used in C++ to perform
a
branch
• if (boolean expression)
true statement
else
false statement
• When the boolean expression is true
• Example: if ( ( x = = 1) | | ( x = = y) )
- True if x contains 1
- True if x contains the same value as y
- True if both comparisons are true
NOT
• ! -- negates any boolean expression
- !( x < y)
• True if x is NOT less than y
- !(x = = y)
• True if x is NOT equal to y
NOT
if ( x < y < z )
Pitfall: Using = or ==
• ' = ' is the assignment operator
- Used to assign values to variables
- Example: x = 3;
• '= = ' is the equality operator
- Used to compare values
- Example: if ( x == 3)
• The compiler will accept this error:
if (x = 3)
but stores 3 in x instead of comparing x and 3
- Since the result is 3 (non-zero), the expression is true
Compound Statements
• A compound statement is more than one
statement enclosed in { }
• Branches of if-else statements often need to
execute more that one statement
• Example: if (boolean expression)
{
true
statements
else}
{
false
statements
}
Branches Conclusion
• Can you
- Write an if-else statement that outputs the word
High if the value of the variable score is greater
than 100 and Low if the value of score is at most
100? The variables are of type int.
} while (boolean_expression);
Increment/Decrement
• Unary operators require only one operand
- + in front of a number such as +5
- - in front of a number such as -5
• ++ increment operator
- Adds 1 to the value of a variable
x ++;
is equivalent to x = x + 1;
• -- decrement operator
- Subtracts 1 from the value of a variable
x --;
is equivalent to x = x - 1;
Sample Program
• Bank charge card balance of $50
• 2% per month interest
• If-else statement
• Simple loops
Session 2 70