Download presentation

Presentation is loading. Please wait.

Published bySabrina Hern Modified over 2 years ago

1
What is the Result and Type of the Following Expressions? int x=2, y=15;double u=2.0,v=15.0; -xx+yx-y x*vy / xx/yy%xx%y u*vu/vv/uu%v x * u(x+y)*uu /(x-x) x++u++ u = --xu = x-- u *= ++x;v /= x; 1

2
Types l are these constants legal?.5 5.E 0.5e30.5E-30.5E-3.5 ’a’’%’’Ab’”Ab” l are these variable names legal? MyTotalMy_Total __MyTotal My Total l what does this mean? int mytotal=0;int yourtotal(1); what would be stored in myvar ? int myvar; myvar=2.56; what would be stored in yourvar ? double yourvar; yourvar=5/2; 2

3
Logical Expressions, IF

4
Boolean Algebra l logical expressions have the one of two values - true or false n a rectangle has three sides. n the instructor has a pleasant smile l the branch of mathematics that deals with this type of logic is called Boolean algebra n developed by the British mathematician George Boole in the 19th century l C++ makes extensive use of Boolean algebra in the form of logical expressions; what is an expression again? l three key logical operators in C++: && - logical “and”, binary || - logical “or”, binary ! - logical “not”, unary 4

5
Boolean Algebra l truth tables n Lists all combinations of operand values and the result of the operator for each combination truth table for && (logical “and’) PQ P && Q False FalseFalse False True False True FalseFalse True TrueTrue 5

6
Boolean Algebra truth table for || (logical “or”) PQ P || Q False FalseFalse False TrueTrue True FalseTrue True TrueTrue 6

7
Boolean Algebra truth table for ! (logical “not”) P! PP! P False True True False 7

8
Boolean Algebra l can create complex logical expressions by combining simple logical expressions l example ! (P && Q) l a truth table can be used to determine when a logical expression is true note that & and | are also legal operators, make sure to use correct ones PQ P && Q ! (P && Q) False FalseFalseTrue False TrueFalseTrue True FalseFalseTrue True TrueTrueFalse 8

9
Example Logical Expressions bool P = true; bool Q = false; bool R = true; bool S = P && Q; bool T = P && !Q bool U = !Q || R; bool V = P || !Q || !R; bool W = P && Q && !R; bool X = Q || (P && R); bool Y = !(R && !Q); bool Z = !(P && Q && R); 9

10
Relational Operators l equality operators == note the two equal signs != l examples int i = 32; int k = 45; bool q = i == k; bool r = i != k; 10

11
Relational Operators l ordering operators < > >=( ) <=( ) l examples int i = 5; int k = 12; bool p = i < 10; bool q = k > i; bool r = i >= k; bool s = k <= 12; 11

12
Operator Precedence Expanded l precedence of operators (from highest to lowest) () Unary + - * / % + - > = >= != == && || = 12

13
Examples of Logical Expressions l int a = 5;int b = 10int c = 20; l bool d = a b; l bool f = (a > b) || (b < c ); l bool g = (a > b) && (b < c ); l bool h = !(a < b);bool i = !(a==b); l bool j = 2*a == b;bool k = (a+b) >= c; l bool l = !((a+b) != c); l bool m = (a+b) == (c-a); bool n = (a+b) >= (c-a); l int o=a; int p=o=b; what is the outcome of this statement? l bool q=true;q = d = false; 13

14
Operator Precedence Revisited l same or different? (a*b)+ca*b + c a*(b+c)a*b + c (a+b) > ca + b > c a+(b>c)a + b > c (a > b) == (b > c)a > b == b > c (a == b) > (b == c)a == b > b == c (a != b) && (c <= d)a != b && c <= d (a > b) && (c || d)a > b && c || d (a = b) && ca = b && c 14

15
Conditional Constructs l provide ability to control whether a statement is executed l two constructs if -statement –if –if-else –if-else-if switch -statement 15

