Programming Basics
To learn a computer programming language it is necessary to understand the
concepts of sequence, looping, and conditional branching.
Sequence - Doing things in a defined order
Here is a simple script to illustrate sequence. Copy it into the BODY portion of a
simple HTML document, call it "sequence.html" and then open it in a modern
Web browser. To view the completed page,
click here
.
<script language="javascript" type="text/javascript">
// <![CDATA[
document.write("This line of text is the result of the first command in the
sequence.<br />");
document.write("This line of text is the result of the second command in the
sequence.<br />");
document.write("This line of text is the result of the third command in the
sequence.<br />");
// ]]>
</script>
Looping - Doing things over and over again
Here is a simple script to illustrate looping. Copy it into the BODY portion of a
simple HTML document, call it "looping.html" and then open it in a modern
Web browser. To view the completed page,
click here
.
<script language="javascript" type="text/javascript">
// <![CDATA[
for (i=10;i>0;i--){
document.write(i + " bottles of beer on the wall.<br />");
}
// ]]>
</script>
Conditional Branching - Doing things only if certain conditions are true.
Here is a simple script to illustrate conditional branching or decision
making. Copy it into the BODY portion of a simple HTML document, call it "conditionals.html"
and then open it in a modern Web browser. To view the completed page,
click here
.
<script language="javascript" type="text/javascript">
// <![CDATA[
gender = window.prompt("Are you male or female?","");
if (gender == "female"){
document.write("Greetings Madame.<br />");
}
if (gender == "male"){
document.write("Greetings Sir.<br />");
}
if (gender != "male" && gender != "female"){
document.write("Please don't confuse me.<br />");
}
// ]]>
</script>