Perl: Arithmetic, Operator Precedence and other operators. Perl has five basic kinds of arithmetic:

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

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

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

Unit 3. Operators. School of Science and Technology INTRODUCTION

Operators & Expressions

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

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

Chapter 4: Basic C Operators

QUIZ: What value is stored in a after this

Expressions and Precedence. Last updated 12/10/18

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

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

Information Science 1

JAVA OPERATORS GENERAL

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

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

Operators. Lecture 3 COP 3014 Spring January 16, 2018

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

Department of Computer Science

Prof. Navrati Saxena TA: Rochak Sachan

Lecture 3 Tao Wang 1

Chapter 2: Using Data

Information Science 1

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

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.

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

Lecture 3. More About C

Operators. Java operators are classified into three categories:

Fundamentals of Programming CS-110. Lecture 3

Add Subtract Multiply Divide

WEEK 4 OPERATORS, EXPRESSIONS AND STATEMENTS

Operators in C. Staff Incharge: S.Sasirekha

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

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

Variables and Operators 2/20/01 Lecture #

Expression and Operator

by Pearson Education, Inc. All Rights Reserved. 2

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #23 Loops: Precedence of Operators

Programming in C++ 5. Integral data types

LECTURE 3 C++ Basics Part 2

Lesson 3: Basic Programming Concepts

Lecture 3 Operators MIT AITI

Chapter 3: Operators, Expressions and Type Conversion

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

CSE 115. Introduction to Computer Science I

Operators and Expressions:

Programming for Engineers: Operators, Expressions, and Statem

More Programming Constructs -- Introduction

Week 4 Lecture 1. Expressions and Functions

Operators And Expressions

Chapter 2, Part III Arithmetic Operators and Decision Making

SECTION II: LANGUAGE BASICS

Unit-2 (Operators) ANAND KR.SRIVASTAVA

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

Chap 6 - Introduction to HDL (b)

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

Overview of C, Part 2. CSE 130: Introduction to Programming in C Stony Brook University

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

JAVASCRIPT - OPERATORS

Basics of Java Programming variables, assignment, and input

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

VARIABLES & ASSIGNMENTS

ECEN 468 Advanced Logic Design

CS Kangmei Yang. Page 1

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

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

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

Slide 1 CS 170 Java Programming 1 Expressions Duration: 00:00:41 Advance mode: Auto

Lesson 3: Arithmetic & Casting. Pic 10A Ricardo Salazar

Sir Muhammad Naveed. Arslan Ahmed Shaad ( ) Muhammad Bilal ( )

Decisions, Decisions. Testing, testing C H A P T E R 7

Arithmetic Operators. Binary Arithmetic Operators. Arithmetic Operators. A Closer Look at the / Operator. A Closer Look at the % Operator

Operators and Expressions in C & C++ Mahesh Jangid Assistant Professor Manipal University, Jaipur

A flow chart is a graphical or symbolic representation of a process.

Computer Programming CS F111

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

Introduction to Computers. Laboratory Manual. Experiment #3. Elementary Programming, II

Expressions and Statementst t. Assignment Operator. C Programming Lecture 6 : Operators. Expression

CT 229. Java Syntax 26/09/2006 CT229

Course PJL. Arithmetic Operations

ECE 122 Engineering Problem Solving with Java

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

Chapter 3 Structure of a C Program

4. Inputting data or messages to a function is called passing data to the function.

Recursion and Induction: Haskell; Primitive Data Types; Writing Function Definitions

Chapter 2. Lexical Elements & Operators

Chapter 4: Control Structures I (Selection)

Selenium Class 9 - Java Operators

C OVERVIEW BASIC C PROGRAM STRUCTURE. C Overview. Basic C Program Structure

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

Chapter 2: Data Types and Arithmetic Expressions TRUE/FALSE

Programming Fundamentals - A Modular Structured Approach using C++ By: Kenneth Leroy Busbee

Structures, Operators

Ch. 12: Operator Overloading

COMP 110 Introduction to Programming. What did we discuss?

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

Operators and Expressions

C OVERVIEW. C Overview. Goals speed portability allow access to features of the architecture speed

In Fig. 3.5 and Fig. 3.7, we include some completely blank lines in the pseudocode for readability. programs into their various phases.

I Internal Examination Sept Class: - BCA I Subject: - Principles of Programming Lang. (BCA 104) MM: 40 Set: A Time: 1 ½ Hrs.

Transcription:

Perl: Arithmetic, Operator Precedence and other operators Robert A. Fulkerson College of Information Science and Technology http://www.ist.unomaha.edu/ University of Nebraska at Omaha http://www.unomaha.edu/ Arithmetic and other Basic Perl Operators Perl has five basic kinds of arithmetic: Addition Subtraction Multiplication Division Modulus (remainder division) There are also exponentiation, repetition and concatenation operators. Slide #2 Basic Perl arithmetic operators Addition (+): my $kf = 3 + 4; Subtraction (-): my $mc = 3-4; Multiplication (*): my $kr = 5 * 3; # assigns 7 to scalar $kf # assigns - to scalar $mc # assigns 5 to scalar $kr Division (/): my $ko = 6 / 5; Modulus (%) my $cj = 6 % 4; # assigns.2 to $ko # assigns 2 to $cj Slide #3

