Slide 1 Side Effects Duration: 00:00:53 Advance mode: Auto

Size: px
Start display at page:

Download "Slide 1 Side Effects Duration: 00:00:53 Advance mode: Auto"

Transcription

1 Side Effects The 5 numeric operators don't modify their operands Consider this example: int sum = num1 + num2; num1 and num2 are unchanged after this The variable sum is changed This change is called a side effect Java also has a few other side-effect operators Increment/decrement, shorthand assignment Slide 1 Side Effects Duration: 00:00:53 Hello. Welcome to the CS 170, Java Programming 1 lecture on Side Effects. The five numeric operators we looked at in the last lesson all have one thing in common: none of them modify their operands. Let me show you what I mean. When you use the addition operator to add two numbers together, like this: int sum = num1 + num2; the operands, num1 and num2, are not changed at all; the variable sum, which stores the results of the calculation, is changed, however. This kind of change is called a side effect. Side effects occur whenever an expression changes the value of one of its operands. The assignment operator produces a side effect, as do the increment and decrement operators, and the various shorthand assignment operators. Let's start by first reviewing the assignment operator. In Java, assignment is an operator, not statement The job of the assignment operator is to: Copy the value on its right into the variable on its left, and return the result copied as its value The variable on the left is changed when this happens Because of the side-effect, left-side must be a variable Slide 2 Assignment Review Duration: 00:00:45 Assignment Review A variable, you'll recall, is a memory location that contains a value. So, the most common thing you'll do with your variables is to change the value that they contain. You do this through assignment. The assignment operator is a binary operator whose operation is to: Copy the value on the right into the variable on the left, and return the result copied as its value When you apply the assignment operator, the fact that the variable on the left is changed is a side effect of the assignment (although for most of us, it's really the main effect). Because of this side-effect, the operand on the left side of the assignment operator must be a variable; it cannot be a constant or a method invocation. CS 170 Lecture: Side Effects Page 1 of Stephen Gilbert

2 Kinds of Assignment The assignment operator is used in two situations 1) to give an initial value to a new variable at creation int x = 5; // initialize i to 5 This is a declaration, not an executable statement It can appear outside of any method 2) to copy a value into an existing variable x = 23; // copy (store) the value 23 in location x Executable statement; must appear inside a method Slide 3 Kinds of Assignment Duration: 00:00:44 Assignment and Equality Assignment is not equality (like algebra) x = x + 1; // OK in Java, nonsense in algebra Means that the operand on the left must be a variable 13 = a + b; // OK in algebra, nonsense in Java Java uses a different operator to test equality if ( a == 5 )... ; // Not ( a = 5 ) Slide 4 Assignment and Equality Duration: 00:01:15 We can use the assignment operator in two different situations. When you first create a variable, you can give it an initial value using the assignment operator. When you use the assignment operator as part of a variable declaration like this, it is not considered an executable statement, which means that it can appear outside of any method. You used such initializing assignments to provide an initial value for your instance variables. You can give an existing variable a new value by using an executable assignment. Executable assignments must be placed inside a method, just like all other executable statements. Finally, remember that assignment is not equality. You might find this confusing because in math the "equals" sign used in an equation does not mean "copy"; instead it means that the value on the left and the value on the right are identical. Here's an example. In Algebra, you could never say x = x + 1 because for every value of x, x + 1 is never equal to x. In Java, however, this statement simply means to add x and 1 together, and then store the result of the operation back into the variable x. This works both ways. In Algebra, 13 = a + b is legal because there are several values for a and b that equal 13. In Java, though, it is illegal (and nonsense), because 13 is not a variable that can be changed. When you use the assignment statement, remember, the right-hand side must be a variable. Java does have an operator that checks for equality, though, used in if statements and loops. Instead of a single equals sign, Java's equality operator uses a pair of equal signs like this. CS 170 Lecture: Side Effects Page 2 of Stephen Gilbert

3 The Value of Assignment Since assignment is an operator, it produces a value Different than VB or Pascal where it is a statement The value of assignment is the value that is copied System.out.println(a = 3); Exercise 1: Let's see what happens with this Create an int variable a initialized to 0 Type this statement into Code Pad (with semicolon) Type the variable name, no semicolon Slide 5 The Value of Assignment Duration: 00:01:32 Now, let's look at something new about the assignment operator. Since assignment is an operator, it produces a value, unlike languages like Pascal or Visual Basic where assignment is purely a statement that carries out the copy operation. The value produced by using the assignment operator is the value that was copied. Here's an example. What do you suppose will happen if you write this: Let's see. System.out.println(a = 3);? Create a new section in your IC exercise document and then open up BlueJ. In the Code Pad window: Create an int variable a initialized to 0 Type the line shown here. Don't forget the semicolon. On the next line, simply type the variable name, a, with no semicolon, and hit Enter Shoot me one screen-shot of your Code Pad window and one of the BlueJ Console with your output. When you used System.out.println() to display the expression, Java first performed the assignment, storing 3 in the variable a (that's the side-effect part). It then returned the 3 as the expression value, and that's what System.out.println() used as it's parameter. When you display the value of a on the last line, you can see that it has been modified. Chained Assignment The assignment operator is right-associative It works from right to left, not left to right Means that executable assignments can be chained a = b = c = 3; Works from right to left Copies 3 to c and produces the value 3 Value 3 copied to b and produces the value 3, etc. Initializing assignments cannot be chained Slide 6 Chained Assignment Duration: 00:01:14 Unlike the other binary operators, the assignment operator is right associative; it works from right to left, rather than left to right. Because of this, we can "chain" assignment statements together like this: a = b = c = 3; This expression first applies the assignment operation to the variable c, (the right-most part of the expression), copying the value 10 into the variable. The expression c = 3 produces a value, (3), which is used as the right-hand operand in the next assignment, where 3 is copied into b. In the same way, assigning 3 to b produces a value (3 again), and that value is used as the right-hand operand for the final assignment to a. There is one potential pitfall you may encounter when doing this: it does not work when defining a new variable, only on variables that already exist. That means you cannot write: int a = b = c = 3; // Not OK Instead, to get the desired effect, you must write: CS 170 Lecture: Side Effects Page 3 of Stephen Gilbert

