Arithmetic Operators Topics Arithmetic Operators Operator Precedence

Slides:



Advertisements
Similar presentations
CMSC 104, Version 9/011 Arithmetic Operators Topics Arithmetic Operators Operator Precedence Evaluating Arithmetic Expressions In-class Project Incremental.
Advertisements

Java Programming, 3e Concepts and Techniques Chapter 3 Section 63 – Manipulating Data Using Methods – Day 2.
1 ICS103 Programming in C Lecture 4: Data Types, Operators & Expressions.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in.
Introduction to Computers and Programming - Class 2 1 Introduction to Computers and Programming Class 2 Introduction to C Professor Avi Rosenfeld.
1 Expressions, Operators Expressions Operators and Precedence Reading for this class: L&L, 2.4.
CMSC 104, Version 9/01 1 The Box Problem: Write an interactive program to compute and display the volume and surface area of a box. The program must also.
Input, Output, and Processing
Arithmetic Operators Topics Arithmetic Operators Operator Precedence
Assignment Statements Operator Precedence. ICS111-Java Programming Blanca Polo 2 Assignment, not Equals  An assignment statement changes the value of.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
CHAPTER 2 COMPONENTS OF A PROGRAMMING LANGUAGE I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
CMSC 104, Version 9/01 1 Functions, Part 3 of 3 Topics: Coding Practice o In-Class Project: The Box o In-Class Project: Drawing a Rectangle Reading: None.
JavaScript 101 Lesson 3: Variables and Arithmetic.
CMSC 104, Version 8/061L10ArithmeticOps.ppt Arithmetic Operators Topics Arithmetic Operators Operator Precedence Evaluating Arithmetic Expressions In-class.
Doing math In java.
1 Variables and Arithmetic Operators in JavaScript.
HAWKES LEARNING SYSTEMS Students Matter. Success Counts. Copyright © 2013 by Hawkes Learning Systems/Quant Systems, Inc. All rights reserved. Section 7.5.
CSCI 1100/1202 January 18, Arithmetic Expressions An expression is a combination of operators and operands Arithmetic expressions compute numeric.
 Most C programs perform calculations using the C arithmetic operators (Fig. 2.9).  Note the use of various special symbols not used in algebra.  The.
Notes 2.1 Order of Operations PEMDAS If an expression has only numbers and symbols, it is a numerical expression. If it has a variable it is a variable.
1-2 Order of Operations Objective: Use the order of operations to evaluate expressions.
1 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.
Chapter Sections 1.1 – Study Skills for Success in Mathematics
Introduction to Computing Science and Programming I
Topics Designing a Program Input, Processing, and Output
UMBC CMSC 104 – Section 01, Fall 2016
Expressions.
UMBC CMSC 104 – Section 01, Fall 2016
Expressions.
ICS103 Programming in C Lecture 4: Data Types, Operators & Expressions
2.5 Another Java Application: Adding Integers
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Arithmetic operations & assignment statement
Nahla Abuel-ola / WIT.
Assignment and Arithmetic expressions
Variables and Arithmetic Operators in JavaScript
Computer Science 101 While Statement.
Arithmetic Operators Topics Arithmetic Operators Operator Precedence
Arithmetic Operator Operation Example + addition x + y
Lecture 3 Expressions Richard Gesick.
Arithmetic Operators in C
Arithmetic Expressions in C
Arithmetic Operators Topics Arithmetic Operators Operator Precedence
Introduction to C++ Programming
Variables and Arithmetic Operators in JavaScript
A First Book of ANSI C Fourth Edition
Arithmetic Operators in C
Arithmetic Expressions & Data Conversions
Introduction to C Topics Compilation Using the gcc Compiler
CS150 Introduction to Computer Science 1
Exponents, Parentheses, and the Order of Operations
Doing Arithmetic Today’s lecture is in chapter 2 Assignment statement:
Algorithms computer as the tool process – algorithm
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
CS150 Introduction to Computer Science 1
Data Types and Expressions
Topics Designing a Program Input, Processing, and Output
Introduction to C Topics Compilation Using the gcc Compiler
Topics Designing a Program Input, Processing, and Output
Arithmetic Operators Topics Arithmetic Operators Operator Precedence
Using C++ Arithmetic Operators and Control Structures
The while Looping Structure
is multiplied by a variable of type to yield a result of type
Chapter 2, Part III Arithmetic Operators and Decision Making
Data Types and Expressions
Functions, Part 3 of 4 Topics: Coding Practice Reading: None
Arithmetic Expressions & Data Conversions
Data Types and Expressions
Presentation transcript:

