Computer Science 161.01

Professor Valente

 

October 14, 2019

 

Finally, a High Level Language

 

We have suffered the pains of low-level languages long enough.  We understand that throughout the last half century, high-level programming languages such as FORTRAN, Basic, Pascal, and C++ have been invented.  They allow us to think more easily about the algorithms and programs we need to solve complex problems, because we operate at a level more comfortable to humans than to machines.

 

Our experience will be with a modern language known as JavaScript.  It is a web-based language, so its strength is in adding interactions to a web page.  For example, I used JavaScript for many of the interactive web pages I developed for this course, including the VSC-32 pages, and those various “calculator” type pages for binary/hex/decimal conversions.

 

JavaScript is string-oriented

 

Not surprisingly, JavaScript is quite adept at handling strings.  That’s because whenever we enter information in a text box, we are able to use any of the keyboard symbols.  In particular, forms you fill out on the web allow you to enter name, address, city name, credit card number, etc, all of which are read by JavaScript as strings.

 

Recall that strings are collections of symbols.

Your name “Seymour” and your social security number “123-45-6789” are regarded as strings.

 

Later in the course, I hope you’ll get to experiment with many of the string capabilities offered by this language.

We will use these capabilities for some elementary cryptography.

 

 

Expressions


An important part of programming in a high-level language is to be able to write expressions in a manner similar to how we did in high school algebra.

So now, we will be able to write  x + y - 5  instead of LOAD x, ADD y, SUBTRACT five

(or worse yet, 00101001, 10101010, 11001000… ouch).  

 

The x + y - 5  is called an expression because it represents some value.

For example if x has 20 and y has 30, the expression represents the value 45.

 

In our studies, we will focus on both integer expressions and boolean expressions.

Integer expressions evaluate to an integer (like 45), whereas boolean expressions are like the logical sentences we studied a few weeks ago: they evaluate to true or to false.

 

Boolean expressions help a program decide what it needs to do.

For example, if the program reads an integer that is supposed to represent a test grade,

it can check to see whether the user entered an integer in the range 0 to 100.

(The integer must be greater than or equal to 0 AND it must be less than or equal to 100).

 

Integer Expressions

 

Integer expressions are composed of integer constants (like 5) and variables (like x and y).

These are combined with different operators: for example, + (for plus) , - (for minus),

* (for times), and an interesting one, % (for mod). 

 

Let’s mess around with some.  Enter each of the expressions below separately in the JavaScript scratchpad site

and click the Evaluate button.  See if the answer at the bottom is what you would have predicted.  

 Be sure to delete each expression before entering a new one.

 

20 * 5 + 3

20 * (5 + 3)

 

10 - 5 – 1

10 - (5 – 1)

 

73 % 10

73 %  9

 

10 * 6 % 4

10 * (6 % 4)

 

10 - 4 % 3

(10 - 4) % 3

 

 

Order of Operations

 

Did you notice the order in which operations are undertaken by JavaScript?

 

1) Always do what’s in parentheses first. 

Thus 20 * (5 + 3) is done as 20 * 8 which is 160.

 

2) In the absence of parentheses, * and % are done before + and -.

We’ll say that the * and % have higher precedence than  + and -.

Thus 20 * 5 + 3 is done as 100 + 3 which is 103.

 

3) If all operators in an expression have equal precedence, the order of

evaluation is left-to-right.

Thus 10 – 5 – 1 is done as (10 – 5) - 1 which is 4.

 

 

The mod Operator:  %

 

I’m certain that the strangest of the operators for you is the mod operator.

We saw, for example, that 73 % 10 is 3 and that 73 % 9 is 1.

Why is that?

 

Think about this. Imagine I owe you 73 cents but all I have are dimes.

I can give you 7 dimes (7 X 10) and owe you 3 cents, which I’ll pay later, I promise.

That is, 73 = 7 X 10 + 3, which says 73 is 7 X 10 plus a remainder of 3.

We also say then that “73 mod 10 is 3”.  In JavaScript, we say 73 % 10 is 3.

 

What if I had only 9-cent coins?  Then 73 = 8 X 9 + 1, so  73 % 9 is 1.

 

Now see if the other expressions from above make sense!

 

AND now on to Boolean Expressions OR NOT