Programming Basics

Programming Basics

Readings and References • Reading » Fluency with Information Technology Programming Basics • Chapter 18, Fundamental Concepts Expressed in JavaScript • Other References INFO/CSE 100, Autumn 2004 » Games and Puzzles Fluency in Information Technology • Thomas Jefferson National Accelerator Facility, Office of Science Education http://www.cs.washington.edu/100 • http://education.jlab.org/indexpages/elementgames.html 25-Oct-2004 cse100-11-programming © 2004 University of Washington 1 25-Oct-2004 cse100-11-programming © 2004 University of Washington 2 The Plan Begin with HTML • We will learn JavaScript over the next few Basic HTML is static lectures the contents of the file are displayed as given • JavaScript is used with HTML in Web pages <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" • JavaScript is a contemporary programming language -- "http://www.w3.org/TR/html4/loose.dtd"> we will learn only its basics <html> <head> • You will program in a text editor and run your program <title>Simple A</title> </head> with your browser <body> What is 2.0 + 2.0? </body> </html> JavaScript is a way to make HTML “dynamic” 25-Oct-2004 cse100-11-programming © 2004 University of Washington 3 25-Oct-2004 cse100-11-programming © 2004 University of Washington 4 Add some “dynamic” content JavaScript in an HTML page Scripting languages let us create active pages » implement actions to be taken at run-time when the page is <script> block loaded or in response to user event in <head> <head> <title>Simple B</title> Language we are javascript <head> <script type="text/javascript"> using is <title>Simple B</title> var greeting = "Hello World!"; <script type="text/javascript"> </script> var greeting = "Hello World!"; </head> </script> </head> <script> block <body> in <body> <script type="text/javascript"> Generate HTML <body> document.write(greeting); “on the fly” with <script type="text/javascript"> </script> document.write(...) document.write(greeting); What is 2.0 + 2.0? </script> </body> What is 2.0 + 2.0? </body> 25-Oct-2004 cse100-11-programming © 2004 University of Washington 5 25-Oct-2004 cse100-11-programming © 2004 University of Washington 6 Browser interprets your page Browser interprets your page • You are telling the browser what to do • You are telling the browser what to do » using HTML for the static parts of the page » using HTML for the static parts of the page » using JavaScript for the more dynamic parts <head> Here is some script initialization information. <title>Simple B</title> This page is written in the HTML language. <html> <script type="text/javascript"> <head> var greeting = "Hello World!"; <title>Simple A</title> </script> </head> Here is some header information about the page. </head> <body> What is 2.0 + 2.0? Here is some script for the body of the page. <body> Here is the main body of the page. </body> <script type="text/javascript"> </html> document.write(greeting); </script> What is 2.0 + 2.0? </body> 25-Oct-2004 cse100-11-programming © 2004 University of Washington 7 25-Oct-2004 cse100-11-programming © 2004 University of Washington 8 Variables In Real Life • A variable is a "container" for information you want to store » The name of the variable stays the same, but the value associated with that name can change That’s why it’s called a “variable”! Variable Name Current Value Previous Value #1 Single My Boo, Usher And Alicia Keys Goodies, Ciara AL Champion Boston Red Sox New York Yankees #1 Box Office Shark Tale Shark Tale Day of the Week Monday Sunday Husky Card Balance $52 $60 25-Oct-2004 cse100-11-programming © 2004 University of Washington 10 Variables In Programming JavaScript Variables • Program variables have names and values » Names (also called identifiers) <html> • generally start with a letter and can contain letters, numbers, and <head> underscore characters “_” <title>Simple C</title> <script type="text/javascript"> • Names are case sensitive var greeting = "Hello World!"; » Values var balance = 52; var transaction = 12; • can be numbers, strings, boolean, etc </script> • change as the program executes </head> Variable Name Current Value Previous Value <body> <script type="text/javascript"> n1_single "My Boo" "Goodies" document.writeln("<p>"+greeting+"<\/p>"); document.writeln("<p>My current Husky Card balance is $"+balance+".<\/p>"); championAL "Boston Red Sox" "New York Yankees" document.writeln("<p>The next transaction will be for $"+transaction+".<\/p>"); n1_box_office "Shark Tale" "Shark Tale" document.writeln("<p>What will the new balance be?<\/p>"); </script> dayOfTheWeek "Monday" "Sunday" </body> balance 52 60 25-Oct-2004 cse100-11-programming © 2004 University of Washington 11 25-Oct-2004 cse100-11-programming © 2004 University of Washington 12 Assign a value to a variable Expressions The universal form of the assignment statement • The right-hand side of an assignment statement » variable gets value can be any valid expression greeting gets the value “Hello World!” balance gets the value 52 • Expressions are “formulas” saying how to Each language expresses “gets” in a particular way manipulate existing values to compute new values » JavaScript uses the single equals sign = greeting = "Hello World!"; balance = balance - transaction; balance = 52; seconds = 60*minutes; message = "Status code is " + codeValue; NOTE: The equals sign = is used differently in math and programming. 25-Oct-2004 cse100-11-programming © 2004 University of Washington 13 25-Oct-2004 cse100-11-programming © 2004 University of Washington 14 Operators JavaScript Expressions Use operators to build expressions » Numeric operators <html> <head> + - * / mean add, subtract, multiply, divide <title>Simple D</title> <script type="text/javascript"> » String operator var balance = 52; var transaction = 12; + means concatenate strings </script> </head> » Relational operators <body> <script type="text/javascript"> < <= == != >= > mean less than, less than or equal to, document.writeln("<p>My current Husky Card balance is $"+balance+".<\/p>"); equal to, not equal to, greater than or equal to, greater than document.writeln("<p>The next transaction will be for $"+transaction+".<\/p>"); balance = balance - transaction; document.writeln("<p>The new balance will be $"+balance+".<\/p>"); » Boolean operators </script> </body> && || ! mean and, or, not </html> 25-Oct-2004 cse100-11-programming © 2004 University of Washington 15 25-Oct-2004 cse100-11-programming © 2004 University of Washington 16 Practice, practice, practice http://www.w3schools.com/js/js_examples.asp • Write a simple web page with a simple script like the ones here • Save it to disk • Open the web page with your browser • Does it look like what you expected? » Edit, save, reload » Edit, save, reload »... 25-Oct-2004 cse100-11-programming © 2004 University of Washington 17 25-Oct-2004 cse100-11-programming © 2004 University of Washington 18.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    5 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us