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)

Slides:



Advertisements
Similar presentations
Copyright © 2003 Pearson Education, Inc. Slide 1.
Advertisements

Copyright © 2002 Pearson Education, Inc. Slide 1.
Chapter 2 Flow of Control. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 2-2 Learning Objectives Boolean Expressions Building, Evaluating.
ALGEBRAIC EXPRESSIONS
MULT. INTEGERS 1. IF THE SIGNS ARE THE SAME THE ANSWER IS POSITIVE 2. IF THE SIGNS ARE DIFFERENT THE ANSWER IS NEGATIVE.
Addition Facts
ALGEBRAIC EXPRESSIONS
3-1 Chapter 3 Flow of Control (part a - branching)
1 of 31 Images from Africa. 2 of 31 My little Haitian friend Antoine (1985)
Addition 1’s to 20.
Week 1.
1 If Control Construct A mechanism for deciding whether an action should be taken JPC and JWD © 2002 McGraw-Hill, Inc.
C++ Statements represent the lowest-level building blocks of a program and it may be like:. A simple statement is a computation terminated by a semicolon.
© 2007 Lawrenceville Press Slide 1 Chapter 5 The if Statement  Conditional control structure, also called a decision structure  Executes a set of statements.
1 Chapter 3:Operators and Expressions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 | Last Updated: July 2006 Slide 1 Operators and Expressions.
Chapter 3 Flow of Control Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
If Statements & Relational Operators Programming.
July 13 th.  If/ Else if / Else  Variable Scope  Nested if/else's  Switch statements  Conditional Operator.
Branching Constructs Review l what are branching constructs? what type of branching constructs have we studied? l what is nested if? l what is multiway.
J. P. Cohoon and J. W. Davidson © 1999 McGraw-Hill, Inc. Control Constructs Mechanisms for deciding when and how often an action should be taken.
Chapter 6 Horstmann Programs that make decisions: the IF command.
Boolean Types & Compound Conditionals CSC 1401: Introduction to Programming with Java Lecture 4 – Part 3 Wanda M. Kunkle.
COMP 14 Introduction to Programming Miguel A. Otaduy May 18, 2004.
If Statements. COMP104 If / Slide 2 Three Program Structures * Sequence - executable statements which the computer processes in the given order * Choice.
BOOLEAN LOGIC CSC 171 FALL 2004 LECTURE 7. ASSIGNMENT Review Quiz # 2 Start reading Chapter 5.
CS 117 Spring 2002 Decision Making Hanly Chapter 3 Friedman-Koffman Chapter 4.
CSci 125 Lecture 10 Martin van Bommel. Simple Statements Expression followed by semicolon Assignments total = n1 + n2; Function calls printf(”Hello.\n”);
Computer Science 101 The Boolean System. George Boole British mathematician ( ) Boolean algebra –Logic –Set theory –Circuits –Conditions in if.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 4 Decision Structures and Boolean Logic.
Decision Structures and Boolean Logic
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
Computer Science 210 Computer Organization Introduction to Boolean Algebra.
Selection Boolean What is Boolean ? Boolean is a set with only two values : –true –false true and false are standard identifiers in Pascal, called Boolean.
Chapter 5 Logic; Got Any?. Flow of Control The order in which the computer executes statements in a program Control Structure A statement used to alter.
If…else statements. Boolean Expressions Boolean expression - An expression whose value is either true or false true = 1 false = 0 Datatype: boolean.
1 Compound Assignment C++ has a large set of operators for applying an operation to an object and then storing the result back into the object Examples.
Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement.
3. Controlling Program Flow Methods, parameters, and return values Boolean expressions Conditional branching Loops.
1 © 2002 John Urrutia. All rights reserved. Qbasic Chapter 4 IF Statements and READ & DATA.
1 Chapter 4, Part 1 If Control Construct A mechanism for deciding whether an action should be taken JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S.
Boolean values Gateway to decision making. Background Our problem-solving solutions so far have the straight-line property –They execute the same statements.
Laws of Boolean Algebra Commutative Law Associative Law Distributive Law Identity Law De Morgan's Theorem.
If Statements Programming. COMP104 Lecture 7 / Slide 2 Review: Rules for Division l C++ treats integers different than doubles. 100 is an int. l 100.0,
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Conditions - compare the values of variables, constants and literals using one or more relational.
Copyright Curt Hill The Assignment Operator and Statement The Most Common Statement you will use.
Decisions. Background Our problem-solving solutions so far have the straight-line property –They execute the same statements for every run of the program.
Pascal Programming George Boole, a 19 th Century mathematician, is created with true, false logic. A Boolean expression in Pascal will be true or false.
CONTROL STRUCTURES (SELECTION). PROGRAM COMPONENTS  SEQUENCE  Groups Of Sequential Steps  SELECTION  Making Choices  IF-THEN (one way)  IF-THEN-ELSE.
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)
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 15, 2004 Lecture Number: 11.
Decisions Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Condition – any expression that evaluates to true/false value Relational operators are BINARY.
Rational Expressions relational operators logical operators order of precedence.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
Decision Structures Michele Co CS 101-E. 2 UVa CS101E Spring 2007 Today if statements Logical expressions –Truth tables –Boolean algebra –DeMorgan’s laws.
Computer Science 210 Computer Organization
Chapter 4 : Control Construct.
Rational Expressions. relational operators. logical operators
A mechanism for deciding whether an action should be taken
Branching Constructs Review
Expression Review what is the result and type of these expressions?
Bools and simple if statements
Relational Operators Operator Meaning < Less than > Greater than
Computer Science 210 Computer Organization
Chapter 7 Conditional Statements
Flow Control Statements
Selection Control Structure
COMS 261 Computer Science I
Conditionals.
Presentation transcript:

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

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

Logical Expressions, IF

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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