Number and String Operations

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Python November 14, Unit 7. Python Hello world, in class.
 2002 Prentice Hall. All rights reserved. 1 Chapter 2 – Introduction to Python Programming Outline 2.1 Introduction 2.2 First Program in Python: Printing.
Introduction to C Programming
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
Python Programming Fundamentals
Week 9 PHP Cookies and Session Introduction to JavaScript.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
06/10/ Working with Data. 206/10/2015 Learning Objectives Explain the circumstances when the following might be useful: Disabling buttons and.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 2 Input,
Input, Output, and Processing
Mathematical Calculations in Java Mrs. G. Chapman.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
Mathematical Calculations in Java Mrs. C. Furman.
C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Chapter Two: Fundamental Data Types Slides by Evan Gallagher.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
JavaScript Part 1 Introduction to scripting The ‘alert’ function.
Math operations 9/19/16.
Topics Designing a Program Input, Processing, and Output
User-Written Functions
Chapter 6 JavaScript: Introduction to Scripting
Chapter 2 More on Math More on Input
Introduction to Python
Expressions.
Formatting Output.
Loops BIS1523 – Lecture 10.
Arrays: Checkboxes and Textareas
Chapter 2 - Introduction to C Programming
Introduction to Computer Science / Procedural – 67130
IF statements.
Assignment and Arithmetic expressions
Variables, Expressions, and IO
Other Kinds of Arrays Chapter 11
Arrays and files BIS1523 – Lecture 15.
PHP Introduction.
Chapter 2 - Introduction to C Programming
Intro to PHP & Variables
Chapter 2 Basic Computation
Cookies BIS1523 – Lecture 23.
While Loops BIS1523 – Lecture 12.
HTML Forms and User Input
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
More Selections BIS1523 – Lecture 9.
Functions BIS1523 – Lecture 17.
Chapter 2 - Introduction to C Programming
Conditions and Ifs BIS1523 – Lecture 8.
Building Web Applications
In Class Programming BIS1523 – Lecture 11.
Arithmetic Expressions & Data Conversions
T. Jumana Abu Shmais – AOU - Riyadh
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
Fundamentals of Data Representation
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Computing in COBOL: The Arithmetic Verbs and Intrinsic Functions
THE COMPUTE STATEMENT Purpose: performs mathematical calculations
Topics Designing a Program Input, Processing, and Output
Statistics for the Social Sciences
Relations And Functions © 2002 by Shawna Haider.
Topics Designing a Program Input, Processing, and Output
Introduction to Primitives
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Introducing JavaScript
In this class, we will cover:
Unit 3: Variables in Java
Arithmetic Expressions & Data Conversions
Presentation transcript:

Number and String Operations BIS1523 – Lecture 6

Mathematical Operators The following mathematical operators may be used in PHP to perform calculations: + : addition - : subtraction * : multiplication / : division **: exponentiation These may be used with numeric literals, or variables Example: 5 * 3 10 + 6 $sum / $count

Assigning to Variables Normally, we will store the results of calculations in variables, so that we can print them out later. $sum = 88 + 92 + 45; $average = $sum / 3; You can also print out mathematical expressions directly, as long as you do not use double quotes print 88 + 92 + 45; print $sum / 3;

Precedence Mathematical expressions are not necessarily performed “left to right”. For example the expression: $tempC – 32 * 5 / 9 In “left to right” order, it would do the subtraction first, then the multiplication, then the division. Programming languages, however, have a ‘precedence’ that follows a specific order of resolution for math: Exponentiation first Multiplication & Division next Addition and Subtraction last So in the above example, 32 * 5 / 9 would be done first, then subtracted from the value of $tempC

Parenthesis Rather than try and remember the precedence on longer mathematical formulas, we can use parenthesis to specify what order we want our calculations to occur Inner most parenthesis are executed first. So in our previous example, if we wanted to execute the subtraction first, we enclose it in parenthesis ($tempC – 32) * 5 / 9 We can nest parenthesis as much as needed: 3 * (5 + 2 * (15 – 3)) The result of the above is: 15 - 3 is done first, the inner most parenthesis (result is 12) Then 2 * 12 is executed, since within a set of parenthesis multiplication goes before addition Then 5 + 24 Then 3 * 29

Program Flow In our standard HTML/PHP programming design, we will normally take the following steps: Read in the inputs from HTML into variables (INPUT) Perform the calculations needed to calculate our outputs (PROCESS) Print out our results Example program with one input: The form calls “results.php”, the input is called “temp-in”

Temperature Converter Output for the previous example: The Code:

