Expressions and operators

Similar documents
12. Numbers. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

Primitive Data Types: Intro

Chapter 5 Methods. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

DUBLIN CITY UNIVERSITY

DUBLIN CITY UNIVERSITY

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

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

Functions. Functions are everywhere in C. Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR

CS110: PROGRAMMING LANGUAGE I

9 Using Equation Networks

9 C Language Function Block

C Programs: Simple Statements and Expressions

Chapter 5 Methods. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

Functions. Prof. Indranil Sen Gupta. Dept. of Computer Science & Engg. Indian Institute t of Technology Kharagpur. Introduction

COP3502 Programming Fundamentals for CIS Majors 1. Instructor: Parisa Rashidi

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

To define methods, invoke methods, and pass arguments to a method ( ). To develop reusable code that is modular, easy-toread, easy-to-debug,

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

Benefits of Methods. Chapter 5 Methods

JAVA OPERATORS GENERAL

Mathematical Functions, Characters, and Strings. CSE 114, Computer Science 1 Stony Brook University

Operators & Expressions

Chapter 4 Mathematical Functions, Characters, and Strings

Chapter 4: Basic C Operators

ECET 264 C Programming Language with Applications

Mathematical Functions, Characters, and Strings. CSE 114, Computer Science 1 Stony Brook University

Chapter 5 Methods / Functions

Operators. Java operators are classified into three categories:

Lecture 2:- Functions. Introduction

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

Chapter 5 Methods. Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

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

1st and 3rd September 2015

Chapter 5 Methods. Lecture notes for computer programming 1 Faculty of Engineering and Information Technology Prepared by: Iyad Albayouk

ENGI Introduction to Computer Programming M A Y 2 8, R E Z A S H A H I D I

Lecture 3 Operators MIT AITI

PASS$MOCK$EXAM$ $FOR$PRACTICE$ONLY$

Function. specific, well-defined task. whenever it is called or invoked. A function to add two numbers A function to find the largest of n numbers

Chapter 6 Methods. Dr. Hikmat Jaber

INTRODUCTION TO C++ FUNCTIONS. Dept. of Electronic Engineering, NCHU. Original slides are from

Operators Functions Order of Operations Mixed Mode Arithmetic VOID Data. Syntax and type conventions Using the Script window interface

Fundamentals of Programming CS-110. Lecture 3

Operators in C. Staff Incharge: S.Sasirekha

Functions. Autumn Semester 2009 Programming and Data Structure 1. Courtsey: University of Pittsburgh-CSD-Khalifa

FAQ No. 53. ihost: Logic Points. Roles and Privileges. Adding and removing logic points. Accessing and using the Logic Editor

Chapter 2. Outline. Simple C++ Programs

JAVA Programming Concepts

Using Free Functions

CT 229 Java Syntax Continued

Summary of basic C++-commands

Programming in C Quick Start! Biostatistics 615 Lecture 4

Expressions. Eric McCreath

Fixed point algorithmic math package user s guide By David Bishop

Programmierpraktikum

