"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.
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.
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,
var s = "the"; s;
can'tis easy with " "s, but not with ' 's. Try entering
'can't'in the interpreter and see what the problem is. Conversely, a string like
the "it" is not importantis easy to write with ' 's but not with " "s. What about the string:
I didn't say "hello" to you!How could I set a variable equal to such a string? Inside a " "-delimited string, the " character has a special meaning, putting a \ character in front of it "escapes" that special meaning. Same thing works for a ' '-delimited string. Thus either of these two statements
var s1 = "I didn't say \"hello\" to you!"; var s2 = 'I didn\'t say "hello" to you!';are OK. Of course this begs the question" "How do I write a \ character?" The answer is, escape it!
var s3 = "You can put \\ in a string!";I recommend putting all of those in the interpreter and seeing what values the variables get.
"man" + "age"evaluates to
"manage". The + operator works this way regardless of whether we have literal strings or variables that refer to strings. So, for example, if
var x = "man"and
var y = "age", then
x + yevaluates to
"manage". Don't forget: with strings,
+means concatenation, not addition.
]. So, if
var x = "hard", the expression
h, the expressions
.lengthat the end of a string to create an expression that evaluates to a string's length. So, for example,
"hard".charCodeAt(0)yields 104, because 104 is the ASCII value of the character h.
)Example: String.fromCharCode(104); yields "h"
typeof, and type conversions
String. There are other types (
typeof, that can be used to determine the type of a value. For example,
String. We can call
typeoffor any expression. You might ask what kind of value
typeofreturns. Here's how to find out: enter
typeof(typeof(4))into the interpreter. So now you know,
Stringthat gives the name of the type.
Here's some interesting ones to try:
+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
To say a word about the three other types:
Math.sqrt( )are examples of functions. Programmers can define their own functions as well as use the extensive library of built-in functions like the Math functions we've already seen.
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
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.
alert( )pops up a window with a message given by whatever string you put between the ( )s. Try entering
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
//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.
totalkeeps 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
totalis 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 —
order— are constantly being assigned new values.