16
Blocks and Local Variables l a list of statements enclosed in curly brackets is called a block l block may be placed anywhere a statement can be placed (note the placement of brackets: if ((saleType == ’W’) || (saleType == ’w’)) { total = price * number; } l a variable can be declared and used within block, such variable is local to the block and does not exist outside of it else if ((saleType == ’R’) || (saleType == ’R’)){ double subtotal; subtotal = price * number; total = subtotal + subtotal * TAX_RATE; } variable scope – area in program where a variable can be used what’s the scope of a variable local to function? block? l pitfall: a local variable is accessed outside of the block 16

17
The Basic If-Statement l syntax if ( expression ) action l if the expression is true then execute action l action is either a single statement or a block example 1: if (value > 0) value =0; l example 2: if (value < 0) { value = -value; ++i; } expression action truefalse 17

18
Sorting Two Numbers cout << "Enter two integers: "; int n1, n2; cin >> n1 >> n2; if (n1 > n2) { int tmp = n1; n1 = n2; n2 = tmp; } cout << ”Numbers in order: “ << n1 << " " << n2 << endl; programming idiom – a common way of accomplishing a simple task swapping values of two variables with a third is an idiom 18

19
The If-Else Statement l syntax if ( expression ) action1 else action2 l if expression is true then execute action1 otherwise execute action2 if (v == 0) cout << "v is 0"; else cout << "v is not 0"; expression action1 action2 true false 19

20
Selection l it is often the case that depending upon the value of an expression we want to perform a particular action l two major ways of accomplishing this n multiway if-statement –if-else statements “glued” together n switch statement 20

21
Multiway If-Statement l example int vclass; cout << "Enter the vehicle class: "; cin >> vclass; if (vclass == 1) cout << ”Passenger car”; else if (vclass == 2) cout << ”Bus”; else if (vclass == 3) cout << ”Truck”; else cout << ch << ”Unknown vehicle class!”; 21

22
Switch Statement syntax switch ( expression ) { case constant : statements break; case constant : statements default: statements } 22

23
Switch Example 1 int vclass; cout << "Enter the vehicle class: "; cin >> vclass; switch (vclass){ case 1: cout << "Passenger car"; break; case 2: cout << "Bus"; break; default: cout << "Unknown vehicle class! "; break; // unnecessary but used for consistency } 23

24
cout << "Enter simple expression: "; int Left; int Right; char Operator; cin >> Left >> Operator >> Right; cout << Left << " " << Operator << " " << Right << " = "; switch (Operator) { case '+' : cout << Left + Right << endl; break; case '-' : cout << Left - Right << endl; break; case '*' : cout << Left * Right << endl; break; case '/' : cout << Left / Right << endl; break; default: cout << "Illegal operation" << endl; } Switch Example 2 24

25
Arity and Conditional Operator l ternary operator – operator accepting three operands l conditional operator is used as an abbreviated form of branching boolean-expression ? true-expression : false-expression l if boolean-expression is true, then the value of whole expression is true- expression, or false-expression otherwise l conditional assignment - if conditional operator is used to assign value to variable n what branching construct is this assignment equivalent to? example: int i = j>0 ? j : -j; l program that calculates the largest number ( of two) int main() { int n1, n2; cin >> n1 >> n2; int max = n1 > n2 ? n1 : n2; cout << ”maximum is ” << max << endl; } l arity (again) – number of operands an operator accepts? What arities have we studied? 25

26
Named Constants l there are problems with using literal constants 9.8 does not give an idea as to what it means in the program n hard to modify if used in multiple places in program l named constant provides a name to a constant: const int windowCount = 5; const double taxRate = 9.8; l named constants are usually declared at the beginning of the program. capital letters are usually used to give the programmer a hint that this is a constant when he encounters it in the program note to C programmers: #define is completely replaced by const in C++ 26

27
Debugging and Tracing Programs l specially compiled executables leave information the original source file: names of variables and source lines l this allows n program tracing – suspending program execution at specific source line and executing program one source line at a time n variable watching – observing values stored in source program variables l breakpoint – line in the source program where execution has to be suspended 27

Similar presentations

OK

© 2007 Lawrenceville Press Slide 1 Chapter 5 The if Statement Conditional control structure, also called a decision structure Executes a set of statements.

© 2007 Lawrenceville Press Slide 1 Chapter 5 The if Statement Conditional control structure, also called a decision structure Executes a set of statements.

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Ppt on different solid figures faces Ppt on 2nd world war records Ppt on action words for class 1 Ppt on power grid failure in india 2012 Ppt on tcp ip protocol layers Seminar ppt on geothermal energy Ppt on earth and space current Ppt on french revolution class 9 Download ppt on fundamental rights and duties of a citizen Free ppt on the road not taken