4 int a = 3, b = 3, c = 3; // Initialization Shorthand Assignment To sum an amount in runningtotal you'd write: runningtotal = runningtotal + currentamount; You have to write the word runningtotal twice Java has a shorthand way to write the same thing: runningtotal += currentamount; Slide 7 Shorthand Assignment Duration: 00:00:46 Many times, when you perform an operation on a variable, you store the result of that operation right back in the same variable. If you are summing up the total in a checkbook, for instance, this would be a common expression: runningtotal = runningtotal + currentamount. What we really want to do is just add currentamount to runningtotal, and leave the result in the runningtotal variable. The shorthand-assignment operators give us a concise way to do that. Using these operators we could write the previous expression as: runningtotal += currentamount; There are shorthand assignment operators corresponding to most of the basic numeric operators. More Shorthand Assignment There are shorthand versions of all binary operators: a += b; // same as a = a + b; a -= = b; // same as a = a - b; a /= b; // same as a = a / b; a *= b; // same as a = a * b; a %= b; // same as a = a % b; Let's look at each of these. You've already seen the first one. a += b is the same as a = a + b; a -= b is the same as a = a b a /= b means a = a / b a *=b works like a = a * b and a %= b is the same as a = a % b Slide 8 More Shorthand Assignment Duration: 00:00:45 These all look pretty straightforward, but you might need to think about them a bit if the "b" part is something other than a simple variable. Let's try some examples. CS 170 Lecture: Side Effects Page 4 of Stephen Gilbert

5 Express Yourself Exercise 2: Write equivalent "shorthand" if possible: int x = 2, y = 3, z = 4, sum = 5, num = 6; x = 2 * x; x = x + y 2; sum = sum + num; z = z * x + 2 * z; y = y / (x + 5); Slide 9 Express Yourself Duration: 00:00:41 Increment and Decrement Adding or subtracting 1 from a variable is common Called increment (add) and decrement (subtract) Can do this with the addition, subtraction, assignment int a = 5; a = a + 1; // increment a = a - 1; // decrement Creates a temporary value Doesn't take advantage of special CPU instructions Slide 10 Increment and Decrement Duration: 00:01:00 For Exercise 2, I want you to explore the different short-hand assignment operators and how they work with different kinds of expressions. Open up Code Pad and type in the initialization line that creates the five different variables. Then, replace each of the following expressions with a shorthand assignment, if that's possible. To check to see if you've done it right, I've typed the original expressions into Code Pad so you can see what values you should get for each expression. Shoot me a screen-shot of your Code Pad session. A common operation in most computer programs is to add or subtract one from a variable and then store the result back into the same variable. This is called incrementing or, (when subtracting), decrementing the variable. This is easily done using the operators you've met already (addition, subtraction, and assignment) like this: int a = 5; a = a + 1; // Increment a = a - 1; // Decrement One problem with this solution is that the expression a + 1 produces a temporary value (like all expressions) that must be stored before it is finally copied back into the variable a by the assignment operator. Since most computer hardware contains special instructions to add one to, (or subtract one from), a variable "in place" (that is, without creating a temporary variable), Java provides operators that use these more efficient instructions. Increment and decrement operators Unary operators (++ ++, --) that add or subtract one Can only be applied to a variable You may use the operator before or after the variable Slide 11 int a = 5; a++; // a is now 6 ++a; // a is now 7 Up and Down Both have exactly the same side-effect on variable Variable is always incremented or decremented by 1 The increment (++) and decrement (--) operators are unary operators that can only be applied to a variable; you can't use them with literals, constants, or method calls. You can use the increment or decrement operators immediately before or immediately after a variable, like this: int a = 5; a++; ++a; In both cases, the value that was previously stored in a is incremented by 1. Since the increment operator modifies its operand, like the assignment operator, this behavior is called CS 170 Lecture: Side Effects Page 5 of Stephen Gilbert

6 Up and Down Duration: 00:00:47 a side effect. In addition to this side effect, the increment operator also produces a value, which has some interesting properties, as you'll see next. Pre and Post The "expression" or "returned" value is different With pre-increment (before the variable) The variable is updated The new value is returned as the expression's value With post-increment (after the variable) The original value is saved temporarily The variable is updated The original value is returned as the expression's value int first = 1, second = 2; int third = first++; // third==1, first==2 int fourth = ++second; // fourth, second == 3 Slide 12 Pre and Post Duration: 00:01:53 As I just mentioned, it doesn't make any difference whether you place the operator in front of the variable or after the variable as far as the side-effect value goes. The number is always incremented or decremented by 1. It does make a difference when it comes to the expression or returned value, though. When the operator is placed before the variable, (called a preincrement or decrement, depending upon the operator) then: the variable is updated to its new value the new value is returned as the expression value When the operator is placed after the variable, (called a postincrement or decrement) then: the original value stored in the variable is saved temporarily the variable is updated to its new value the original saved value is returned as the expression value As far as the side effect is concerned it doesn't make a bit of difference whether the operator is placed in front of, or after, a variable. In both cases, the variable is left holding a value one greater (or less) than it was before. The expression value produced by an increment or decrement, however, depends on whether it is a post or pre expression. This can be confusing, so here are some examples: Line 2 uses post-increment, so the value stored in first (1) is first saved, first is then updated to 2, and finally the saved value (1) is returned and stored in the variable third. Line 3 uses pre-increment so the value stored in second (2) is updated to 3 and that new value is returned to be stored in the variable fourth. CS 170 Lecture: Side Effects Page 6 of Stephen Gilbert

