Expression Review what is the result and type of these expressions?

Slides:



Advertisements
Similar presentations
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)
Advertisements

If Statements & Relational Operators Programming.
True or false A variable of type char can hold the value 301. ( F )
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.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
Week 4 Selections This week shows how to use selection statements for more flexible programs. It also describes the various integral types that are available.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
If Statements. COMP104 If / Slide 2 Three Program Structures * Sequence - executable statements which the computer processes in the given order * Choice.
1 Midterm Review COMP 102. Tips l Eat a light meal before the exam l NO electronic devices (including calculators, dictionaries, phones, pagers, etc.)
Chapter 4 Making Decisions
C++ for Engineers and Scientists Third Edition
1 CS 105 Lecture 5 Logical Operators; Switch Statement Wed, Feb 16, 2011, 5:11 pm.
Quiz 1 Exam 1 Next Week. Nested if Statements if (myGrade >= 80) if (myGrade >= 90) cout
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
Introduction to Programming (in C++) Data and statements Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream.
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
CSE 332: C++ execution control statements Overview of C++ Execution Control Expressions vs. statements Arithmetic operators and expressions * / % + - Relational.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
Chapter 05 (Part III) Control Statements: Part II.
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.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
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.
Chapter Making Decisions 4. Relational Operators 4.1.
Loops cause a section of a program to be repeated a certain number of times. The repetition continues while a condition remains true. When a condition.
Boolean values Gateway to decision making. Background Our problem-solving solutions so far have the straight-line property –They execute the same statements.
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 © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions 1.
Lecture 6 – Selection FTMK, UTeM – Sem /2014.
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.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Condition – any expression that evaluates to true/false value Relational operators are BINARY.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
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.
Chapter 3 Control Statements
Chapter 4 : Control Construct.
Selection (also known as Branching) Jumail Bin Taliba by
Predefined Functions Revisited
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
Chapter 4: Making Decisions.
A mechanism for deciding whether an action should be taken
EGR 2261 Unit 4 Control Structures I: Selection
Branching Constructs Review
Chapter 4: Making Decisions.
Control Structures – Selection
Conditinoal Constructs Review
COMS 261 Computer Science I
Chapter 4: Control Structures I (Selection)
Conditinoal Constructs Review
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
Chapter 7 Conditional Statements
Flow Control Statements
Selection Control Structure
Summary Two basic concepts: variables and assignments Basic types:
Chapter 4: Control Structures I (Selection)
Boolean Expressions to Make Comparisons
Predefined Functions Revisited
COMS 261 Computer Science I
CprE 185: Intro to Problem Solving (using C)
Chap 7. Advanced Control Statements in Java
CSC215 Lecture Control Flow.
Module 3 Selection Structures 6/25/2019 CSE 1321 Module 3.
Controlling Program Flow
Presentation transcript:

Expression Review what is the result and type of these expressions? int x=2, y=15; double u=2.0, v=15.0; -x x+y x-y x*v y / x x/y y%x x%y u*v u/v v/u u%v x * u (x+y)*u u /(x-x) x++; u++; u = --x; u = x--; u *= ++x; v /= x;

Constants and Variables Review are these constants legal? .5 5.E 0.5e3 0.5E-3 0.5E-3.5 ’a’ ’%’ ’Ab’ ”Ab” are these variable names legal? MyTotal My_Total __MyTotal My Total what does this mean? int mytotal=0; int yourtotal(1); what would be stored in myvar? int myvar; myvar=2.56; is this code correct? const int i=55; ++i; what would be stored in yourvar? double yourvar; yourvar=5/2;

Logical Expressions, IF

Boolean Algebra Boolean (logical) expressions can have two values true or false branch of mathematics that deals with this type of logic is called Boolean algebra developed by British mathematician George Boole in the 19th century logical operators in C++ && - logical “and”, binary || - logical “or”, binary ! - logical “not”, unary

Truth Table for && truth table - lists all combinations of operand values and the result of the operator for each combination truth table for && (logical “and’) p q p && q false false false false true false true false false true true true

Truth Tables for II and ! p !p false false false false true p q p || q false false false false true true true false true true true true p !p false true true false

Boolean Algebra ! (p && q) p q p && q !(p && q) false false false true can create complex logical expressions by combining with subexpressions example ! (p && q) 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 p q p && q !(p && q) false false false true false true false true true false false true true true true false

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);

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

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

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

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

Operator Precedence Revisited same or different? (a*b)+c a*b + c a*(b+c) a*b + c (a+b) > c a + 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) && c a = b && c

Conditional Constructs provide ability to control whether a statement is executed constructs if-statement if if-else if-else-if switch-statement conditional statement

Blocks and Local Variables a list of statements enclosed in curly brackets is called a block block may be placed anywhere a statement can be placed (note the placement of brackets: if ((saleType == ’W’) || (saleType == ’w’)) { total = price * number; } 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 * taxRate; variable scope – area in program where a variable can be used what’s the scope of a variable local block? accessing variable outside the block is an error

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

Sorting Two Numbers cout << "Enter two integers: "; int n1, n2; cin >> n1 >> n2; if (n1 > n2) { const 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

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

Nested Ifs nested if - one if is the body or inside the block of another outer if – enclosing if inner If – enclosed if if (n1 > n2) // outer if if (n2 > n3) // inner if is the body of outer if cout << n3; // no need for curly brackets if (n1 > n2) { // outer if i++; if (n2 > n3) // inner if inside block of outer if cout << n3; } nesting may be more than two if-s deep

Selection want to carry out actions depending on the value of an expression two major ways to do this multiway if-statement if-else statements “glued” together switch statement

Multiway If-Statement 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!”;

Switch Statement syntax switch (expression){ case constant: statements break; default: statements } semantics expression is evaluated, execution continues in first matching case (optional) default matches any expression value break-statement terminates the switch statement, execution continues with a statement following switch-block if case does not end with break, execution continues to next case expression of any “countable” type (int, char) literal or named constant of same type as expression

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

Switch Example 2 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; }

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

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