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 head portion of a simple HTML document, call it "sequence.html" and then open it in a Netscape web browser. To view the completed page, click here.

<SCRIPT language="JavaScript">
	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 head portion of a simple HTML document, call it "looping.html" and then open it in a Netscape web browser. To view the completed page, click here.

<SCRIPT language="JavaScript">
	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 intothe head portion of a simple HTML document, call it "conditionals.html" and then open it in a Netscape web browser. To view the completed page, click here.

<SCRIPT language="JavaScript">
	gender = window.prompt("Are you male or female?","");	
	if (gender == "female"){
		document.write("Greetings Madame.<BR>");
	}
	else if (gender == "male"){
		document.write("Greetings Sir.<BR>");
	}
	else {
		document.write("Please don't confuse me.<BR>");
	}
</SCRIPT>