"Cyber Space" consists of digital information, physical machines and programs ... and people, depending on how you want to look at it. Of the non-human elements, programs are the most important to understand in order to get a handle on cyber security/warfare. This doesn't mean you need to be a programmer (though to be a true technical practitioner of cyber security you do), but you need some understanding of what programs are and the basics of how they work. In this lesson, that's what we'll start to do.

What are programs? What are programming languages? What is Javascript?

The word program means lots of things in the context of computing. In previous classes, we described a program as a file that contains instructions for the computer in its native language of 1's and 0's. We might call that a Program with a capital "P" to clarify. Almost nobody writes this kind of program. Instead, we write programs in a programming language, which is a set of commands that can be automatically turned in to a Program (capital "P"). Programming languages should be easier for humans to use than the physical machine's native language of 0's and 1's.

Javascript is a programming language that's primarily used in web pages. This aspect of the language is something we'll look more closely at later. You'll hear Javascript referred to as a "scripting language" and its programs called "scripts". A Program (capital "P") is said to "run on the machine". Javascript programs are not meant to run on a physical machine, but instead to "run on the browser", which itself is a Program running on the physical machine. This kind of "program running within a program", which is actually extremely common, is often referred to as a script.

Expressions: Type, Value and Basic Operations

A calculator is interactive. You enter a mathematical expression, press =, and you get the value of the expression. Most programs don't work that way. The commands that make up the program are all written ahead of time in some file, and then the program executes all at once. Some kinds of programming languages must work that way. They are called compiled languages. Other languages are interpreted, which means that they can be evaluated interactively one command at a time, although that's still not the way it's usually done. Javascript is interpreted, and we have an interpreter below that allows you to enter Javascript a single expression or statement at a time, just like a calculator. Using the language interactively like this is a great way to learn.
Numbers, Expressions, Variables If you enter an expression in the input box to the right, the interpreter will evaluate the expression and write its value in the box below.
  • Numbers: The simplest expression is a number. It evaluates to itself. Try this: input 58 and press enter. Not exciting, but it shows that 58 evaluates to 58. There are lots of arithmetic operations available, so you can enter complex mathematical expressions and the interpreter will evaluate them for you.
    basic arithmetic: +, -, *, / (the usual order of operations apply!) [note: % is the "remainder" operator]
    math functions:, Math.sin( ), Math.cos( ), Math.log( ), Math.exp( ), Math.sqrt( ), Math.floor( ), etc.
    math constants: Math.PI, Math.E, etc.
    Check out this complete reference of Math functions and constants.
    Question: What is the value of the expression
    which, by the way, computes the side length for a square whose area is the same as a circle of radius 3.
  • Variables: Javascript lets you define variables, which are simply names that point to values. Once a name is declared as a variable, that name evaluates to the value it points to.
    declare name x as a variable: var x;
    assign name x a value: x = Math.sqrt(17);
    evaluate name x to get its value: x
    Usually, we declare and assign simultaneously, e.g. var x = Math.sqrt(17);.
    Enter the following statements in turn.
    var r = 0.25;
    var cost = 57.99;
    cost = cost*(1 - r);
    cost = cost*(1 - r);
    You've just calculated how much a $57.99 item would cost with 25% off of a price already reduced by 25%.
    Note: = is assignment, not equality, which is why x = x + 1; makes sense.
  • What's with the ';'? The ; is used to terminate statements. Statements are actions for the interpreter to perform. The action may be nothing more than evaluating an expression, but sometimes, like when we declare a variable with var, there's no expression — just an action to perform. Javascript is pretty flexible about whether or not you use ;'s, but better to just terminate every command with one. BTW: you can put several statements on one line, e.g.
    var r = 0.25; var cost = 57.99; cost = cost*(1 - r);
  • Guess what — there are only seconds until 2016's graduation! Use the Javascript interpreter to figure the time to graduation as the number of weeks, days, hours, minutes, seconds. Note: the % operator, which gives remainder, is really useful! For example, to start things off, if there are T seconds until graduation, then calculating s = T % 60; h = (T - s)/60;, there are h hours and s seconds.
Input a Javascript expression or statement and press enter


Javascript has more types of values than just Number. Another value type in Javascript is String. Strings are used to represent sequences of characters. A string literal, analogous to a number literal like 76, is anything starting or ending with the " "s or ' 's. So, "hello world!" and 'hello world!' are both string literals. I cannot overestimate the importance in this course, in cyber security and in life of strings! Here are some things to know about strings in Javascript.
Escape Artist

The Best Jobs Need Programming
Five of the top 10 jobs in 2012 U.S. News & World Report's "25 Best Jobs" list require programming skilz!

Types, typeof, and type conversions

We now know two types that a value can have, Number and String. There are other types (Boolean, Function and Object), though they'll be less relevant to us given the small amount we do with Javascript. Javascript has a special operator, typeof, that can be used to determine the type of a value. For example, typeof(7)Number, and typeof("foo")String. We can call typeof for any expression. You might ask what kind of value typeof returns. Here's how to find out: enter typeof(typeof(4)) into the interpreter. So now you know, typeof returns a String that gives the name of the type.

