Expressions and Order of Operations Operators – There are the standard operators: add, subtract, divide, multiply – Note that * means multiply? (No times.

Slides:



Advertisements
Similar presentations
Logic & program control part 2: Simple selection structures.
Advertisements

Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
All the Operators. Precedence An operator with higher precedence is done earlier (prededes) one with lower precedence –A higher precedence is indicated.
All the Operators. Precedence An operator with higher precedence is done earlier (prededes) one with lower precedence –A higher precedence is indicated.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
COMP 14 Introduction to Programming Miguel A. Otaduy May 18, 2004.
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
Primitive Data Types There are exactly eight primitive data types in Java four of them represent integers: byte (class Byte), short (class Short), int.
1 Midterm Review COMP 102. Tips l Eat a light meal before the exam l NO electronic devices (including calculators, dictionaries, phones, pagers, etc.)
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
1 MATERI PENDUKUNG OPERATOR Matakuliah: M0074/PROGRAMMING II Tahun: 2005 Versi: 1/0.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 30, 2005.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
C++ Operators CS242 COMPUTER PROGRAMMING T.Banan Al-Hadlaq.
CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05.
CSci 125 Lecture 10 Martin van Bommel. Simple Statements Expression followed by semicolon Assignments total = n1 + n2; Function calls printf(”Hello.\n”);
2440: 211 Interactive Web Programming Expressions & Operators.
Flow of Control Part 1: Selection
Assignment Statements Operator Precedence. ICS111-Java Programming Blanca Polo 2 Assignment, not Equals  An assignment statement changes the value of.
C++ Basics Tutorial 6 Operators. What are going to see today? Assignment operator(=) Arithmetic operators(+,-,*,/,%) Compound assignment(+=,-=,*=……..)
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Operators Precedence - Operators with the highest precedence will be executed first. Page 54 of the book and Appendix B list C's operator precedence. Parenthesis.
Computer Engineering 1 st Semester Dr. Rabie A. Ramadan 3.
15-Nov-15 All the Operators. operators.ppt 2 Precedence An operator with higher precedence is done earlier (precedes) one with lower precedence A higher.
PHY-102 SAPVariables and OperatorsSlide 1 Variables and Operators In this section we will learn how about variables in Java and basic operations one can.
Python Conditionals chapter 5
Assignment statement: Assigns a value to a variable Variable must appear on the left side, value on the right side of the assignment operator Right side.
ICT Introduction to Programming Chapter 4 – Control Structures I.
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
CONTROL STRUCTURE. 2 CHAPTER OBJECTIVES  Learn about control structures.  Examine relational and logical operators.  Explore how to form and evaluate.
Operators.
© 2007 Pearson Addison-Wesley. All rights reserved2-1 Character Strings A string of characters can be represented as a string literal by putting double.
COP 2551 Introduction to Object Oriented Programming with Java Topics –Java Statements –Java Expressions –Postfix Expressions –Prefix Expressions –Evaluating.
Programming Principles Operators and Expressions.
Chapter 2: Data and Expressions. Variable Declaration In Java when you declare a variable, you must also declare the type of information it will hold.
Inside Class Methods Chapter 4. 4 What are variables? Variables store values within methods and may change value as the method processes data.
Operators Chapter 4 - Lecture Slides Math, Conditional operators, conversions They aren’t all just objects to me, some of them are primitive.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
CompSci 230 S Programming Techniques
Sequence, Selection, Iteration The IF Statement
Selections Java.
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Arithmetic operations & assignment statement
Multiple variables can be created in one declaration
CMSC201 Computer Science I for Majors Lecture 03 – Operators
Operators and Expressions
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
All the Operators 22-Nov-18.
All the Operators 4-Dec-18.
CSC215 Lecture Flow Control.
Summary Two basic concepts: variables and assignments Basic types:
Chapter 4: Control Structures I (Selection)
The System.exit() Method
Control Structure Chapter 3.
SE1H421 Procedural Programming LECTURE 4 Operators & Conditionals (1)
Chapter 2 Programming Basics.
All the Operators 6-Apr-19.
All the Operators 13-Apr-19.
Chap 7. Advanced Control Statements in Java
CSC215 Lecture Control Flow.
Control Structure.
The boolean type and boolean operators
Presentation transcript:

Expressions and Order of Operations Operators – There are the standard operators: add, subtract, divide, multiply – Note that * means multiply? (No times symbol on the keyboard) – Java has many other operators (Next slide) Operands – Can be a literal (A constant that we don’t have to declare) – Can be a variable – Can be a call to a method call that returns a value (Ex: int x = 3 + Math.round(4.4); ) Order of operations (The text has a complete list) – Multiply and division happen before add and subtract – Use parentheses to alter the order Definition: An expression is a sequence of operators and operands Important Note: Equal does NOT mean equal, it means assign

Other Operators in Java Prefix ++: Add one, very high precedence int x = 3; int y = ++x; // x and y becomes a 4. Postfix ++: Add one, very low precedence Int x = 3; int y = x++; // x becomes 4, y becomes 3. +=, -=, *=, /=: Adjust the previous value int x = 3; x += 4; // x becomes 7 Int x = 6; x /= 5; // x becomes 1 %: Compute the remainder int x = 7 % 4; // x becomes a 3 (remainder of 7/4) int x = 7; x %=4; // x becomes 3 (remainder of x/4) Definition : Precedence is a number indicating which operators get applied first

Assignment Operator Java evaluates what is on the right of the equal sign, and then stores into the variable on the left You can have more than one assignment in a statement Examples 1.x = 3 + 4; // Add and store the result in x 2.int x = 3; y = x + 4; // Add 4 to what’s in x and store in y. 3.x = x + 3; // Add 3 to x and store in x 4.x = y + Math.round(3.4); // Round 3.4 and add to y and then store in x. * Equal does not mean equal; it means assign * The call to the Math.round() method returns a value

Bitwise Operators Consider a byte: b = , c = byte d = b & c puts a 4 in d (one bit only if both corresponding bits on) 2.byte d = c | 5 puts a 7 in d (one bit if either of the corresponding bits on) 3.byte d = c ^ 5 puts a 3 in c (one bit if corresponding bits not equal) 4.! Flips the bits (!b becomes 75) &(and), |(or), ^(xor), !(not) We’ll discuss these examples in class

Ternary operator Most operators use two operands, one to the left of the operator and one to the right (ex: a+b) Java has one ternary operator – Example: (The following statement stores a 10 into x) int x = (5<6)? 10 : 20; – Example: int x = 5; System.out.println( (x++<6)? "hello" : "goodbye"); System.out.println((x<6)? "hello": "goodbye"); (The first line above prints "hello", second prints "goodbbye")

Logical operators Int x = 3; double y = 3.1, z = 3.0; String s="abc", t="abcdef", u="abb" Examples 1.boolean b = (x<y); 2.boolean b = (x<y && x<=z); 3.boolean b = (x>y || x<z); 4.boolean b = !(x>y || y<z); 5.boolean b = (s<t); 6.boolean b = (s<u); And (&&), OR (||), Not (!) Answers: true, true, false, true, true, false Note: s<t and s<u will not work in practice because we are comparing object addresses and not contents

Basic Control Structures Sequence Structure 1.Do this 2.Do that 3.Do something else 4.Do that again 5.Do another thing Condition or Transfer Structure IF this THEN do that ELSE do the other thing Iteration or Loop WHILE this true DO something DO something UNTIL this false FOR many times DO something All computer processing can be done this way

Condition Statement Syntax (Java Grammar) If (boolean expression) { ** true part: any number of Java statements ** } else { ** false part: any number of java statements ** } Notes 1.The braces are not needed if there is only one statement in the block 2.The else part is optional Do one thing if an expression is true, and another if it is false

Condition Statement Example // Convert military time to standard time // Assume hours has the current military hours String str; hours = hours %12; if (hours == 0) hours = 12; if (hours <=12) str = " a.m. "; else { str = " p.m. "; } // {} not required here System.out.println("It is " + hours + str); There is a bug in this logic; do you see it?

Nested If /* Assume month has the current month stored in it and years has the current year */ int days; if (month == 9 || month == 4 || month == 6 || month == 11) days = 30; else { if (month == 2) { if (year %4 == 0) days = 29; else days = 28; } else days = 31; } System.out.println("month " + month + " has " + days + " days");

Another Example Find maximum of five numbers if it is greater than 10 Variables we will use are: first, second, third, fourth, fifth maximum = first; if (second > maximum) maximum = second; if (third > maximum) maximum = third; if (fourth > maximum) maximum = fourth; if (fifth > maximum) maximum = fifth; if (maximum > 10) System.out.println(maximum); else System.out.println("Maximum less than 10");

Review What are the three structures that we can use to program any application, no matter how complicated? What are the following operators for (%, ?:, and !)? What is the difference between x++ and ++x? What is the difference between || and |? What is the ternary operator? What does the term precedence mean? What is the difference between = and ==? What is a condition statement? When are braces needed in a condition statement? What is a bitwise operator?