Expressions. Eric McCreath

Similar documents
Chapter 4: Basic C Operators

9 Using Equation Networks

Operators. Java operators are classified into three categories:

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

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

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

Bil 104 Intiroduction To Scientific And Engineering Computing. Lecture 5. Playing with Data Modifiers and Math Functions Getting Controls

Beginning C Programming for Engineers

JAVA OPERATORS GENERAL

6-1 (Function). (Function) !*+!"#!, Function Description Example. natural logarithm of x (base e) rounds x to smallest integer not less than x

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

Introduction to Programming

ISA 563 : Fundamentals of Systems Programming

ANSI C Programming Simple Programs

Integer Representation. Variables. Real Representation. Integer Overflow/Underflow

LESSON 5 FUNDAMENTAL DATA TYPES. char short int long unsigned char unsigned short unsigned unsigned long

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

Numerical Data. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

Chapter 3. Computer Science & Engineering 155E Computer Science I: Systems Engineering Focus. Existing Information.

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.

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

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

C++ Programming Lecture 11 Functions Part I

Computer Programming: Skills & Concepts (CP) arithmetic, if and booleans (cont)

2 Making Decisions. Store the value 3 in memory location y

9/2/2016. Expressions are Used to Perform Calculations. ECE 120: Introduction to Computing. Five Arithmetic Operators on Numeric Types

Lectures 4 and 5 (Julian) Computer Programming: Skills & Concepts (INF-1-CP1) double; float; quadratic equations. Practical 1.

1/31/2017. Expressions are Used to Perform Calculations. ECE 120: Introduction to Computing. Five Arithmetic Operators on Numeric Types

Computer Science & Engineering 150A Problem Solving Using Computers

Will introduce various operators supported by C language Identify supported operations Present some of terms characterizing operators

Chapter 3: Operators, Expressions and Type Conversion

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

Computer Science & Engineering 150A Problem Solving Using Computers. Chapter 3. Existing Information. Notes. Notes. Notes. Lecture 03 - Functions

Standard Library Functions Outline

SECTION II: LANGUAGE BASICS

PROGRAMMING WITH MATLAB DR. AHMET AKBULUT

Bits, Words, and Integers

Ordinary Differential Equation Solver Language (ODESL) Reference Manual

CSI31 Lecture 5. Topics: 3.1 Numeric Data Types 3.2 Using the Math Library 3.3 Accumulating Results: Factorial

Review Activity 1 CALL and RET commands in assembler

Intrinsic Functions Outline

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

Chapter 2. Outline. Simple C++ Programs

Maths Functions User Manual

Arithmetic Operators. Portability: Printing Numbers

Lecture 14. Daily Puzzle. Math in C. Rearrange the letters of eleven plus two to make this mathematical statement true. Eleven plus two =?

Unit 3. Operators. School of Science and Technology INTRODUCTION

Section A Arithmetic ( 5) Exercise A

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

Engineering Problem Solving with C++, Etter/Ingber

Exercises C-Programming

Introduction to C Language

CT 229. Java Syntax 26/09/2006 CT229

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

Lesson 1: Arithmetic Review

Graphics calculator instructions

Expressions and operators

Goals for This Lecture:

ENGINEERING 1020 Introduction to Computer Programming M A Y 2 6, R E Z A S H A H I D I

Introduction to Python, Cplex and Gurobi

CSC 1214: Object-Oriented Programming

2 Unit Bridging Course Day 10

Object Oriented Programming Using C++ Mathematics & Computing IET, Katunayake

Operators in java Operator operands.

Chapter 3 : Computer Science. Class XI ( As per CBSE Board) Data Handling. Visit : python.mykvs.in for regular updates

Our Strategy for Learning Fortran 90

CT 229 Java Syntax Continued

Data Types and Variables in C language

Operators in C. Staff Incharge: S.Sasirekha

CS313D: ADVANCED PROGRAMMING LANGUAGE

INTERMEDIATE LEVEL PYTHON PROGRAMMING SELECTION AND CONDITIONALS V1.0

Prof. Navrati Saxena TA: Rochak Sachan

CHAPTER 3: CORE PROGRAMMING ELEMENTS

Expressions and Variables

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

Introduction to Programming II W4260. Lecture 2

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University

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

Lesson 5: Introduction to the Java Basics: Java Arithmetic THEORY. Arithmetic Operators

QUIZ: What value is stored in a after this