7 Express Yourself Exercise 3: What value does each variable contain after each statement is executed? (U if not defined) int a = 5, b = 6, c; a = b++ + 3; c = 2 * a + ++b; b = 2 * ++c a++; OK, it's your turn to "Be the Computer" again. Create a column for each of these variables and trace them in your exercise document. (Remember that tracing simply means mentally executing the line of code and then entering the state or value of each variable in the appropriate column.) You don't need to supply a screen-shot for this exercise. Slide 13 Express Yourself Duration: 00:00:27 What happens when Java evaluates this expression? x = 5.3 * 2; Mixed-type Expressions 5.3 is a double, but 2 is an int With these expressions, Java promotes the 2 Creates temporary value, 2.0, to use in the expression Called a promotion, because no data is lost The expression result will be a double, not an int Most precise type used in calculation Slide 14 Mixed-type Expressions Duration: 00:02:29 Not all expressions involve integers. You can also have expressions using floating-point numbers, characters, and Strings. You can even have expressions that involve all of the above. Remember, every expression produces a value, and each value produced by an expression has a particular type. When you add or subtract two integers, for instance, the result of that expression is an integer. When you add or subtract two floating-point numbers, the result is a floating point number. If you perform an operation with a character, the result is a character, and with a a String, the result is a String. Simple, but... What happens when you write this? a = 5.3 * 2; Whoa! Now we've got a problem. You know that the literal 5.3 is a double. You also know that the literal 2 is an int. What you don't know is the result of multiplying 5.3 * 2, or what type the variable a is. Let's look at both those questions. Not all numeric types are created equal. A short can hold more information (a greater range of values) than a byte, and an int can hold more information than a short. Likewise, a double can hold more information than a float--it is more precise. When Java encounters an expression that uses different types of operands, it first determines the most precise of the operands using this data hierarchy. Then, it creates temporary, unnamed variables of the most precise type, initializing those temporary variables using the less-precise values. This process is called promotion, or, a widening conversion. Let's look at our example again. Of our two values, 5.3 and 2, 5.3 has greater precision--it is higher in the data hierarchy. To perform the calculation, Java creates an unnamed temporary CS 170 Lecture: Side Effects Page 7 of Stephen Gilbert