Integer Arithmetic Regular integer division (/) will generate real number results (i.e, 5/2 is 2.5). If you desire whole number results (such as with long division), you can use the int() function with division (/) to convert your result to an integer or whole number value. int (numerator / denominator) will tell you how many times denominator goes into numerator completely. numerator % denominator will tell you what's left over (the remainder) after denominator goes into numerator as many times as it can. Slide #4 Integer Arithmetic with / (division) and the int() function 3. my $x = 0; 4. say $x / 2; # outputs 5 5. say $x / 3; # outputs 3.33333333333333 6. say $x / 4; # outputs 2.5 7. say $x / 5; # outputs 2 8. say $x / 6; # outputs.66666666666667 9. say $x / ; # outputs 0.909090909090909 0. # the three lines below do division with int(). say int ( $x / 3 ); # outputs 3 2. say int ( $x / 4 ); # outputs 2 3. say int ( $x / );# outputs 0 Slide #5 Integer Arithmetic with % (remainder) 3. my $x = 0; 4. say $x % 2; # outputs 0 5. say $x % 3; # outputs 6. say $x % 4; # outputs 2 7. say $x % 5; # outputs 0 8. say $x % 6; # outputs 4 9. say $x % ; # outputs 0 The math in line 5 is the same as: 3 R 3 0-9 Slide #6 2

Order of Precedence Rules Operators are evaluated in an expression according to the rules of precedence. Operators within () are evaluated first. *, /, % evaluated next. +, - evaluated last. Slide #7 Order of Precedence Examples my $x = 5 + 6-7 * 8; 2 3 my $average = ( + 2 + 3 + 4 + 5) / 5; 2 3 4 5 my $z = 5 * 60 % 7 / 8 + 7 * 6; 2 3 5 4 my $z2 = int( 5 * 60 % 7 / 8 ) + 7 * 6; 2 3 5 4 $x is -45, $average is 3, $z is 42.75 and $z2 is 42 Slide #8 More basic Perl operators Concatenation (.): my $noun = "kid"; $noun = $noun. 's'; print $noun; # prints kids Repetition (x): my $word = "ba" x 4; print $word; # prints babababa my $zz = 8x8; print $zz; # prints 88888888 my $a = ("xy" x 2. "z") x 2; prints $a # prints xyxyzxyxyz print "=" x 78, "\n"; # line of 78 = signs Slide #9 3

Compound Perl operators Addition and assignment (+=): my $kf = 3; $kf += 4; # assigns 7 to scalar $kf Subtraction and assignment (-=): my $mc = 3; $mc -= 4; # assigns - to scalar $mc Multiplication and assignment (*=): my $kr = 5; $kr *= 3; # assigns 5 to scalar $kr Division and assignment (/=): my $ko = 6; $ko /= 5 ; # assigns.2 to $ko Slide #0 Compound Perl operators continued Modulus and assignment (%=) my $cj = 6; $cj %= 4 ; # assigns 2 to $cj Concatenation assignment (.=) my $noun = "kid"; $noun.= 's'; Repetition assignment (x=) # assigns kids to $noun my $word = "blah "; $word x= 3; # assigns blah blah blah Slide # Exponentiation Unlike most programming languages, Perl has an exponentiation operator: $result = 8 ** 3; # 52 ** is the exponentiation operator, and takes the left operand to the right operand's power and returns the result. $num = 2; $num **= 4; print "$num\n"; # 6 What about this: $newmath = -7 ** 2; print $newmath. "\n"; # prints -49? Slide #2 4

Precedence of operators In the previous example, the unary - operator has lower precedence than **, so -7 ** 2 is interpreted as: - (7 ** 2) so what you want to do is: $newmath = (-7) ** 2; print $newmath. "\n"; # prints 49! Slide #3 Pre vs. Post for increment (++) and decrement (--) Pre-increment (++) adds one to the current scalar's value and then uses the new value in the larger expression it's contained within, such as assignment, arithmetic, output, etc. Post-increment uses the current scalar's value in the larger expression it's contained within (assignment, arithmetic, output, etc) and then adds one to the scalar's value. Pre- and post-decrement (--) have similar behaviours. Slide #4 Integer Arithmetic with / (division) and the int() function 3. my $x = 0; 4. $x = $x + ; say $x; # the long, clear way 5. $x += ; say $x; # a shorter way 6. $x++; say $x; # super-short way 7. ++$x; say $x; # super-short again 8. $x = 6; # assigns 6 to x 9. say $x++; # 6 and post-incr 0.say $x; # prints 7.say ++$x; # 8 and pre-increment 2.say $x; # prints 8 Slide #5 5

Integer Arithmetic with / (division) and the int() function 3. my $x = 8; 4. my $z = $x-- + 5; # $z is 3, $x is 7 5. say "z is $z and x is $x."; 6. my $y = 5 * --$x; # $z is 30, $x is 6 7. say "y is $y and x is $x."; Slide #6 Precedence ordering so far Operator Description Associativity. () parentheses left-to-right, inside-out 2. ++ -- increment / decrement nonassociative 3. ** exponentiation right-to-left 4. + - unary sign right-to-left 5. * / % x multiplicative left-to-right 6. + -. additive left-to-right 7. = += -= *= (compound) assignment right-to-left /= %=.= x= **= Associativity refers to how operators are evaluated if there are multiples of the same type of operator present in the same expression. All operators are binary - require an operand on the left and right sides - except for the increment/decrement and unary sign operators. Slide #7 6