Arithmetic Operators Topics Arithmetic Operators Operator Precedence Evaluating Arithmetic Expressions In-class Project Incremental Programming Reading Section 2.5

Arithmetic Operators in C Name Operator Example Addition + num1 + num2 Subtraction - initial - spent Multiplication * width * 4 Division / sum / count Modulus % m % n

Division If both operands of a division expression are integers, you will get an integer answer. The fractional portion is thrown away. Examples : 17 / 5 = 3 3 / 3 = 1 35 / 9 = 3

Division (con’t) Division where at least one operand is a floating point number will produce a floating point answer. Examples : 17.0 / 5 = 3.4 4 / 3.2 = 1.25 35.2 / 9.1 = 3.86813 What happens? The integer operand is temporarily converted to a floating point, then the division is performed.

Division By Zero Division by zero is mathematically undefined. If you allow division by zero in a program, it will cause a fatal error. Your program will terminate execution and give an error message. Non-fatal errors do not cause program termination, just produce incorrect results.

Modulus The expression m % n yields the integer remainder after m is divided by n. Modulus is an integer operation -- both operands MUST be integers. Examples : 17 % 5 = 2 6 % 3 = 0 9 % 2 = 1 5 % 8 = 5

Uses for Modulus Used to determine if an integer value is even or odd 5 % 2 = 1 odd 4 % 2 = 0 even If you take the modulus by 2 of an integer, a result of 1 means the number is odd and a result of 0 means the number is even. The Euclid’s GCD Algorithm (done earlier)

Arithmetic Operators Rules of Operator Precedence Operator(s) Precedence & Associativity ( ) Evaluated first. If nested (embedded), innermost first. If on same level, left to right. * / % Evaluated second. If there are several, evaluated left to right. + - Evaluated third. If there are several, evaluated left to right. = Evaluated last, right to left.

a + b * c Would multiply b * c first, then add a to the result. Using Parentheses Use parentheses to change the order in which an expression is evaluated. a + b * c Would multiply b * c first, then add a to the result. If you really want the sum of a and b to be multiplied by c, use parentheses to force the evaluation to be done in the order you want. (a + b) * c Also use parentheses to clarify a complex expression.

Practice With Evaluating Expressions Given integer variables a, b, c, d, and e, where a = 1, b = 2, c = 3, d = 4, evaluate the following expressions: a + b - c + d a * b / c 1 + a * b % c a + d % b - c e = b = d + c / b - a

A Sample Project Let’s write a program that computes and displays the volume and surface area of a box. (I’ll help with prompting the user and displaying the results.) Procedure: Use the pseudocode that we developed in “Algorithms, Part 3 of 3” Convert the algorithm to code Clean up the code (spacing, indentation, commenting)

The Box - Pseudocode Display “Enter the height: “ Read <height> While (<height> <= 0 ) Display “The height must be > 0” End_while

The Box - Pseudocode (con’t) Display “Enter the width: “ Read <width> While (<width> <= 0 ) Display “The width must be > 0” End_while

The Box - Pseudocode (con’t) Display “Enter the depth: “ Read <depth> While (<depth> <= 0 ) Display “The depth must be > 0” End_while

The Box - Pseudocode (con’t) <volume> = <height> X <width> X <depth> <surface1> = <height> X <width> <surface2> = <width> X <depth> <surface3> = <height> X <depth> <surface area> = 2 X (<surface1> + <surface2> + <surface3>)

The Box - Pseudocode (con’t) Display “Height = “, <height> Display “Width = “, <width> Display “Depth = “, <depth> Display “Volume = “, <volume> Display “Surface Area = “, <surface area>

Assignment and Next Read Sections 2.5. Get familiar with any Linux text editor, xemacs or emacs. Handout Project 1. Project 1 due Wednesday 10/23/2002. Next: Loops. Section 3.7, 4.1 – 4.3