Lec11_Expressions &Control Structures
-
Upload
maryam-kausar -
Category
Documents
-
view
229 -
download
0
Transcript of Lec11_Expressions &Control Structures
-
7/31/2019 Lec11_Expressions &Control Structures
1/40
One major use of AND operation is for
placing 0s in one part of a bit pattern while
not disturbing the other part.
For example 00001111 without knowing the second operand we can say
that the first four most significant bits will be 0s
Moreover the four least significant bits will be a
copy of the second operand.
2-1
-
7/31/2019 Lec11_Expressions &Control Structures
2/40
00001111
AND 1010101000001010
2-2
-
7/31/2019 Lec11_Expressions &Control Structures
3/40
This use of the AND operation is called
masking.
Here one operand, called the mask,
determines which part of the other operandwill affect the result.
In case of AND operation, masking produces a
result that is a partial replica of the one
operand, with 0s occupying thenonduplicated positions.
2-3
-
7/31/2019 Lec11_Expressions &Control Structures
4/40
Such an operation is useful when
manipulating a bit map,
A string of bits in which each bit represents the
presence or absence of a particular object
We have encountered bit maps in context of
representing images, where each bit is
associated with a pixel.
2-4
-
7/31/2019 Lec11_Expressions &Control Structures
5/40
Where AND operation can be used to
duplicate a part of bit string while placing 0s
in the nonduplicated part
The OR operation can be used to duplicate apart of a string while putting 1s in the
nonduplicated part
For example 11110000
Produces 1s in four most significant bits While remaining bits are copied in the four least
significant bits.
2-5
-
7/31/2019 Lec11_Expressions &Control Structures
6/40
11110000
OR 1010101011111010
2-6
-
7/31/2019 Lec11_Expressions &Control Structures
7/40
A major use of XOR operation is in the
forming of complement of a bit string
XORing any byte with a mask of all 1s
produces the complement of the byte.
2-7
-
7/31/2019 Lec11_Expressions &Control Structures
8/40
11111111
XOR1010101001010101
2-8
-
7/31/2019 Lec11_Expressions &Control Structures
9/40
Suppose you want to isolate the middle 4 bits
of a byte by placing 0s in the other 4bits
without disturbing the middle 4bits. What
mask must you use together with thatoperation?
1-9
00000000
00111100
-
7/31/2019 Lec11_Expressions &Control Structures
10/401-10
00111100Apply masking with?
AND
Because in AND operations we put
0s where we dont want to change
the bits.
-
7/31/2019 Lec11_Expressions &Control Structures
11/40
Expressions / mixed expressions and
casting (Implicit and Explicit casting),
Post fix and Pre fix Operators
Control structures. If, if-else, nested if,
-
7/31/2019 Lec11_Expressions &Control Structures
12/40
How expressions involving mixed types of dataare evaluated.
int value1=10;
long value2 =25L;
float value3= 30.0f;
double result=value1+value2+value3;
The value of result is calculated as the sumof three different types of variables.
For each add operation, one of the operandswill be converted to the type of the otherbefore addition can be carried out.
12
-
7/31/2019 Lec11_Expressions &Control Structures
13/40
The statement in the previous slide is actuallyexecuted with the steps: value1+ value2 is calculated by convertingvalue1 to type long before the addition. Theresult is also of type long, so the calculation is10L+25L=35L
The next operation is 35L+value3. Theprevious result, 35L is converted to type float
so 35.0f+30.0f=65.0f
Finally 65.0f is converted to double an stored inresult.
13
-
7/31/2019 Lec11_Expressions &Control Structures
14/40
The rules for dealing with mixed
expressions apply only when the types of
the operands for a binary operations are
different. Rules in the sequence in whichthey apply are as following:
If either operand is of type long double, the
other us converted to long double.
If either operand is of type double, the otherus converted to double.
If either operand is of type float, the other
us converted to float.
14
-
7/31/2019 Lec11_Expressions &Control Structures
15/40
Any operand of type char, signed char,unsigned char, short or unsigned short
is converted to type int, as long as type int can
represent all values of the original operand type.Otherwise, the operand is converted to unsigned
int.
An enumeration type is converted to the first int,
unsigned int, long or unsigned long that
accomodates the range ofenum.
If either operand is of type unsigned long, theother us converted to unsigned long.
15
-
7/31/2019 Lec11_Expressions &Control Structures
16/40
If one operand is type long and the other isunsigned int, then provided type long can
represent all values it is converted to type long
otherwise unsigned long.
If either operand is of type long, the other usconverted to long.
16
-
7/31/2019 Lec11_Expressions &Control Structures
17/40
The basic idea is very simple. With twooperands of different types the type with thelesser range of values is converted to theother. The formal rules roughly boil down to:
if the operation involves two different floating pointtypes, the one with the lesser precision will bepromoted to the other.
If the operations involves an integer and a floatingpoint value, the integer will be promoted to the
floating point type. If the operation involves mixed integer types, the
type with the more limited range will be promoted tothe other.
If the operation involves enumerations they will be
converted to a suitable integer type.17
-
7/31/2019 Lec11_Expressions &Control Structures
18/40
-
7/31/2019 Lec11_Expressions &Control Structures
19/40
short a=2000;
int b;
b = (int) a; // c-like cast notation
b = int (a); // functional notation
19
-
7/31/2019 Lec11_Expressions &Control Structures
20/40
Expression LHS Value
int1 = 7; int2 = 2;float1 = int1 / int2 + 5; 8.0
int1 = 3.8; 3
float2 = float(int1)/int2 +5 8.5
float1 = 5; 5.0
Implicit castingType casting
-
7/31/2019 Lec11_Expressions &Control Structures
21/40
Both the increment operator (++) and the
decrement operator(--) come in two
varieties:
Prefix and postfix. The prefix variety is written before the variable
name (++myAge);
Variable is changed, then the expression it is in is evaluated.
The postfix variety is written after(myAge++)
Expression the variable is in executes, then the variable is
changed.
21
-
7/31/2019 Lec11_Expressions &Control Structures
22/40
Increment operator (++)
Increment variable by one
c++
Same as c += 1
Decrement operator (--) similar
Decrement variable by one
c--
22
-
7/31/2019 Lec11_Expressions &Control Structures
23/40
Ifc = 5, then
cout
-
7/31/2019 Lec11_Expressions &Control Structures
24/40
24
-
7/31/2019 Lec11_Expressions &Control Structures
25/40
25
-
7/31/2019 Lec11_Expressions &Control Structures
26/40
26
3 control structures
Sequence structure
Programs executed sequentially by default
Selection structures
if, if/else, switch
Repetition structureswhile, do/while, for
-
7/31/2019 Lec11_Expressions &Control Structures
27/40
27
C++ keywords
Cannot be used as identifiers or variable namesC++ Keywords
Keywords common to theC and C++ programming
languagesauto break case char const
continue default do double else
enum extern float for goto
if int long register return
short signed sizeof static struct
switch typedef union unsigned void
volatile while
C++ only keywordsasm bool catch class const_cast
delete dynamic_cast explicit false friend
inline mutable namespace new operator
private protected public reinterpret_cast
static_cast template this throw true
try typeid typename using virtual
wchar_t
-
7/31/2019 Lec11_Expressions &Control Structures
28/40
28
Flowchart
Graphical representation of an algorithm
Special-purpose symbols connected by arrows
(flowlines)
Rectangle symbol (action symbol)
Any type of action
Oval symbol
Beginning or end of a program, or a section of code
(circles)
Single-entry/single-exit control structures
Connect exit point of one to entry point of the
next
-
7/31/2019 Lec11_Expressions &Control Structures
29/40
29
Selection structure
Choose among alternative courses of action
Pseudocode example:
If students grade is greater than or equal to 60
Print Passed
If the condition is true
Print statement executed, program continues to next
statement
If the condition is false Print statement ignored, program continues
Indenting makes programs easier to read
C++ ignores whitespace characters (tabs, spaces, etc.)
-
7/31/2019 Lec11_Expressions &Control Structures
30/40
30
Translation into C++If students grade is greater than or equal to 60
Print Passed
if ( grade >= 60 )cout
-
7/31/2019 Lec11_Expressions &Control Structures
31/40
31
Flowchart of pseudocode statement
-
7/31/2019 Lec11_Expressions &Control Structures
32/40
32
if Performs action if condition true
if/else Different actions if conditions true or false
Pseudocodeif students grade is greater than or equal to 60print Passed
else
print Failed
C++ codeif ( grade >= 60 )cout
-
7/31/2019 Lec11_Expressions &Control Structures
33/40
Ternary conditional operator (?:)
Three arguments (condition, value iftrue,
value iffalse)
Code could be written:cout= 60 ? Passed : Failed );
truefalse
print
Failed
print
Passed
grade >= 60
Condition Value if true Value if false
-
7/31/2019 Lec11_Expressions &Control Structures
34/40
34
Nested if/else structures
One inside another, test for multiple cases
Once condition met, other statements skippedif students grade is greater than or equal to 90
Print Aelse
if students grade is greater than or equal to 80Print B
else
if students grade is greater than or equal to 70Print C
else
if students grade is greater than or equal to 60Print D
else
Print F
-
7/31/2019 Lec11_Expressions &Control Structures
35/40
35
Example
if ( grade >= 90 ) // 90 and above
cout = 80 ) // 80-89
cout = 70 ) // 70-79
cout = 60 ) // 60-69
cout
-
7/31/2019 Lec11_Expressions &Control Structures
36/40
36
Compound statement Set of statements within a pair of bracesif ( grade >= 60 )
cout
-
7/31/2019 Lec11_Expressions &Control Structures
37/40
What is wrong with the following if
statement
1-37
if numNeighbors >= 3
++numNeighbors;
cout
-
7/31/2019 Lec11_Expressions &Control Structures
38/40
Describe the output
1-38
int number = 4;
double alpha = -1.0;if (number > 0)
if (alpha > 0)
cout
-
7/31/2019 Lec11_Expressions &Control Structures
39/40
1-39
int number = 4;
double alpha = -1.0;
if(number > 0)
{ if(alpha > 0)cout
-
7/31/2019 Lec11_Expressions &Control Structures
40/40
Using if-else statement write program that
calculates the following:
Addition
Subtraction
Multiplication
Division
When a user writes addition is adds and so on