Week 2: Console I/O and Operators Arithmetic Operators. Integer Division. Arithmetic Operators. Gaddis: Chapter 3 (2.14,3.1-6,3.9-10,5.

Math 1 Variable Manipulation Part 2 Exponents & Roots

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

Chapter 5 Methods. Modifier returnvaluetype methodname(list of parameters) { // method body; }

Introduction to Programming

PIC 10A. Lecture 3: More About Variables, Arithmetic, Casting, Assignment

Unit 3. Operators. School of Science and Technology INTRODUCTION

The Math Class (Outsource: Math Class Supplement) Random Numbers. Lab 06 Math Class

Prof. Navrati Saxena TA: Rochak Sachan

Section 1.8. Simplifying Expressions

Data Types and Basic Calculation

MA 511: Computer Programming Lecture 3: Partha Sarathi Mandal

Preview from Notesale.co.uk Page 2 of 79

Engineering Problem Solving with C++, Etter/Ingber

Functions and Inverses ID1050 Quantitative & Qualitative Reasoning

Arithmetic. 2.2.l Basic Arithmetic Operations. 2.2 Arithmetic 37

APPENDIX P. Derived Parameter Specification

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

Telemetry Standards, IRIG Standard (Part 1), Appendix P, June 2011 APPENDIX P DERIVED PARAMETER SPECIFICATION. Paragraph Title Page

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

Chapter 4. Operations on Data

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

Sketchify Tutorial Properties and Variables. sketchify.sf.net Željko Obrenović

Chapter 15 Graphing Functions and Data

Professor Peter Cheung EEE, Imperial College

static int min(int a, int b) Returns the smaller of two int values. static double pow(double a,

SECTION II: LANGUAGE BASICS

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

Expression and Operator

C++ Programming: From Problem Analysis to Program Design, Third Edition

Chapter 2: Using Data

Operators and Expressions:

Expressions and Precedence. Last updated 12/10/18

Algebra 1 Review. Properties of Real Numbers. Algebraic Expressions

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

Calling Prewritten Functions in C

1.1 Your First Program! Naive ideal. Natural language instructions.

Math 10- Chapter 2 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

C++ Overview. Chapter 1. Chapter 2

Fundamentals: Expressions and Assignment

Ordinary Differential Equation Solver Language (ODESL) Reference Manual

Getting Started. Office Hours. CSE 231, Rich Enbody. After class By appointment send an . Michigan State University CSE 231, Fall 2013

Unit-2 (Operators) ANAND KR.SRIVASTAVA

Department of Computer Science

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

Transcription:

Mathematical operators and expressions The five basic binary mathematical operators are Operator Operation Example + Addition a = b + c - Subtraction a = b c * Multiplication a = b * c / Division a = b / c % Remainder a = b%c = Remainder of (b / c) The operators + and - also have unary versions that change the sign of a variable. Mathematical expressions are constructed using these operators and the assignment operator =. As in the last section start a new application, add a button to it, double click on the button to get the method that is run when the button Click event is raised. Insert the following example of a mathematical expression: double x = 2.5; double y = 20; double z = x*y+x; MessageBox.Show("z = "+z.tostring()); You should get the following after running the application and clicking the button: It is obvious in this example that first x was multiplied by y and then the result was added to x to get z. This brings up the issue of how the compiler selects the order of operations. Why did the compiler not calculate y+x first and then multiply it by x. This is because operators have an order of precedence which the compiler used to figure out which operations are done first. For mathematical expressions the order of precedence are: Unary + -, then * / %, and then + - Mehrdad Negahban, 2003. 1

Parenthesis are used to instruct the compiler on the order of operations. The compiler first calculates the contents of an () as one mathematical expression. Therefore if one replaces x*y+x in the above example by x*(y+x) the result of running the application would be: Note, in this case the compiler first calculated (y+x) to get 22.5 and then multiplied it by x. There are several increment and decrement operators that can be used to increment by one unit the variable. These are: Example A = ++B A = - -B A = B++ A = B- - Meaning First increment B by one and then assign it to A First subtract one from B and then assign it to A First assign B to A and then increment B by one First assign B to A and then subtract one from B Operator precedence is as follows where operators of equal precedence are evaluated from left to right. Precedence order Operators Highest ++ (prefix), - - (prefix), unary +, unary - *, /, % +, - =, *=, /=, %=, +=, -= Lowest ++ (suffix), -- (suffix) Mathematical assignment operator There are several assignment operators that can be used Name Example Meaning = A = B += A += B A = A+B -= A -= B A = A-B *= A *= B A = A*B /= A /= B A = A/B 12/1/2003 2

%= A %=B A = Remainder(A/B) Boolean expressions and operators Boolean expressions are expressions that evaluate to true or to false. The Boolean comparison operators in C# can be used with a variety of data types and variables and given in the following table: Operator Name Example = = Equal A = = B!= Not equal A!=B < Less than A < B > Grater than A > B <= Less than or equal A <= B >= Grater than or equal A >= B In each case the example expression results in a Boolean value of true if the expression is true and false if the expression is false. For example, try the following code in your application: bool result = 3<2; MessageBox.Show("result = "+result.tostring()); Once you run your application, you should get: There are a number of Boolean operators that are specifically for use with Boolean variables. These are given as: Operator Name Example! NOT!A ^ XOR (exclusive or) A^B && AND A&&B OR A B The following rules exist for the examples shown in the table. The logical AND provides a true value only when both A and B are true. The logical OR provides a true value when either A or B is true. The XOR provides a value of true when either A or B are true, but Mehrdad Negahban, 2003. 3

not when both are true. The NOT simply changes the value of A from true to false, or from false to true depending on if A is true or false. The && and have two additional forms & and which are similar but less safe when used with reference types since for these cases they check the address not the actual value. Boolean assignment operators As the arithmetic operators, the Boolean operators have several assignment operators Operator Example Meaning = A = B &= A &= B A = A & B = A = B A = A B ^= A ^= B A = A ^ B As an example enter the following code into your application: bool A = false; A &= true; MessageBox.Show("A = "+A.ToString()); The result of running your application should be: Boolean operator precedence is given in the following table: Precedence order Operator Highest precedence! <, >, <=, >= = =,!= & ^ && Lowest precedence &=, ^=, = 12/1/2003 4

As in arithmetic expressions, it is best to use parenthesis to control the order of operation. Math functions There are a number of mathematical functions and constants that can be used in developing mathematical expressions. These mathematical functions can be invoked by using the Math namespace. For example, insert the following code in your application to get the value of cos(π ) : double a = Math.Cos(Math.PI); MessageBox.Show("a = "+a.tostring()); The result of running your application should be The existing mathematical functions and constants are: Function Return type Meaning Abs(double x) double x Acos(double x) double arccos(x) Asin(double x) double arcsin(x) Atan(double x) double arctan(x) Atan2(double x, double y) double Angle that tangent is quotient of two numbers BigMul(int m, int n) long Full product of two 32-bit numbers Ceiling(double x) double Smallest hole number >= x Cos(double x) double cos(x), x in radians Cosh(double x) double cosh(x) DivRem(int m, int n, out int result) int Quotient of two integers m and n E double Natural logarithm base e Equals(object a, object b) bool True if a and b are equal Exp(double x) double Exponent of x Floor(double x) double Largest whole number <= x IEEERemainder(double x, double y) double Remainder of x/y Log(double x, double base) double Logarithm of x in given base Log(double x) double Natural logarithm of x (base e) Log10(double x) double Log base 10 or x Mehrdad Negahban, 2003. 5

Max(x,y), x and y are any data types Type of x,y Larger of x and y Min(x,y), x and y are any data types Type of x,y Smaller of x and y PI double π Pow(double x, double y) double x to the power y ReferenceEquals(object x, object y) bool Checks to see if x and y reference same object Round(double x, int decimals) double Nearest number to x with precision decimals (many variations) Sign(double x) int Returns an integer indicating sign of x Sin(double x) double sin(x) Sinh(double x) double sinh(x) Sqrt(double x) double Square root of x Tan(double x) double tan(x) Tanh( double x) double tanh(x) Some of these have variations not included in the table. Visual Studio can be used to find all variations. 12/1/2003 6