Here's some interesting ones to try:

What's going on here? Javascript automatically converts numbers to strings and strings to numbers when it deems it to be necessary. If an arithmetic expression involves strings and any operation other than +, Javascript will convert the string to a number. Because the + operation means something special to strings, Javascript won't convert strings to numbers in that case. In fact, if you mix strings and numbers in a + expression, the numbers get converted to strings, which the last of the above examples demonstrates. This kind of type conversion that happens automatically behind the scenes is called an implicit conversion. There are explicit conversions too. If you wrap a string in Number( ) the interpreter attempts to convert the string to a number. For example, try 3 + Number("7") and compare what you get to 3 + "7". Similarly, if you wrap a number in String( ) the interpreter attempts to convert the number to string. For example, try typeof(String(365)).

To say a word about the three other types:

Programs Part 2

Programming languages in the DoD

1974: DoD is using 450 different programming languages, spending $3 billion/year on software maintenance ($14 billion in 2012 dollars). A major part of "software maintenance" is fixing BUGS!. A search begins for an existing language most suited to DoD requirements ...

1977: No existing programming language is suitable. DoD solicits proposals for a language appropriate for embedded computer applications (i.e., command and control, communications, avionics, shipboard, test equipment, software development and maintenance, and support applications). ["Steelman"]

1980: DoD completes specification of the "ADA" language and subsequently mandates its use. Here's ADA code essentially equivalent to     prompt( "Hello, World!" );

  with Text_IO; use Text_IO;
  procedure Hello is
    Put_Line("Hello, World!");
  end Hello;

1996: DoD now using only 37 different programming languages. Much new code is being written in ADA, which is easier (thus less costly) to maintain: ADA was designed to be less prone to the kind of bugs that make software vulnerable to attack.

Today: ADA code is executing on US Navy systems such as the Aegis Weapon System, SSN-21 AN/BSY-2 Submarine Combat Control System, V-22 Osprey, AGM-114 Hellfire missile, CH46 Cockpit Control System, Tomahawk missiles, MK 41 Vertical Launch System.

Programs and alert( ) and prompt( )

A program is a sequence of statements. The statements get executed one after another in a single batch by the interpreter. Since the user isn't working with the interpreter, in fact the user no longer even knows there is an interpreter, we need another mechanism to display answers. The function alert( ) pops up a window with a message given by whatever string you put between the ( )s. Try entering alert("Hello World!"); in our Javascript interpreter. Because this doesn't rely on having some kind of interpreter window, alert( ) is a good way to communicate information to the user in a program. To get values from the user, Javascript has a function called prompt( ). You give it a message indicating to the user what information is supposed to be provided, and prompt( ) pops up a window with the message and an input line. Whatever string gets entered on that line gets returned as the value of the prompt( ) function.

Once you start entering several lines of statements, you often want to write little notes to yourself or whomever else will be looking at your code. Notes in programs are called comments. In Javascript, anything written after a // until the next newline character is ignored by the interpreter, and so we use // to write comments.

Below is a simple program that gets the users height and weight and returns to the user what his weight would be if he were 25 feet tall. Play with it, understand it, and see if you can modify it so that it asks the user for a target height (in feet) rather than always using 25.


Fast food restaurant calculator — variables changing over time

A common feature of many programs is that they have a relatively small set of variables, but these variables change over time, i.e. they continually get new values assigned to them. The following program illustrates this. The program is a "fast food restaurant calculator". The user inputs the number of burgers they want, fries they want and drinks they want, and the program prints out the total cost. The way the program is written, the variable total keeps a running total of the cost, which gets updated as the user orders a certain number of burgers, then a certain number of fries, then a certain number of drinks.
An interesting twist is to ask for the program to repeat the user's order before giving the total. We can accomplish this by keeping a running list of what gets ordered. The variable total is a number, and is initally zero to indicate that, at the beginning, the user hasn't yet ordered anything. To keep a running list of what's been ordered, we need a variable (we'll call it order) of type string. Initially, since nothing has been ordered, that string will be the empty string, i.e. "".
In this second version of the program, all three variables — total, num and order — are constantly being assigned new values.


Here are some example problems to try and/or two look at solutions for.
  1. Write a program to do Fahrenheit to Celsius temperature conversion. solution
  2. Write a program to read the x and y coordinates of two points and compute the slope of the line through them. solution
  3. Write a program to read in a dollar amount and annual interest rate and print out how much money you'd have at that rate starting with that amount with after 10 years of annually compounded interest. solution, alternate solution.
  4. Write a program to solve the following problem:
    Consider a horizontal segment AC of length x feet and a point B above it, where the angle CAB is a degrees and ACB is b degrees. Taking x,a,b as input, compute and display the height y (in feet) of point B above segment AC.



You are expected to be able to understand simple programs, and modify simple programs as a means of demonstrating that understanding, but we are not going to give you an empty box and ask you to write a program from scratch to solve a problem. That's simply beyond the scope of this course. If you are interested in pursuing something like that, consider taking the intro CS/IT course IC210.

Well 2