8 double variable that contains 2.0, to take the place of the int value 2 during the calculation. The int value 2 is not changed in any way by this. Finally, the multiplication is performed using the double values 2.0 and 5.3 and the result is the double value Express Yourself Exercise 4: What value does each variable contain after each statement has been executed? int a = 3, b = 5, sum; double c = 14.1; sum = a + b + (int) c; c /= a; b += (int)c( a; a *= 2 * b + (int)c( int)c; Let's finish up with one last exercise. Create a table for each of the four variables a, b, c and d. Then, mentally execute each line of code and place the appropriate values in each column. When you're finished, use Code Pad to double check your original answers and post a screen-shot below your table. Slide 15 Express Yourself Duration: 00:00:27 CS 170 Lecture: Side Effects Page 8 of Stephen Gilbert

Slide 1 CS 170 Java Programming 1 Expressions Duration: 00:00:41 Advance mode: Auto

Slide 1 CS 170 Java Programming 1 Expressions Duration: 00:00:41 Advance mode: Auto CS 170 Java Programming 1 Expressions Slide 1 CS 170 Java Programming 1 Expressions Duration: 00:00:41 What is an expression? Expression Vocabulary Any combination of operators and operands which, when

More information

Slide 1 CS 170 Java Programming 1 Real Numbers Duration: 00:00:54 Advance mode: Auto

Slide 1 CS 170 Java Programming 1 Real Numbers Duration: 00:00:54 Advance mode: Auto CS 170 Java Programming 1 Real Numbers Understanding Java's Floating Point Primitive Types Slide 1 CS 170 Java Programming 1 Real Numbers Duration: 00:00:54 Floating-point types Can hold a fractional amount

More information

Slide 1 CS 170 Java Programming 1 More on Strings Duration: 00:00:47 Advance mode: Auto

Slide 1 CS 170 Java Programming 1 More on Strings Duration: 00:00:47 Advance mode: Auto CS 170 Java Programming 1 More on Strings Working with the String class Slide 1 CS 170 Java Programming 1 More on Strings Duration: 00:00:47 What are Strings in Java? Immutable sequences of 0 n characters

More information

Slide 1 CS 170 Java Programming 1 Multidimensional Arrays Duration: 00:00:39 Advance mode: Auto

Slide 1 CS 170 Java Programming 1 Multidimensional Arrays Duration: 00:00:39 Advance mode: Auto CS 170 Java Programming 1 Working with Rows and Columns Slide 1 CS 170 Java Programming 1 Duration: 00:00:39 Create a multidimensional array with multiple brackets int[ ] d1 = new int[5]; int[ ][ ] d2;

More information

Slide 1 CS 170 Java Programming 1 The Switch Duration: 00:00:46 Advance mode: Auto

Slide 1 CS 170 Java Programming 1 The Switch Duration: 00:00:46 Advance mode: Auto CS 170 Java Programming 1 The Switch Slide 1 CS 170 Java Programming 1 The Switch Duration: 00:00:46 Menu-Style Code With ladder-style if-else else-if, you might sometimes find yourself writing menu-style

More information

Slide 1 CS 170 Java Programming 1

Slide 1 CS 170 Java Programming 1 CS 170 Java Programming 1 Objects and Methods Performing Actions and Using Object Methods Slide 1 CS 170 Java Programming 1 Objects and Methods Duration: 00:01:14 Hi Folks. This is the CS 170, Java Programming

More information

Structured Programming

Structured Programming CS 170 Java Programming 1 Objects and Variables A Little More History, Variables and Assignment, Objects, Classes, and Methods Structured Programming Ideas about how programs should be organized Functionally

More information

Slide 1 CS 170 Java Programming 1 Testing Karel

Slide 1 CS 170 Java Programming 1 Testing Karel CS 170 Java Programming 1 Testing Karel Introducing Unit Tests to Karel's World Slide 1 CS 170 Java Programming 1 Testing Karel Hi Everybody. This is the CS 170, Java Programming 1 lecture, Testing Karel.

More information

What we will do today Explain and look at examples of. Programs that examine data. Data types. Topic 4. variables. expressions. assignment statements

What we will do today Explain and look at examples of. Programs that examine data. Data types. Topic 4. variables. expressions. assignment statements Topic 4 Variables Once a programmer has understood the use of variables, he has understood the essence of programming -Edsger Dijkstra What we will do today Explain and look at examples of primitive data

More information

Slide 1 CS 170 Java Programming 1 Arrays and Loops Duration: 00:01:27 Advance mode: Auto

Slide 1 CS 170 Java Programming 1 Arrays and Loops Duration: 00:01:27 Advance mode: Auto CS 170 Java Programming 1 Using Loops to Initialize and Modify Array Elements Slide 1 CS 170 Java Programming 1 Duration: 00:01:27 Welcome to the CS170, Java Programming 1 lecture on. Loop Guru, the album

More information

printf( Please enter another number: ); scanf( %d, &num2);

printf( Please enter another number: ); scanf( %d, &num2); CIT 593 Intro to Computer Systems Lecture #13 (11/1/12) Now that we've looked at how an assembly language program runs on a computer, we're ready to move up a level and start working with more powerful

More information

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1 Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1 Topics 1. Expressions 2. Operator precedence 3. Shorthand operators 4. Data/Type Conversion 1/15/19 CSE 1321 MODULE 2 2 Expressions

More information

Chapter 3: Operators, Expressions and Type Conversion

Chapter 3: Operators, Expressions and Type Conversion 101 Chapter 3 Operators, Expressions and Type Conversion Chapter 3: Operators, Expressions and Type Conversion Objectives To use basic arithmetic operators. To use increment and decrement operators. To

More information

Express Yourself. What is Eclipse?

Express Yourself. What is Eclipse? CS 170 Java Programming 1 Eclipse and the for Loop A Professional Integrated Development Environment Introducing Iteration Express Yourself Use OpenOffice or Word to create a new document Save the file

More information

COMP-202: Foundations of Programming. Lecture 5: More About Methods and Data Types Jackie Cheung, Winter 2016

COMP-202: Foundations of Programming. Lecture 5: More About Methods and Data Types Jackie Cheung, Winter 2016 COMP-202: Foundations of Programming Lecture 5: More About Methods and Data Types Jackie Cheung, Winter 2016 More Tutoring Help The Engineering Peer Tutoring Services (EPTS) is hosting free tutoring sessions

More information

Outline. Performing Computations. Outline (cont) Expressions in C. Some Expression Formats. Types for Operands

Outline. Performing Computations. Outline (cont) Expressions in C. Some Expression Formats. Types for Operands Performing Computations C provides operators that can be applied to calculate expressions: tax is 8.5% of the total sale expression: tax = 0.085 * totalsale Need to specify what operations are legal, how

More information

} Evaluate the following expressions: 1. int x = 5 / 2 + 2; 2. int x = / 2; 3. int x = 5 / ; 4. double x = 5 / 2.

} Evaluate the following expressions: 1. int x = 5 / 2 + 2; 2. int x = / 2; 3. int x = 5 / ; 4. double x = 5 / 2. Class #10: Understanding Primitives and Assignments Software Design I (CS 120): M. Allen, 19 Sep. 18 Java Arithmetic } Evaluate the following expressions: 1. int x = 5 / 2 + 2; 2. int x = 2 + 5 / 2; 3.

More information

Slide 1 CS 170 Java Programming 1 Loops, Jumps and Iterators Duration: 00:01:20 Advance mode: Auto

Slide 1 CS 170 Java Programming 1 Loops, Jumps and Iterators Duration: 00:01:20 Advance mode: Auto CS 170 Java Programming 1 Loops, Jumps and Iterators Java's do-while Loop, Jump Statements and Iterators Slide 1 CS 170 Java Programming 1 Loops, Jumps and Iterators Duration: 00:01:20 Hi Folks, welcome

More information

Skill 1: Multiplying Polynomials

Skill 1: Multiplying Polynomials CS103 Spring 2018 Mathematical Prerequisites Although CS103 is primarily a math class, this course does not require any higher math as a prerequisite. The most advanced level of mathematics you'll need

More information

CS 106 Introduction to Computer Science I

CS 106 Introduction to Computer Science I CS 106 Introduction to Computer Science I 01 / 31 / 2014 Instructor: Michael Eckmann Today s Topics Comments and/or Questions? Pseudocode Programming exercise to determine projected homeruns How Java determines

More information

Lecture Set 4: More About Methods and More About Operators

Lecture Set 4: More About Methods and More About Operators Lecture Set 4: More About Methods and More About Operators Methods Definitions Invocations More arithmetic operators Operator Side effects Operator Precedence Short-circuiting main method public static

More information

Chapter 12 Variables and Operators

Chapter 12 Variables and Operators Chapter 12 Variables and Operators Basic C Elements Variables Named, typed data items Operators Predefined actions performed on data items Combined with variables to form expressions, statements Statements