Computer Programming 5th Week loops (do-while, for), Arrays, array operations, C libraries

Macro Programming Reference Guide. Copyright 2005 Scott Martinez

Summary of basic C++-commands

Chapter 7. Expressions and Assignment Statements ISBN

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

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

APPENDIX P. Derived Parameter Specification

Operators. Lecture 3 COP 3014 Spring January 16, 2018

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

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

1 Introduction to C part I

MA 511: Computer Programming Lecture 3: Partha Sarathi Mandal

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

Numeric Data Types in C

Arithmetic and Logic Blocks

Selection Statements. Pseudocode

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

UNIT- 3 Introduction to C++

UNIVERSITY OF ENGINEERING & MANAGEMENT, KOLKATA C ASSIGNMENTS

4. Number Representations

Transcription:

Expressions Eric McCreath

2 Expressions on integers There is the standard set of interger operators in c. We have: y = 4 + 7; // add y = 7-3; // subtract y = 3 * x; // multiply y = x / 3; // integer divide y = x % 3; // integer remainder y = -x; // integer negate c uses the standard ordering when determining the order these operators are applied. e.g. x * 7 + 5 is (x*7) + 5 and not x * (7 + 5). The basic rule of thumb for precedence: "if you didn't learn it in high school then add brackets". Or if you are uncertain then add brackets. Interestingly, the way the '%' operator works on negative numbers is not exactly specified in the c language.

3 The assignment expression The assignment operator is also an expression that returns the value that it assigned. int x,y; x = y = 7; // this is okay because y is assigned to 7 and // this assignment operator returns 7 which is assigned to x. Take care with this as it can make your code harder to understand.

4 Expressions on floats Like the expressions on integers there is a standard set of operations for floats. y = 4.0 + 7.0; // add y = 7.0-3.0; // subtract y = 3.0 * x; // multiply y = x / 3.0; // divide y = -x; // negate If you mix integers with floats, c will convert the integer to a float and complete the operation using a floating operation. If you wish convert a float to an integer then you can cast it with (int). e.g. int value; value = (int) 3.4;

5 Comparison You can compare both integers and floats using: < less than <= less than or equal to > greater than >= greater than or equal to == equal to!= not equal to If you are using "==" on floating point numbers then this would generally indicate a problem with your code.

6 Working with Booleans Booleans are represented using integers (remeber 0 is false, non-zero is true). Operators on booleans include: && logical add logical or! logical not The == operator may not work on booleans as two true booleans in c may have different values. You can use "(a && b) (!a &&!b)" to see if booleans a and b are equal. Generally avoid side effects within boolean expressions.

7 operations on bits There is a number of operations that you can do on bits within an integer. These include: x >> 4 right shift x by 4 bits x << 2 left shift x by 2 bits x & y bitwise "and" of x with y x y bitwise "or" of x with y ~x negate each bit within x x ^ y bitwise "xor" of x with y Arithmetic right shift fills the left most bits with the sign bit. Logical right shift fills the left most bits with 0. ANSI c does not specify if arithmetic or logical shift is used.

8 conditional operator One handy, but sometimes forgotten, operator is the ternary conditional operator. (boolean_expression? expression1 : expression1) // in the above expression if the boolean_expression evaluates to true // then expression1 is evaluated and returned otherwise, // expression2 is evaluated and returned. The use of this operator will often save on using a extra local variable along with a if-else conditional. e.g. Using a if-else: int maxval; if (a > b) { maxval = a; } else { maxval = b; } printf("the max is : %d\n",maxval); Using ternary condidtional: printf("the max is : %d\n",(a>b?a:b));

9 math.h Operators like square root, power, sin, cos,... are not part of the c language. However, there are standard libraries that enable you to compute these functions. The math.h library contains a useful range of mathematical operators. These include: See: cos(x) calculate the cosine of x sin(x) calculate the sine of x acos(theta) calculate the arc sine of theta pow(x,y) take x to the power of y M_PI the pi constant sqrt(x) calculate the square root of x http://en.wikibooks.org/wiki/c_programming/c_reference/math.h for a more extensive list. On some compilers you may need to link the math library for these to work (in gcc use the "-lm" option).

10 Exersizes Write a program that calculates the area of a triangle given the lengths of the sides. The program should take input on a single line with three space separated floats which are the side lengths of the triangle. It should output on a single line the area of the triangle. You may assume the triangles are possible. Hint using Heron's formula (where,, are the side lengths):