Incrementing & Decrementing Some additional mathematical operators exist to perform common tasks. Two of these are incrementing (adding 1), and decrementing (subtracting 1) from a number The shortcut operators are ++ and – To add 1 to the variable “$count”: $count++; Likewise, to subtract 1: $count--;

Arithmetic Assignments There is also a special operator used to compute sums or totals. We often want to take the value of a variable, add some value to it, and store the result in the same place. The += operator does this. $total += $someamount; The above command would take the value of total, add the value of the variable $someamount, and store the result in $total. So if for example $total was 10, and $someamount was 5, the end result would be $total would have the value 15. (The value of $someamount is unchanged) The assignments -=, *= and /= also exist, but are used less often.

Formatting Numbers We can use the number_format function to limit how many decimal places are printed for numbers. There are several ways to use this function for output (some covered later). We are going to start by using it to just round a variable off to a specified number of decimal places. number_format takes 2 parameters, the first is a variable (or number), and the second is how many decimal places to use. The above command would take the value of total, round it to 2 decimal places, then store the result back in $total (changing its value.)

Comments Sometimes it is helpful to be able to document our code so when we look at it later, we can be reminded of what, why, or how we did something. The mechanism for this internal documentation is called “Comments”, and can be done one of two ways in PHP A pair of double slashes tells PHP to ignore the rest of this line. So if I do something like: $count++; // This is where I add 1 to count. The typing past the double slashes is ignored. I can write whatever I wish there, as a note to myself later.

Multi Line Comments For comments spanning multiple lines, I can use the /* and */ opening and closing tags. /* marks the start of a comment, and */ marks the end, everything between will be ignored, even over multiple lines. Similarly I could do:

Printing much HTML with one Print The PRINT command ignores line breaks within the double quotes. This means that you can output a lot of HTML in one print command, rather than having a print command for each line. In a previous example, we had output done like this: This could also be written as:

Calculator Example Let’s step through a full program from start to finish, using all the tools we have discussed so far. We start with a list of information we need to read in from the user, and create an HTML form that allows the user to enter it.

The Input Form Enclose the entire input section in a <form>. The action attribute points to the program to run when form is submitted. Have an <input> for each thing we need to read in. Include some text to let the user know what they are typing in. Each input will also have a name attribute that will be used by our PHP to get the values Include a submit button

Results Our PHP program should print out a list of results. Some of these values are just repeating what the user typed in, but total cost, and monthly payment are calculated We will follow our normal formula for the program: Input Process Output

Input For each input the user entered, read them into a variable. It’s okay to use the same names as the inputs. Variables in PHP always start with $’s.

Processing Next we do all of our calculations, as well as applying formatting We wait and apply formatting after all of our calculations are complete, so we don’t introduce rounding errors. We also didn’t format the inputs, assuming that the user didn’t enter them in some bad format.

Output Using the print command, we format the output using HTML breaks to print it on separate lines. PHP says variables start with dollar signs and then have a letter. So if there are 2 dollar signs in a row, the first one must just be a dollar sign for printing.

Concatenation You can use the period operator to concatenate 2 strings together. You will notice that this would be the same as the command: The first example is more commonly seen in coding today, but either way works. One good use for concatenation is to format numbers as we print them out. You can’t use number_format() inside the double quotes, but you could use it as part of a concatenation

Concatenation & Formatting Example In a previous example, we used number_format() to change the value of variables before printing them out. Using concatenation we can format them as we are printing. In this case, we close the double quotes, concatenate the formatted number, then re-open the double quotes and continue with the output. By not changing the original variable, we haven’t introduced any rounding errors.

Printing with Several Prints If concatenation is to messy to read, you can still always just use multiple print commands to print with number_format.

Multiple PHP Sections PHP allows you to have any number of PHP sections, intermingled within your HTML. Each of them should be enclosed within the PHP tags <? … ?> Remember though: Inside PHP tags, only PHP commands, no HTML without it being specifically printed. Outside PHP tags, don’t use PHP commands, as they’ll just appear on the screen as content. When we have a lot of HTML, rather than print it, we can use multiple PHP sections to reduce the amount of stuff we need to put inside the “print” commands.

Example For example if we were printing something like this: We could, instead, just have most of the HTML outside of the PHP tags, and just use a small section of PHP to print the variable $quantity. Everywhere there is a variable, we just start PHP, print it, and end PHP. Remember, white space doesn’t matter. So the PHP tags can be on lines by themselves, or not…..your goal is to make it as readable as possible.

Example Continued Using the same process, a larger section would go from: To: Notice: when printing just a variable, double quotes aren’t needed.

Example Continued In the full program, notice we have HTML at the top, then we begin our input & process PHP. Then before the output, we close the PHP tags, and fill in the HTML we want for output, opening groups of PHP tags for each variable we want to print.