More information

MITOCW watch?v=0jljzrnhwoi

MITOCW watch?v=0jljzrnhwoi MITOCW watch?v=0jljzrnhwoi The following content is provided under a Creative Commons license. Your support will help MIT OpenCourseWare continue to offer high quality educational resources for free. To

More information

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence Data and Variables Data Types Expressions Operators Precedence String Concatenation Variables Declaration Assignment Shorthand operators Review class All code in a java file is written in a class public

More information

Unit 3. Operators. School of Science and Technology INTRODUCTION

Unit 3. Operators. School of Science and Technology INTRODUCTION INTRODUCTION Operators Unit 3 In the previous units (unit 1 and 2) you have learned about the basics of computer programming, different data types, constants, keywords and basic structure of a C program.

More information

Lecture Set 4: More About Methods and More About Operators

Lecture Set 4: More About Methods and More About Operators Lecture Set 4: More About Methods and More About Operators Methods Definitions Invocations More arithmetic operators Operator Side effects Operator Precedence Short-circuiting main method public static

More information

Data Conversion & Scanner Class

Data Conversion & Scanner Class Data Conversion & Scanner Class Quick review of last lecture August 29, 2007 ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor: Alexander Stoytchev Numeric Primitive Data Storing

More information

Slide 1 CS 170 Java Programming 1 The while Loop Duration: 00:00:60 Advance mode: Auto

Slide 1 CS 170 Java Programming 1 The while Loop Duration: 00:00:60 Advance mode: Auto CS 170 Java Programming 1 The while Loop Writing Counted Loops and Loops that Check a Condition Slide 1 CS 170 Java Programming 1 The while Loop Duration: 00:00:60 Hi Folks. Welcome to the CS 170, Java

More information

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I BASIC COMPUTATION x public static void main(string [] args) Fundamentals of Computer Science I Outline Using Eclipse Data Types Variables Primitive and Class Data Types Expressions Declaration Assignment

More information

Express Yourself. The Great Divide

Express Yourself. The Great Divide CS 170 Java Programming 1 Numbers Working with Integers and Real Numbers Open Microsoft Word and create a new document Save the file as LastFirst_ic07.doc Replace LastFirst with your actual name Put your

More information

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Web Site I have decided to keep this site for the whole semester I still hope to have blackboard up and running, but you

More information

More Programming Constructs -- Introduction

More Programming Constructs -- Introduction More Programming Constructs -- Introduction We can now examine some additional programming concepts and constructs Chapter 5 focuses on: internal data representation conversions between one data type and

More information

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement Last Time Let s all Repeat Together 10/3/05 CS150 Introduction to Computer Science 1 1 We covered o Counter and sentinel controlled loops o Formatting output Today we will o Type casting o Top-down, stepwise

More information

LECTURE 3 C++ Basics Part 2

LECTURE 3 C++ Basics Part 2 LECTURE 3 C++ Basics Part 2 OVERVIEW Operators Type Conversions OPERATORS Operators are special built-in symbols that have functionality, and work on operands. Operators are actually functions that use

More information

C Programming

C Programming 204216 -- C Programming Chapter 3 Processing and Interactive Input Adapted/Assembled for 204216 by Areerat Trongratsameethong A First Book of ANSI C, Fourth Edition Objectives Assignment Mathematical Library

More information

MITOCW ocw f99-lec07_300k

MITOCW ocw f99-lec07_300k MITOCW ocw-18.06-f99-lec07_300k OK, here's linear algebra lecture seven. I've been talking about vector spaces and specially the null space of a matrix and the column space of a matrix. What's in those

More information

Chapter 12 Variables and Operators

Chapter 12 Variables and Operators Basic C Elements Chapter 12 Variables and Operators Original slides from Gregory Byrd, North Carolina State University! Variables named, typed data items! Operators predefined actions performed on data

More information

Information Science 1

Information Science 1 Information Science 1 Simple Calcula,ons Week 09 College of Information Science and Engineering Ritsumeikan University Topics covered l Terms and concepts from Week 8 l Simple calculations Documenting

More information

Post Experiment Interview Questions

Post Experiment Interview Questions Post Experiment Interview Questions Questions about the Maximum Problem 1. What is this problem statement asking? 2. What is meant by positive integers? 3. What does it mean by the user entering valid

More information

A complex expression to evaluate we need to reduce it to a series of simple expressions. E.g * 7 =>2+ 35 => 37. E.g.

A complex expression to evaluate we need to reduce it to a series of simple expressions. E.g * 7 =>2+ 35 => 37. E.g. 1.3a Expressions Expressions An Expression is a sequence of operands and operators that reduces to a single value. An operator is a syntactical token that requires an action be taken An operand is an object

More information

Information Science 1

Information Science 1 Topics covered Information Science 1 Terms and concepts from Week 8 Simple calculations Documenting programs Simple Calcula,ons Expressions Arithmetic operators and arithmetic operator precedence Mixed-type

More information

Topic 4 Expressions and variables

Topic 4 Expressions and variables Topic 4 Expressions and variables "Once a person has understood the way variables are used in programming, he has understood the quintessence of programming." -Professor Edsger W. Dijkstra Based on slides

More information

Programming Lecture 3

Programming Lecture 3 Programming Lecture 3 Expressions (Chapter 3) Primitive types Aside: Context Free Grammars Constants, variables Identifiers Variable declarations Arithmetic expressions Operator precedence Assignment statements

More information

CS313D: ADVANCED PROGRAMMING LANGUAGE

CS313D: ADVANCED PROGRAMMING LANGUAGE CS313D: ADVANCED PROGRAMMING LANGUAGE Computer Science department Lecture 2 : C# Language Basics Lecture Contents 2 The C# language First program Variables and constants Input/output Expressions and casting

