Since the invention by Von Neumann of the stored program computer in 1948, electrical engineers recognized that a tremendous potential of computing equipment was the ability to alter its behavior, depending on the input data. Calculating machines had, for some time, been able to perform fixed arithmetic operations on data, but the potential of machines capable of making decisions opened up many new possibililities. Machines that could make decisions were capable of sorting records, tabulating and summarizing data, searching for information, and many more advanced operations that could not even be imagined at the time.
In early programming languages, like Fortran (first invented in 1954) and various low level machine languages, the goto statement allowed the computer to deviate from the sequential execution of the program instructions. The goto statement was recognized to be a very powerful construction, and soon, programs of increasing complexity and power were developed.
However, the increasing complex code became harder and harder to maintain. Edsger Dijkstra, in 1966, was one of the first persons to recognize that this run away complexity of programs was due to the overuse of the goto statement (Dijkstra, E. W., "Go To Considered Harmful," Communications of the ACM, March 1966). In fact, it was determined shortly thereafter, that the goto statement is not needed at all. This was the birth of the discipline of Structured Programming, which uses three constructions: sequence, repetition, and decision.
JavaScript does not have a goto statement.
Add flour Add salt Add yeast Mix Add water Knead Let rise Bake
stack dishes by sink fill sink with hot soapy water while there are more dishes get dish from counter wash dish put dish in drain rack end wipe off counter rinse out sink
compute courseScore if courseScore is greater than or equal to 90 assign course grade to A else if courseScore is greater than or equal to 80 assign course grade to B else if courseScore is greater than or equal to 70 assign course grade to C else if courseScore is greater than or equal to 60 assign course grade to D else assign course grade to F end submit course grade to registrar
get mail from mailbox put mail on table while more mail to sort get piece of mail from table if piece is personal read it else if piece is magazine put in magazine rack else if piece is bill pay it else if piece is junk mail throw it in wastebasket end end
Structured programming is a program written with only the three constructions sequence, decision (if..else if statements), repetition (while or for statements). Important: (1) the condition of a JavaScript if, else if, while, or for statement is placed in parentheses, (2) the body of a JavaScript if, else if, while, or for statement is enclosed in braces and indented four spaces.
// Sequence var x = 56; var y = 11; var z = x + y; document.writeln(z)
if (condition) { action; }Example:
// Decision Example if (x % 2 == 0) { document.writeln("The number is even."); }
while (condition) { action; }Example:
// Repetition Example var n = 1; while (n < 100) { document.writeln(n + "<br>"); n = 2 * n; }
To make programs easier to read, some additional constructs were added to the basic three original structured programming constructs:
for (initialization; condition; iteration) { action; }Example:
// Definite Repetition Example for (var n = 1; n <= 30; n++) { document.writeln(n + " "); }This form of the for statement is used by several languages, including C, C++, Java, C#, and of course JavaScript. There are four parts to this for statement:
if (condition1) { action1; } else if (condition2) { action2; } else if (condition3) { action3; } else { defaultAction; }Example:
// ElseIf Example var n = prompt("Enter a positive integer: ", "0") if (n == 1) { document.writeln("one"); } else if (n == 2) { document.writeln("two"); } else if (n == 3) { document.writeln("three"); } else if (n > 3) { document.writeln("many"); } else if (isNaN(n)) { document.writeln("Input is not a number"); } else if (n == null || input == undefined) { document.writeln("Input canceled"); }
// Switch Example var n = parseInt( prompt("Enter a positive integer: ", "")); switch(n) { case 1: document.writeln("one"); break; case 2: document.writeln("two"); break; case 3: document.writeln("three"); break; default: document.writeln("other"); break; }The switch statements are more compact than the corresponding if..else statements. However, the switch statements are more limited in the comparisons that they can perform.