IS2802 Introduction to Multimedia Applications for Business Lecture 4: JavaScript, Loops, and Conditional Statements Rob Gleasure

Slides:



Advertisements
Similar presentations
Java Script Session1 INTRODUCTION.
Advertisements

CHAPTER 5: LOOP STRUCTURES Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
14/11/11.  These words have special meanings in themselves  These should NOT be used as Identifiers.  For example:  Break, do, function, case, else,
Executes a statement or statements for a number of times – iteration. Syntax for(initialize; test; increment) { // statements to be executed } Initial.
ITC 240: Web Application Programming
JavaScript 101 Lesson 01: Writing Your First JavaScript.
Working with JavaScript. 2 Objectives Introducing JavaScript Inserting JavaScript into a Web Page File Writing Output to the Web Page Working with Variables.
Tutorial 12 Working with Arrays, Loops, and Conditional Statements
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Loops – While, Do, For Repetition Statements Introduction to Arrays
XP 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties Tutorial 10.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
JavaScript Programming an Introduction Prepared By P.D. Krolak and M.S. Krolak Based on material from JavaScript Unleashed (2nd Ed)
Introduction to JavaScript for Python Programmers
Programming Concepts MIT - AITI. Variables l A variable is a name associated with a piece of data l Variables allow you to store and manipulate data in.
Python quick start guide
Arrays and Control Structures CST 200 – JavaScript 2 –
Using Object-Oriented JavaScript CST 200- JavaScript 4 –
Loops Doing the same thing over and over and over and over and over and over…
JavaScript Defined DOM (Document Object Model) General Syntax Body vs. Head Variables Math & Logic Selection Functions & Events Loops Animation Getting.
SYST Web Technologies SYST Web Technologies Lesson 6 – Intro to JavaScript.
WEEK 3 AND 4 USING CLIENT-SIDE SCRIPTS TO ENHANCE WEB APPLICATIONS.
CISC474 - JavaScript 03/02/2011. Some Background… Great JavaScript Guides: –
Arrays – What is it? – Creation – Changing the contents Functions – What is it? – Syntax – How they work – Anonymous functions A quick lesson in Objects.
Week 9 PHP Cookies and Session Introduction to JavaScript.
CITS1231 Web Technologies JavaScript and Document Object Model.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
XP Tutorial 10New Perspectives on Creating Web Pages with HTML, XHTML, and XML 1 Working with JavaScript Creating a Programmable Web Page for North Pole.
Using Client-Side Scripts to Enhance Web Applications 1.
20-753: Fundamentals of Web Programming 1 Lecture 12: Javascript I Fundamentals of Web Programming Lecture 12: Introduction to Javascript.
Introduction to PHP A user navigates in her browser to a page that ends with a.php extension The request is sent to a web server, which directs the request.
Roles of Variables with Examples in Python ® © 2014 Project Lead The Way, Inc.Computer Science and Software Engineering.
JavaScript Syntax and Semantics. Slide 2 Lecture Overview Core JavaScript Syntax (I will not review every nuance of the language)
ALBERT WAVERING BOBBY SENG. Week 4: JavaScript  Quiz  Announcements/questions.
L OO P S While writing a program, there may be a situation when you need to perform some action over and over again. In such situation you would need.
XP Tutorial 10New Perspectives on HTML and XHTML, Comprehensive 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties Tutorial.
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 10 - JavaScript/JScript: Control Structures II Outline 10.1Introduction 10.2Essentials of.
Loops Robin Burke IT 130. Outline Announcement: Homework #6 Conditionals (review) Iteration while loop while with counter for loops.
IS2802 Introduction to Multimedia Applications for Business Lecture 3: JavaScript and Functions Rob Gleasure
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
JavaScript Defined DOM (Document Object Model) General Syntax Body vs. Head Variables Math & Logic Selection Functions & Events Loops Animation Getting.
Introduction to JavaScript CSc 2320 Fall 2014 Disclaimer: All words, pictures are adopted from “Simple JavaScript”by Kevin Yank and Cameron Adams and also.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
CS 106 Introduction to Computer Science I 03 / 22 / 2010 Instructor: Michael Eckmann.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
Dr. Abdullah Almutairi Spring PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages. PHP is a widely-used,
JavaScript, Sixth Edition
Introduction to Javascript. What is javascript?  The most popular web scripting language in the world  Used to produce rich thin client web applications.
JavaScript 101 Lesson 6: Introduction to Functions.
1 Objects In JavaScript. 2 Types of Object in JavaScript Built-in objects User Defined Objects Browser Object Document Object Model.
For Loops & Arrays. my_rect_pink_mc.width = 40 my_rect_pink_mc.alpha =.5 trace my_rect_pink_mc.x= 20 my_rect_pink_mc.y= 20 for (var i:int = 0; i < 250;
PHP Tutorial. What is PHP PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages.
Learning Javascript From Mr Saem
XP Tutorial 10New Perspectives on HTML, XHTML, and DHTML, Comprehensive 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties.
1 Looping Chapter 6 2 Getting Looped in C++ Using flags to control a while statement Trapping for valid input Ending a loop with End Of File condition.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
Programming Web Pages with JavaScript
Tutorial 12 Working with Arrays, Loops, and Conditional Statements
Loop Structures.
JavaScript Syntax and Semantics
Chapter 19 JavaScript.
Control Structures (Structured Programming) for controlling the procedural aspects of programming CS1110 – Kaminski.
T. Jumana Abu Shmais – AOU - Riyadh
Control Structures (Structured Programming) for controlling the procedural aspects of programming CS1110 – Kaminski.
Topic: Loops Loops Idea While Loop Introduction to ranges For Loop
JavaScript 101 Lesson 8: Loops.
Presentation transcript:

IS2802 Introduction to Multimedia Applications for Business Lecture 4: JavaScript, Loops, and Conditional Statements Rob Gleasure

Lecture Outline Comments Conditional statements Loops Objects Arrays

Comments As with HTML and CSS, comments are important to ensure the code you write is easy to understand for other programmers (or yourself, if you are writing large volumes code or complicated programs) It is also useful for deactivating chunks of code during debugging

Comments There are two ways of writing comments in JavaScript Function1 (){ statement1; // this is a one line comment statement2; /* this is a multi-line comment */ }

Conditional Statements These are used in conjunction with some comparison operator to determine whether a piece of code is executed The syntax of these statements will look like: condition_statement_type (condition){ statement; } The most common of these statements are ‘if’ statements

If Statements The basic syntax of an ‘if’ statement will look like: if (condition){ // statements in here } This may also be expanded with the addition of ‘else’ conditions: if (condition){ // statements in here } else if (condition){ // statements in here } else { // statements in here }

Loops Loops are powerful and extremely important things in programming. They are used in control of program flow, searching, iteration through data-sets and files, amongst many other things We will cover two kinds of loops 1. While loops 2. For loops Loops will contain 2 key components  a conditional statement or ‘stopping condition’  a number of statements to be executed

While Loops While loops are condition-tested loops While loops take the following basic format while(condition returns true){ // code }  the piece of code will run over and over again until the condition returns false Note: the condition will only be checked after each iteration of the code – it is not continuously checking while running the code inside

While Loops Often (but not always) at least one of the statements will alter a variable involved in the stopping condition For example: var x = 0; while(x < 10){ document.write(x) x++; }

A Warning About While Loops An extremely common reason why programs crash is because an infinite loop occurs. This is when it becomes impossible for the stopping condition to occur For example var x = 0; while (x < 10){ y = y+x; document.write(x); } x++;

For Loops For Loops are counted loops. They are used when you need to do a set of operations many times For loops have 4 main components: 1. The counter variable: a variable is created and used to record how many times the for loop has looped (the letter ‘i’ is normally used for this) 2. The conditional statement. As with the while loop, this decides whether the loop is run again (usually involves the counter variable) 3. The increment. This is where the counter variable is incremented after every loop. 4. The code: This is the collection of statements executed every time the loop runs.

For Loops For loop structure For (i = 0; i < numOfTimesToDoLoop; i++){ statement1; statement2; }

Objects JavaScript is an Object Oriented Programming (OOP) language An object is basically just a template for a type of data which gives a standard list of actions (functions) and values (properties) which it will contain Properties are variables which an instance of an object contains  These are referenced by giving the instance name, followed by a full stop, followed by the property name, e.g. myObj.propName Functions are core to the object’s functionality  these are referenced by giving the instance name, followed by a full stop, followed by the function name, e.g. myObj.functName()

Objects The object may then be instantiated, and that instance is assigned values  This instantiation requires we call a special constructor function (we’ll come back to this) There are 3 categories of built-in JavaScript object  General objects e.g. String object, Array object, Math object  Browser objects e.g. Window object, Location object, Navigator object  DOM objects e.g. Document object, HTMLElement object, Style object

Objects In addition to the built-in objects in JavaScript, you can also build your own  We won’t go into this in detail, but you can read more about creating your own objects at and at

Arrays An array is an object! It is basically predefined to be a special type of variable which can store a collection of other variables Arrays are ordered – each variable stored within an array has a number associated with it  This number can then be used to access the variables within the array

Arrays As JavaScript Arrays are objects, they have to be created using a special constructor function e.g.Var myArray = new Array(); Each value in the array may then be entered individually e.g.myArray[0] = “apples"; myArray[1] = “oranges"; myArray[2] = “lemons";

Arrays These values may then be referenced according to their number e.g.document.write(myArray[1]); Arrays can store pretty much any variable type (even other arrays) A complete list of array properties and methods can be found at These can be used with any array we have created, e.g. we can refer to myArray.length or we call the function myArray.join()

JavaScript Exercise Open Notepad, copy in the following basic HTML template, and save it as lecture_4_exercise.html // //

JavaScript Exercise In the  Create an array called shopping_list which stores the three strings milk, cereal, and bread In the  Write a for-loop that prints out each element of shopping_list (use the.length property to specify the number of iterations of the loop)  Write an if-statement which checks the length of myArray and uses document.write() to output either “The shopping list has three or less items” or “The shopping list has more than three items”

References and Further Reading General JavaScript tutorials   List of built-in JavaScript objects 