More information

Chapter 3 Structure of a C Program

Chapter 3 Structure of a C Program Chapter 3 Structure of a C Program Objectives To be able to list and describe the six expression categories To understand the rules of precedence and associativity in evaluating expressions To understand

More information

Exercises Software Development I. 05 Conversions and Promotions; Lifetime, Scope, Shadowing. November 5th, 2014

Exercises Software Development I. 05 Conversions and Promotions; Lifetime, Scope, Shadowing. November 5th, 2014 Exercises Software Development I 05 Conversions and Promotions; Lifetime, Scope, Shadowing November 5th, 2014 Software Development I Winter term 2014/2015 Priv.-Doz. Dipl.-Ing. Dr. Andreas Riener Institute

More information

Programming, Data Structures and Algorithms Prof. Hema A Murthy Department of Computer Science and Engineering Indian Institute of Technology, Madras

Programming, Data Structures and Algorithms Prof. Hema A Murthy Department of Computer Science and Engineering Indian Institute of Technology, Madras Programming, Data Structures and Algorithms Prof. Hema A Murthy Department of Computer Science and Engineering Indian Institute of Technology, Madras Lecture - 54 Assignment on Data Structures (Refer Slide

More information

Express Yourself. Writing Your Own Classes

Express Yourself. Writing Your Own Classes Java Programming 1 Lecture 5 Defining Classes Creating your Own Classes Express Yourself Use OpenOffice Writer to create a new document Save the file as LastFirst_ic05 Replace LastFirst with your actual

More information

Programming for Engineers: Operators, Expressions, and Statem

Programming for Engineers: Operators, Expressions, and Statem Programming for Engineers: Operators,, and 28 January 2011 31 January 2011 Programming for Engineers: Operators,, and Statem The While Loop A test expression is at top of loop The program repeats loop

More information

COMP-202 Unit 2: Java Basics. CONTENTS: Using Expressions and Variables Types Strings Methods

COMP-202 Unit 2: Java Basics. CONTENTS: Using Expressions and Variables Types Strings Methods COMP-202 Unit 2: Java Basics CONTENTS: Using Expressions and Variables Types Strings Methods Assignment 1 Assignment 1 posted on WebCt and course website. It is due May 18th st at 23:30 Worth 6% Part programming,

More information

Operators. Lecture 3 COP 3014 Spring January 16, 2018

Operators. Lecture 3 COP 3014 Spring January 16, 2018 Operators Lecture 3 COP 3014 Spring 2018 January 16, 2018 Operators Special built-in symbols that have functionality, and work on operands operand an input to an operator Arity - how many operands an operator

More information

Operators. Java operators are classified into three categories:

Operators. Java operators are classified into three categories: Operators Operators are symbols that perform arithmetic and logical operations on operands and provide a meaningful result. Operands are data values (variables or constants) which are involved in operations.

More information

MITOCW watch?v=flgjisf3l78

MITOCW watch?v=flgjisf3l78 MITOCW watch?v=flgjisf3l78 The following content is provided under a Creative Commons license. Your support will help MIT OpenCourseWare continue to offer high-quality educational resources for free. To

More information

5/3/2006. Today! HelloWorld in BlueJ. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont.

5/3/2006. Today! HelloWorld in BlueJ. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. Today! Build HelloWorld yourself in BlueJ and Eclipse. Look at all the Java keywords. Primitive Types. HelloWorld in BlueJ 1. Find BlueJ in the start menu, but start the Select VM program instead (you

More information

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI CSCI 2010 Principles of Computer Science Data and Expressions 08/09/2013 CSCI 2010 1 Data Types, Variables and Expressions in Java We look at the primitive data types, strings and expressions that are

More information

JAVA OPERATORS GENERAL

JAVA OPERATORS GENERAL JAVA OPERATORS GENERAL Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups: Arithmetic Operators Relational Operators Bitwise Operators

More information

Computational Expression

Computational Expression Computational Expression Scanner, Increment/Decrement, Conversion Janyl Jumadinova 17 September, 2018 Janyl Jumadinova Computational Expression 17 September, 2018 1 / 11 Review: Scanner The Scanner class

More information

static CS106L Spring 2009 Handout #21 May 12, 2009 Introduction

static CS106L Spring 2009 Handout #21 May 12, 2009 Introduction CS106L Spring 2009 Handout #21 May 12, 2009 static Introduction Most of the time, you'll design classes so that any two instances of that class are independent. That is, if you have two objects one and

More information

CS102: Standard I/O. %<flag(s)><width><precision><size>conversion-code

CS102: Standard I/O. %<flag(s)><width><precision><size>conversion-code CS102: Standard I/O Our next topic is standard input and standard output in C. The adjective "standard" when applied to "input" or "output" could be interpreted to mean "default". Typically, standard output

More information

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal Lesson Goals Understand the basic constructs of a Java Program Understand how to use basic identifiers Understand simple Java data types

More information

Visual C# Instructor s Manual Table of Contents

Visual C# Instructor s Manual Table of Contents Visual C# 2005 2-1 Chapter 2 Using Data At a Glance Instructor s Manual Table of Contents Overview Objectives s Quick Quizzes Class Discussion Topics Additional Projects Additional Resources Key Terms

More information

CS 106 Introduction to Computer Science I

CS 106 Introduction to Computer Science I CS 106 Introduction to Computer Science I 06 / 03 / 2015 Instructor: Michael Eckmann Today s Topics Finish up discussion of projected homeruns 162 as a constant (final) double vs. int in calculation Scanner

More information

Fundamentals of Programming Session 7

Fundamentals of Programming Session 7 Fundamentals of Programming Session 7 Instructor: Reza Entezari-Maleki Email: entezari@ce.sharif.edu 1 Fall 2014 These slides have been created using Deitel s slides Sharif University of Technology Outlines

More information

Chapter 12 Variables and Operators

Chapter 12 Variables and Operators Chapter 12 Variables and Operators Highlights (1) r. height width operator area = 3.14 * r *r + width * height literal/constant variable expression (assignment) statement 12-2 Highlights (2) r. height

More information

Le L c e t c ur u e e 2 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Variables Operators

Le L c e t c ur u e e 2 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Variables Operators Course Name: Advanced Java Lecture 2 Topics to be covered Variables Operators Variables -Introduction A variables can be considered as a name given to the location in memory where values are stored. One

More information

1.00 Lecture 4. Promotion

1.00 Lecture 4. Promotion 1.00 Lecture 4 Data Types, Operators Reading for next time: Big Java: sections 6.1-6.4 Promotion increasing capacity Data Type Allowed Promotions double None float double long float,double int long,float,double

More information

Introduction. Using Styles. Word 2010 Styles and Themes. To Select a Style: Page 1

Introduction. Using Styles. Word 2010 Styles and Themes. To Select a Style: Page 1 Word 2010 Styles and Themes Introduction Page 1 Styles and themes are powerful tools in Word that can help you easily create professional looking documents. A style is a predefined combination of font

More information

QUIZ: What value is stored in a after this

QUIZ: What value is stored in a after this QUIZ: What value is stored in a after this statement is executed? Why? a = 23/7; QUIZ evaluates to 16. Lesson 4 Statements, Expressions, Operators Statement = complete instruction that directs the computer

More information

Chapter 2: Data and Expressions

Chapter 2: Data and Expressions Chapter 2: Data and Expressions CS 121 Department of Computer Science College of Engineering Boise State University January 15, 2015 Chapter 2: Data and Expressions CS 121 1 / 1 Chapter 2 Part 1: Data

More information

CS 106 Introduction to Computer Science I

CS 106 Introduction to Computer Science I CS 106 Introduction to Computer Science I 06 / 04 / 2015 Instructor: Michael Eckmann Today s Topics Questions / comments? Calling methods (noting parameter(s) and their types, as well as the return type)

More information

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #29 Arrays in C

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #29 Arrays in C Introduction to Programming in C Department of Computer Science and Engineering Lecture No. #29 Arrays in C (Refer Slide Time: 00:08) This session will learn about arrays in C. Now, what is the word array

More information

COMP-202 Unit 2: Java Basics. CONTENTS: Using Expressions and Variables Types Strings

COMP-202 Unit 2: Java Basics. CONTENTS: Using Expressions and Variables Types Strings COMP-202 Unit 2: Java Basics CONTENTS: Using Expressions and Variables Types Strings Assignment 1 Assignment 1 posted on WebCt. It will be due January 21 st at 13:00 Worth 4% Last Class Input and Output

More information

Basics of Java Programming

Basics of Java Programming Basics of Java Programming Lecture 2 COP 3252 Summer 2017 May 16, 2017 Components of a Java Program statements - A statement is some action or sequence of actions, given as a command in code. A statement

More information

Week 3: Objects, Input and Processing

Week 3: Objects, Input and Processing CS 170 Java Programming 1 Week 3: Objects, Input and Processing Learning to Create Objects Learning to Accept Input Learning to Process Data What s the Plan? Topic I: Working with Java Objects Learning

More information

CS 112 Introduction to Programming

CS 112 Introduction to Programming CS 112 Introduction to Programming Variables; Type Casting; Using Variables in for Loops Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400 Email: yry@cs.yale.edu

More information

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop Announcements Lab Friday, 1-2:30 and 3-4:30 in 26-152 Boot your laptop and start Forte, if you brought your laptop Create an empty file called Lecture4 and create an empty main() method in a class: 1.00

More information

Chapter 2: Basic Elements of Java

Chapter 2: Basic Elements of Java Chapter 2: Basic Elements of Java TRUE/FALSE 1. The pair of characters // is used for single line comments. ANS: T PTS: 1 REF: 29 2. The == characters are a special symbol in Java. ANS: T PTS: 1 REF: 30

More information

Lecture 9. Assignment. Logical Operations. Logical Operations - Motivation 2/8/18

Lecture 9. Assignment. Logical Operations. Logical Operations - Motivation 2/8/18 Assignment Lecture 9 Logical Operations Formatted Print Printf Increment and decrement Read through 3.9, 3.10 Read 4.1. 4.2, 4.3 Go through checkpoint exercise 4.1 Logical Operations - Motivation Logical

More information

CS106X Handout 03 Autumn 2012 September 24 th, 2012 Getting Started

CS106X Handout 03 Autumn 2012 September 24 th, 2012 Getting Started CS106X Handout 03 Autumn 2012 September 24 th, 2012 Getting Started Handout written by Julie Zelenski, Mehran Sahami, Robert Plummer, and Jerry Cain. After today s lecture, you should run home and read

More information

In Java, data type boolean is used to represent Boolean data. Each boolean constant or variable can contain one of two values: true or false.

In Java, data type boolean is used to represent Boolean data. Each boolean constant or variable can contain one of two values: true or false. CS101, Mock Boolean Conditions, If-Then Boolean Expressions and Conditions The physical order of a program is the order in which the statements are listed. The logical order of a program is the order in

More information

A function is a named piece of code that performs a specific task. Sometimes functions are called methods, procedures, or subroutines (like in LC-3).

A function is a named piece of code that performs a specific task. Sometimes functions are called methods, procedures, or subroutines (like in LC-3). CIT Intro to Computer Systems Lecture # (//) Functions As you probably know from your other programming courses, a key part of any modern programming language is the ability to create separate functions

More information

REPETITION CONTROL STRUCTURE LOGO

REPETITION CONTROL STRUCTURE LOGO CSC 128: FUNDAMENTALS OF COMPUTER PROBLEM SOLVING REPETITION CONTROL STRUCTURE 1 Contents 1 Introduction 2 for loop 3 while loop 4 do while loop 2 Introduction It is used when a statement or a block of

More information

Operators. Java Primer Operators-1 Scott MacKenzie = 2. (b) (a)

Operators. Java Primer Operators-1 Scott MacKenzie = 2. (b) (a) Operators Representing and storing primitive data types is, of course, essential for any computer language. But, so, too, is the ability to perform operations on data. Java supports a comprehensive set

More information

CS110: PROGRAMMING LANGUAGE I

CS110: PROGRAMMING LANGUAGE I CS110: PROGRAMMING LANGUAGE I Computer Science Department Lecture 4: Java Basics (II) A java Program 1-2 Class in file.java class keyword braces {, } delimit a class body main Method // indicates a comment.

More information

Simple Java Programming Constructs 4

Simple Java Programming Constructs 4 Simple Java Programming Constructs 4 Course Map In this module you will learn the basic Java programming constructs, the if and while statements. Introduction Computer Principles and Components Software

More information

Computer Organization & Assembly Language Programming

Computer Organization & Assembly Language Programming Computer Organization & Assembly Language Programming CSE 2312 Lecture 11 Introduction of Assembly Language 1 Assembly Language Translation The Assembly Language layer is implemented by translation rather

More information

Will introduce various operators supported by C language Identify supported operations Present some of terms characterizing operators

Will introduce various operators supported by C language Identify supported operations Present some of terms characterizing operators Operators Overview Will introduce various operators supported by C language Identify supported operations Present some of terms characterizing operators Operands and Operators Mathematical or logical relationships

More information

Slide 1 CS 170 Java Programming 1 Object-Oriented Graphics Duration: 00:00:18 Advance mode: Auto

Slide 1 CS 170 Java Programming 1 Object-Oriented Graphics Duration: 00:00:18 Advance mode: Auto CS 170 Java Programming 1 Object-Oriented Graphics The Object-Oriented ACM Graphics Classes Slide 1 CS 170 Java Programming 1 Object-Oriented Graphics Duration: 00:00:18 Hello, Welcome to the CS 170, Java

More information

Section we will not cover section 2.11 feel free to read it on your own

Section we will not cover section 2.11 feel free to read it on your own Operators Class 5 Section 2.11 we will not cover section 2.11 feel free to read it on your own Data Types Data Type A data type is a set of values and a set of operations defined on those values. in class

More information

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal Lesson Goals Understand the basic constructs of a Java Program Understand how to use basic identifiers Understand simple Java data types and

More information

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics Java Programming, Sixth Edition 2-1 Chapter 2 Using Data At a Glance Instructor s Manual Table of Contents Overview Objectives Teaching Tips Quick Quizzes Class Discussion Topics Additional Projects Additional

More information

Accelerating Information Technology Innovation

Accelerating Information Technology Innovation Accelerating Information Technology Innovation http://aiti.mit.edu Cali, Colombia Summer 2012 Lesson 02 Variables and Operators Agenda Variables Types Naming Assignment Data Types Type casting Operators

More information

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators JAVA Standard Edition Java - Basic Operators Java provides a rich set of operators to manipulate variables.

More information

Full file at

Full file at Java Programming: From Problem Analysis to Program Design, 3 rd Edition 2-1 Chapter 2 Basic Elements of Java At a Glance Instructor s Manual Table of Contents Overview Objectives s Quick Quizzes Class

More information

Arithmetic and IO. 25 August 2017

Arithmetic and IO. 25 August 2017 Arithmetic and IO 25 August 2017 Submissions you can submit multiple times to the homework dropbox file name: uppercase first letter, Yourlastname0829.java the system will use the last submission before

More information

9/10/10. Arithmetic Operators. Today. Assigning floats to ints. Arithmetic Operators & Expressions. What do you think is the output?

9/10/10. Arithmetic Operators. Today. Assigning floats to ints. Arithmetic Operators & Expressions. What do you think is the output? Arithmetic Operators Section 2.15 & 3.2 p 60-63, 81-89 1 Today Arithmetic Operators & Expressions o Computation o Precedence o Associativity o Algebra vs C++ o Exponents 2 Assigning floats to ints int

More information

CMPT 125: Lecture 3 Data and Expressions

CMPT 125: Lecture 3 Data and Expressions CMPT 125: Lecture 3 Data and Expressions Tamara Smyth, tamaras@cs.sfu.ca School of Computing Science, Simon Fraser University January 3, 2009 1 Character Strings A character string is an object in Java,

More information

In our first lecture on sets and set theory, we introduced a bunch of new symbols and terminology.

In our first lecture on sets and set theory, we introduced a bunch of new symbols and terminology. Guide to and Hi everybody! In our first lecture on sets and set theory, we introduced a bunch of new symbols and terminology. This guide focuses on two of those symbols: and. These symbols represent concepts

More information

Binary, Hexadecimal and Octal number system

Binary, Hexadecimal and Octal number system Binary, Hexadecimal and Octal number system Binary, hexadecimal, and octal refer to different number systems. The one that we typically use is called decimal. These number systems refer to the number of

More information

Midterms Save the Dates!

Midterms Save the Dates! University of British Columbia CPSC 111, Intro to Computation Alan J. Hu Primitive Data Types Arithmetic Operators Readings Your textbook is Big Java (3rd Ed). This Week s Reading: Ch 2.1-2.5, Ch 4.1-4.2.

More information