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

Similar documents
1.00 Lecture 4. Promotion

1.00 Lecture 5. Floating Point Anomalies

CMSC 104 -Lecture 6 John Y. Park, adapted by C Grasso

More Programming Constructs -- Introduction

cs1114 REVIEW of details test closed laptop period

Chapter 4: Basic C Operators

COMP Primitive and Class Types. Yi Hong May 14, 2015

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

Programming in C++ 5. Integral data types

CS313D: ADVANCED PROGRAMMING LANGUAGE

Operators and Expressions

Operators. Java operators are classified into three categories:

A Look Back at Arithmetic Operators: the Increment and Decrement

C/C++ Programming Lecture 7 Name:

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

CMPT 125: Lecture 3 Data and Expressions

Operators & Expressions

Operators. Lecture 3 COP 3014 Spring January 16, 2018

Chapter 2: Using Data

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

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

Overview (4) CPE 101 mod/reusing slides from a UW course. Assignment Statement: Review. Why Study Expressions? D-1

Lecture 3 Tao Wang 1

Programming for Engineers Iteration

Laboratory 5: Implementing Loops and Loop Control Strategies

Add Subtract Multiply Divide

Chapter 7. Expressions and Assignment Statements ISBN

Unit 3. Operators. School of Science and Technology INTRODUCTION

Operators and Expressions:

Operators in C. Staff Incharge: S.Sasirekha

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

CSE 1001 Fundamentals of Software Development 1. Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018

Java How to Program, 9/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Two Types of Types. Primitive Types in Java. Using Primitive Variables. Class #07: Java Primitives. Integer types.

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

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

Chapter 4 Introduction to Control Statements

The Arithmetic Operators

Simple Java Programming Constructs 4

Variables and Operators 2/20/01 Lecture #

CS110D: PROGRAMMING LANGUAGE I

Information Science 1

Introduction to Programming Using Java (98-388)

Lesson #3. Variables, Operators, and Expressions. 3. Variables, Operators and Expressions - Copyright Denis Hamelin - Ryerson University

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

Expressions & Assignment Statements

COP 3275: Chapter 04. Jonathan C.L. Liu, Ph.D. CISE Department University of Florida, USA

Dept. of CSE, IIT KGP

ECE 122 Engineering Problem Solving with Java

Declaration and Memory

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

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

CSCI 131, Midterm Exam 1 Review Questions This sheet is intended to help you prepare for the first exam in this course. The following topics have

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

3. EXPRESSIONS. It is a sequence of operands and operators that reduce to a single value.

2/5/2018. Expressions are Used to Perform Calculations. ECE 220: Computer Systems & Programming. Our Class Focuses on Four Types of Operator in C

Full file at

Software II: Principles of Programming Languages. Why Expressions?

CIS133J. Working with Numbers in Java

Accelerating Information Technology Innovation

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

Objects and Types. COMS W1007 Introduction to Computer Science. Christopher Conway 29 May 2003

A Java program contains at least one class definition.

CSE 452: Programming Languages. Outline of Today s Lecture. Expressions. Expressions and Control Flow

Course PJL. Arithmetic Operations

JAVA REVIEW cs2420 Introduction to Algorithms and Data Structures Spring 2015

Chapter 7. Expressions and Assignment Statements (updated edition 11) ISBN

CONTENTS: Compilation Data and Expressions COMP 202. More on Chapter 2

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

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

Pace University. Fundamental Concepts of CS121 1

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

Java Primer 1: Types, Classes and Operators

CIS 110: Introduction to Computer Programming

CS16 Week 2 Part 2. Kyle Dewey. Thursday, July 5, 12

Chapter 7. Expressions and Assignment Statements

CS61B Lecture #14: Integers. Last modified: Wed Sep 27 15:44: CS61B: Lecture #14 1

Chapter 4: Expressions. Chapter 4. Expressions. Copyright 2008 W. W. Norton & Company. All rights reserved.

Fundamentals of Programming Session 7

DEPARTMENT OF MATHS, MJ COLLEGE

3. Java - Language Constructs I

Full file at

Visual C# Instructor s Manual Table of Contents

Computers Programming Course 6. Iulian Năstac

Computer Programming CS F111

CS61B Lecture #14: Integers

Integers II. CSE 351 Autumn Instructor: Justin Hsia

Unit 1 Lesson 4. Introduction to Control Statements

Computer Science & Engineering 150A Problem Solving Using Computers. Chapter 5. Repetition in Programs. Notes. Notes. Notes. Lecture 05 - Loops

Chapter 7. Expressions and Assignment Statements ISBN

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

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

