Lecture 3 Operators MIT AITI

Similar documents
Lecture 2: Variables and Operators. AITI Nigeria Summer 2012 University of Lagos.

Accelerating Information Technology Innovation

Lecture Set 4: More About Methods and More About Operators

Prefix/Infix/Postfix Notation

Unit 3. Operators. School of Science and Technology INTRODUCTION

Fundamentals of Programming CS-110. Lecture 3

Operators. Java operators are classified into three categories:

Chapter 3: Operators, Expressions and Type Conversion

GO - OPERATORS. This tutorial will explain the arithmetic, relational, logical, bitwise, assignment and other operators one by one.

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

JAVA OPERATORS GENERAL

Operators & Expressions

SOFTWARE DEVELOPMENT 1. Operators 2018W A. Ferscha (Institute of Pervasive Computing, JKU Linz)

Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands

Lecture Set 4: More About Methods and More About Operators

SECTION II: LANGUAGE BASICS

Object-Oriented Programming

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

Operators in C. Staff Incharge: S.Sasirekha

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and

The Arithmetic Operators

Prof. Navrati Saxena TA: Rochak Sachan

ECE 122 Engineering Problem Solving with Java

Lesson 1: Arithmetic Review

Visual C# Instructor s Manual Table of Contents

Building Java Programs

Operators in java Operator operands.

Mini-Lesson 1. Section 1.1: Order of Operations PEMDAS

Operators and Expressions:

Add Subtract Multiply Divide

Operators. Lecture 3 COP 3014 Spring January 16, 2018

CS 251L Review Quiz Key

COMP-202: Foundations of Programming. Lecture 3: Boolean, Mathematical Expressions, and Flow Control Sandeep Manjanna, Summer 2015

Lesson 1: Arithmetic Review

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

Declaration and Memory

Expressions and Precedence. Last updated 12/10/18

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

Chapter 4: Basic C Operators

LECTURE 3 C++ Basics Part 2

WEEK 4 OPERATORS, EXPRESSIONS AND STATEMENTS

Building Java Programs

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

Building Java Programs

Department of Computer Science

COMP6700/2140 Operators, Expressions, Statements

Chapter 3 Structure of a C Program

Information Science 1

Sir Muhammad Naveed. Arslan Ahmed Shaad ( ) Muhammad Bilal ( )

Information Science 1

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

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

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

Chapter 2: Using Data

Selenium Class 9 - Java Operators

Chapter 2: Using Data

C/C++ Programming for Engineers: Working with Integer Variables

Loops and Files. Chapter 04 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz

Zheng-Liang Lu Java Programming 45 / 79

Constants and Variables

ESCI 386 IDL Programming for Advanced Earth Science Applications Lesson 1 IDL Operators

Expressions and operators

CS313D: ADVANCED PROGRAMMING LANGUAGE

bitwise inclusive OR Logical logical AND && logical OR Ternary ternary? : Assignment assignment = += -= *= /= %= &= ^= = <<= >>= >>>=

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

Java enum, casts, and others (Select portions of Chapters 4 & 5)

Basics of Java Programming

Operations. Making Things Happen

Programming in C++ 5. Integral data types

Basics of Java Programming variables, assignment, and input

Data Types and Variables in C language

3. Java - Language Constructs I

Lexical Considerations

DATA TYPES AND EXPRESSIONS

Data Types. 1 You cannot change the type of the variable after declaration. Zheng-Liang Lu Java Programming 52 / 87

Perl: Arithmetic, Operator Precedence and other operators. Perl has five basic kinds of arithmetic:

PRIMITIVE VARIABLES. CS302 Introduction to Programming University of Wisconsin Madison Lecture 3. By Matthew Bernstein

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. A Guide to this Instructor s Manual:

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

Principles of Computer Science

Chapter 2 Working with Data Types and Operators

1. Variables 2. Arithmetic 3. Input and output 4. Problem solving: first do it by hand 5. Strings 6. Chapter summary

Informatics Ingeniería en Electrónica y Automática Industrial

Working with Algebraic Expressions

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

Full file at

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

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

Unit-2 (Operators) ANAND KR.SRIVASTAVA

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

Programming with Java

The Order of Operations. Unit 1 Lesson 3

2.2 Order of Operations

1. The keyword main in C language is used for

Java Primer 1: Types, Classes and Operators

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

Lecture 3. More About C

More Programming Constructs -- Introduction

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

Computers Programming Course 6. Iulian Năstac

Transcription:

Lecture 3 Operators MIT AITI - 2004

What are Operators? Operators are special symbols used for mathematical functions assignment statements logical comparisons Examples: 3 + 5 // uses + operator 14 + 5 4 * (5 3) // uses +, -, * operators Expressions can be combinations of variables, primitives and operators that result in a value

