APS105 Calculating 1. Basic Math Operations 2 Basic Arithmetic Operators Operators: –Addition: + –Subtraction: - –Multiplication: * –Division: / Recall.

Slides:



Advertisements
Similar presentations
Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
Advertisements

True or false A variable of type char can hold the value 301. ( F )
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
Announcements Quiz 1 Next Week. int : Integer Range of Typically -32,768 to 32,767 (machine and compiler dependent) float : Real Number (i.e., integer.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Expressions and Operators Program Style.
Basic C Programming Data Types and Arithmetic Operations 01/30/15.
CIS 234: Order of Operations, Shortcut & Other Operators Dr. Ralph D. Westfall February, 2004.
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
More about Numerical Computation CS-2301, B-Term More about Numerical Computation CS-2301, System Programming for Non-Majors (Slides include materials.
1 Expressions, Operators Expressions Operators and Precedence Reading for this class: L&L, 2.4.
1  Ex: Declare a variable to store user’s age: int age; Prompt the user to enter his/her age: printf (“ How old are you? “); Read / scan the entered value.
Data Types, Expressions and Functions (part I)
Performing Computations C provides operators that can be applied to calculate expressions: example: tax is 8.5% of the total sale expression: tax =
Expressions, Data Conversion, and Input
Chapter 2 part #4 Operator
Object-Oriented Programming Using C++ Third Edition Chapter 2 Evaluating C++ Expressions.
CHAPTER 2 PART #4 OPERATOR 2 nd semester King Saud University College of Applied studies and Community Service Csc 1101 By: Asma Alosaimi Edited.
Assignment Statements Operator Precedence. ICS111-Java Programming Blanca Polo 2 Assignment, not Equals  An assignment statement changes the value of.
Arithmetic Operations. Review function statement input/output comment #include data type variable identifier constant declaration.
Integer numerical data types. The integer data types The integer data types use the binary number system as encoding method There are a number of different.
CPS120: Introduction to Computer Science Operations Lecture 9.
Operators & Identifiers The Data Elements. Arithmetic Operators exponentiation multiplication division ( real ) division ( integer quotient ) division.
CSC 107 – Programming For Science. The Week’s Goal.
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.
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.
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
This will all add up in the end. Assignment operator =Simple Assignment operator Arithmetic Operators +Additive operator – Subtraction operator * Multiplication.
Programs That Calculate. Arithmetic Expressions +addition -subtruction *multiplication /division  Order of Operations BEDMAS (brackets, exponentiation,
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)
By: Mr. Baha Hanene Chapter 6. LEARNING OUTCOMES This chapter will cover the learning outcome 02 i.e. 2.Use basic data-types and input / output in C programs.
Doing math In java.
1 Week 5 l Primitive Data types l Assignment l Expressions l Documentation & Style Primitive Types, Assignments, and Expressions.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
Boolean expressions, part 1: Compare operators. Compare operators Compare operators compare 2 numerical values and return a Boolean (logical) value A.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103 material developed.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
ICS102 Lecture 1 : Expressions and Assignment King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer.
Chapter INTRODUCTION Data Types and Arithmetic Calculations.
1 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
L131 Assignment Operators Topics Increment and Decrement Operators Assignment Operators Debugging Tips rand( ) math library functions Reading Sections.
What will each of the following lines print? System.out.println("number" ); number645 System.out.println("number" + (6 + 4)+ 5); number105 System.out.println(6.
1 Section 3.2b Arithmetic Operators Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU Status 6/10/2016 Initial content copied verbatim from ECE 103 material developed.
Chapter 3 Math Operations. Objectives Use the assignment and arithmetic operators. Use operators in output statements. Explain the problem with division.
Variables, Operators, and Expressions
Expressions.
Lecture 3 Java Operators.
Relational Operations
Multiple variables can be created in one declaration
Assignment and Arithmetic expressions
Arithmetic Operator Operation Example + addition x + y
Lecture 3 Expressions Richard Gesick.
More about Numerical Computation
Arithmetic Expressions & Data Conversions
Chapter-3 Operators.
Introduction to Programming
Expressions and Assignment
Assignment Operators Topics Increment and Decrement Operators
Data Types and Expressions
Chapter 4: Expression and Operator
Assignment Operators Topics Increment and Decrement Operators
Assignment Operators Topics Increment and Decrement Operators
Operator King Saud University
Data Types and Expressions
Assignment Operators Topics Increment and Decrement Operators
Arithmetic Expressions & Data Conversions
Data Types and Expressions
Presentation transcript:

APS105 Calculating 1

Basic Math Operations 2

Basic Arithmetic Operators Operators: –Addition: + –Subtraction: - –Multiplication: * –Division: / Recall order of operations: –* and / have highest precedence –left to right otherwise Speficy grouping/precedence: ( ) –Not: { } or [ ] 3

Examples 4

The Modulo Operator % x % y returns the remainder of x / y has same precedence as / and * x and y must be ints –signs of x and y are ignored if either is negative –but result takes the sign of x (is negative if x is) 5

Modulo Examples. 6

Calculating with Char Values Recall ‘A’ is encoded in ASCII as decimal 65 char letter = ‘A’; // letter = 65 Since char value is a number can do math. 7

Increment and Decrement increment: ++ –adds one to the variable –AND changes the variable to this new value –i++; // means the same as i = i + 1; i = 10; i++; // i = 11 afterwards decrement: -- –subtracts one from the variable –AND changes the variable to this new value –i--; // means the same as i = i - 1; i = 10; i--; // i = 9 afterwards 8

Assignment 9

An assignment statement contains an ‘=‘ Evaluation order: –right-hand-side (RHS) of = is evaluated first –Then assignment of result to left-hand-side (LHS) Example: x = 5; x = x + 2; 10

Operation-Assignment Some operations can be combined with ‘=‘ –Ie., +=, -=, *=, /=, %= Examples: x += 2; x += 2 * y; 11

Library Functions 12

Library Functions Library functions: –Commonly-used functions –Packaged together as a “library” –Gain access to them by: Including the library in your program AND telling the compiler to link to the library Example: using the math library 13

Common Functions Note: all arguments/return-values are doubles (absolute value) 14

Random Numbers Generating truly random nums is hard! –Eg., gambling machine pattern discovered –Why? Most programs use “pseudo-random” nums –not perfectly random, close enough rand() function –in program (at top): #include – rand() returns an integer within 0..RAND_MAX 15

Example using rand() Generate a random int between 0 and 1 Generate a random int between 0 and 100 Generate a random int between 1 and 10 Generate a random even int bet. 2 and 10 16

Mixing Types and Casting 17

Mixing Types If multiple types are used in an expression –the wider (more accurate) type is “contagious” Example1:

Mixing Types: Example2 8 / // 19

Casting Casting lets you change one type into another –use casting to get the result type you want! int x = (int) 5.7; // Precedence of cast: –higher than * / –lower than

Cast Examples int x = (int) ; int x = (int) ( ); 21

More Cast Examples int x = (3 / 4) * 8; // 22