Repetition CSC 121 Fall 2014 Howard Rosenthal

Lecture 3. More About C

CS1 Recitation. Week 1

CT 229 Java Syntax Continued

COP 2000 Introduction to Computer Programming Mid-Term Exam Review

Information Science 1

LECTURE 3 C++ Basics Part 2

Transcription:

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 Lecture 4 More on Java Data Types, Control Structures 1

Java Arithmetic Operators Table in precedence order, highest precedence at top Operators ++ -- + (unary) - (unary) * / % + - Meaning increment decrement unary + ( x = +a) unary ( x = -a) multiplication division modulo addition subtraction Associativity Right to left Left to right Left to right Precedence, Associativity Operator precedence is in the order of the previous table Operators in same row have equal precedence Associativity determines order in which operators of equal precedence are applied Parentheses override order of precedence 2

Precedence, Associativity, p.2 Operator precedence is in the order of the previous table Operators in same row have equal precedence Associativity determines order in which operators of equal precedence are applied Parentheses override order of precedence Operator Exercises What is the value of int n: 3

Operator Exercises What is the value of int n: Mixed Arithmetic Promotion: When two operands have different types, Java converts the low capacity to the high capacity type Casting: When you wish to convert a high capacity type to a low capacity type, you must indicate that explicitly Binary operators ( +, -, *, /): If either operand is (double, float, long), other will be converted to (double, float, long) Otherwise both are converted to int 4

Mixed Arithmetic Example Integer Arithmetic Properties Overflows occur from: Division by zero, including 0/0 (undefined) Programmer has responsibility to check and prevent this Java will warn you (by throwing an exception) if it can t do an integer arithmetic operation (discussed later) Accumulating results that exceed the capacity of the integer type being used Programmer has responsibility to check and prevent, as in zero divides No warning is given by Java in this case 5

Integer Overflow Example It s necessary to analyze the range of your results, under worst case circumstances. You often use a long to hold sums of ints, etc. Floating Point Properties Anomalous floating point values: Undefined, such as 0.0/0.0: produces result (Not a Number) Any operation involving produces as result Two values cannot be equal Check if number is by using methods: or Return boolean which is true if argument is Overflow, such as 1.0/0.0: produces result produces result Same rules, results as for Underflow, when result is smaller than smallest possible number we can represent Complex, not handled very well (represented as zero) Rounding errors see following examples 6

Example Float Rounding Program Occurs with doubles too; it s virtually the same, because 1.0 is more precise as a double 7

Doubles as Bad Loop Counters Notice accumulating, increasing error. Never use floats or doubles as loop counters Numerical Problems Problem Zero divide (overflow) 0/0 Overflow Underflow Rounding, accumulation errors Integer Exception thrown. Program crashes unless caught. Exception thrown. Program crashes unless caught No warning. Program gives wrong results. Not possible Not possible Float, double POSITIVE_INFINITY, NEGATIVE_INFINITY NaN (not a number) POSITIVE_INFINITY, NEGATIVE_INFINITY No warning, set to 0 No warning. Program gives wrong results. Common, bad news cases 8

More on Control Structures Three control structures Sequence: execute next statement This is default behavior Branching: if, else statements If, else are the primary construct used Switch statement used if many choices Iteration: while, do, for loops Additional constructs exist to terminate loops prematurely Switch statement Used as substitute for long if-else chains Branch condition must be integer, can t be String, float, etc. No ranges, just single values or expressions in switch C# allows strings as branch condition, but not Java or C++ 9

Terminating Iteration: Break Break statement in for, while or do-while loops transfers control to statement immediately after end of loop Terminating Iteration: Continue Continue statement jumps to end of loop but continues looping 10

Iteration Exercise Recall the definition of a factorial: n! = n * (n-1) * (n-2) * * 1 For example: 4! = 4 * 3 * 2 * 1 = 24 A factorial has the following properties: 0! = 1 n is a positive integer Write a main() that calculates the value of n! for a given n. Use, for example: Calculating the result Input n n >? No Yes fact *= n Print fact? Done 11

Sentinel Controlled Loop (Optional) Suppose that the user doesn t want the program to run just once. Instead he wants to be prompted again to enter a number and, when done, prompted to enter another number. In order to do that, a sentinel controlled loop will be used. The idea of a sentinel controlled loop is that there is a special value (the "sentinel") that is used to say when the loop is done. In this example, the user will enter -1 to tell the program to end. Assume the user enters the number via a JOptionPane. If you re writing the code by hand, don t worry about the exact syntax of JOptionPane; just assume the user enters a valid number. Revise your program. 12