The Operator Groups There are 5 different groups of operators: Arithmetic operators Assignment operator Increment/Decrement operators Relational operators Conditional operators

Arithmetic Operators Java has 6 basic arithmetic operators + add - subtract * multiply / divide % modulo (remainder) ^ exponent (to the power of) Order of operations (or precedence) when evaluating an expression is the same as you learned in school (PEMDAS).

Order of Operations Example: 10 + 15 / 5; The result is different depending on whether the addition or division is performed first (10 + 15) / 5 = 5 10 + (15 / 5) = 13 Without parentheses, Java will choose the second case Note: you should be explicit and use parentheses to avoid confusion Should give more examples during lecture. Be dynamic teachers.

Integer Division In the previous example, we were lucky that (10 + 15) / 5 gives an exact integer answer (5). But what if we divide 63 by 35? Depending on the data types of the variables that store the numbers, we will get different results.

Integer Division Example int i = 63; int j = 35; System.out.println(i / j); Output: 1 double x = 63; double y = 35; System.out.println(x / y); Ouput: 1.8 The result of integer division is just the integer part of the quotient!

Assignment Operator The basic assignment operator (=) assigns the value of expr to var var = expr ; Java allows you to combine arithmetic and assignment operators into a single operator. Examples: x = x + 5; is equivalent to x += 5; y = y * 7; is equivalent to y *= 7;

Increment/Decrement Operators count = count + 1; can be written as: ++count; or count++; ++ is called the increment operator. count = count - 1; can be written as: --count; or count--; -- is called the decrement operator.

The increment/decrement operator has two forms: The prefix form ++count, --count first adds 1 to the variable and then continues to any other operator in the expression int numoranges = 5; int numapples = 10; int numfruit; numfruit = ++numoranges + numapples; numfruit has value 16 numoranges has value 6 The postfix form count++, count-- first evaluates the expression and then adds 1 to the variable int numoranges = 5; int numapples = 10; int numfruit; numfruit = numoranges++ + numapples; numfruit has value 15 numoranges has value 6

Relational (Comparison) Operators Relational operators compare two values Produces a boolean value (true or false) depending on the relationship operation is true when... a > b a >= b a == b a!= b a <= b a < b a is greater than b a is greater than or equal to b a is equal to b a is not equal to b a is less than or equal to b a is less than b Note: == sign!

Examples of Relational Operations int x = 3; int y = 5; boolean result; 1) result = (x > y); now result is assigned the value false because 3 is not greater than 5 2) result = (15 == x*y); now result is assigned the value true because the product of 3 and 5 equals 15 3) result = (x!= x*y); now result is assigned the value true because the product of x and y (15) is not equal to x (3)

Conditional Operators Symbol && Name AND OR! NOT Conditional operators can be referred to as boolean operators, because they are only used to combine expressions that have a value of true or false.

Truth Table for Conditional Operators x y x && y x y!x True True True True False True False False True False False True False True True False False False False True

Examples of Conditional Operators boolean x = true; boolean y = false; boolean result; 1. Let result = (x && y); now result is assigned the value false (see truth table!) 2. Let result = ((x y) && x); (x y) evaluates to true (true && x) evaluates to true now result is assigned the value true

Using && and Examples: (a && (b++ > 3)) (x y) Java will evaluate these expressions from left to right and so will evaluate a before (b++ > 3) x before y Java performs short-circuit evaluation: it evaluates && and expressions from left to right and once it finds the result, it stops.

Short-Circuit Evaluations (a && (b++ > 3)) What happens if a is false? Java will not evaluate the right-hand expression (b++ > 3) if the left-hand operator a is false, since the result is already determined in this case to be false. This means b will not be incremented! (x y) What happens if x is true? Similarly, Java will not evaluate the right-hand operator y if the left-hand operator x is true, since the result is already determined in this case to be true.

POP QUIZ 1) What is the value of number? -12 int number = 5 * 3 3 / 6 9 * 3; 2) What is the value of result? false int x = 8; int y = 2; boolean result = (15 == x * y); 3) What is the value of result? true boolean x = 7; boolean result = (x < 8) && (x > 4); 4) What is the value of numcars? 27 int numbluecars = 5; int numgreencars = 10; int numcars = numgreencars++ + numbluecars + ++numgreeencars;

References Summary of Java operators http://java.sun.com/docs/books/tutorial/java/nutsandbolts/opsummary.html Order of Operations (PEMDAS) 1. Parentheses 2. Exponents 3. Multiplication and Division from left to right 4. Addition and Subtraction from left to right