CMPS 002 - Computer Literacy

Prof. Paulo Franca

CyberSlug Rules!

Classes

Class Syllabus

Grading Policy

Labs

Interact

Login

  • enter system
  • get attendance
  • submit work
  • take lab quiz
  • check scores

Help

Help me!

Links

Staff only

 

 

 

Datacare   Ahshay

 

Computer Programming

  • What is it?
  • Why do we?
  • How do we?

Javascript

  • No need to compile, it is interpreted one statement at a time
  • Can be used in web pages, the browser understands javascript
    • Javascript runs in the client computer, not in the server
    • Javascript is platform-independent because it is run by the browser in each different platform
  • Insert Javascript code in HTML code
    • Start with <script language="javascript>
    • end with </script>
  • Javascript is NOT Java
  • More notes on www.franca.com/javascript

Useful Tools

  • Mozilla Firefox is more helpful when you are programming. Use the Firefox browser and open the "Tools->Javascript Console" this will help you find errors in your programs.
  • Vim is a generic syntax editor that has syntax highlighting - very useful to write programs. You can download it for free from www.vim.org

Elements of a program

Comments

Comments are ignored by the computer but are extremely useful to you and other humans when trying to understand the code.

Just use two slashes to indicate a comment. Anything until the end of the line will be ignored as a comment.

Example:

      // anything can be written here

Variables

names that can be used to represent quantities or sentences

must start with letter or underscore, may contain numbers

Expressions and assignments

similar to math, with few differences

= means "make this equal to" or "becomes"

Displaying results and writing on the screen

document.write("whatever");

whatever you write is HTML code that will be given to the browser!

this can be used to display text, links, images, etc.

window.alert("Hey there");

  • opens a pop-up window with your message
  • window closes when you click OK

Getting values typed by the user:

value=window.prompt("Please input your value","default value");

  • A pop-up window with your message is displayed.
  • The user types something and clicks OK.
  • Whatever the user types is the result of the prompt and, in this case is copied to the variable "value".

if you want to use the value as a number, you must convert to an integer like this:

newvalue=parseInt(value);

Program flow

  • Computer executes statements sequentially, one after the other
  • You may request the program to execute a procedure (or function)
    • result=lesser(a,b);
  • You may decide to execute one part of a program or another:
if(value<100)
{
  // do this
}
else
{
  // do that
}
  • Use the braces { } to delimit what you want done in case: 
  • the comparison is true ( value is indeed less than 100)
  • the comparison is NOT true (value is NOT less than 100)
  • You may repeat a given part of a program:
while( result<0)
{
   // repeat this as long as the above is true
}

Again, use the { } to delimit the part that you want to repeat.