Tonight’s JavaScript Topics 1 Conditional Statements: if and switch The Array Object Looping Statements: for, while and do-while.

Slides:



Advertisements
Similar presentations
Introduction to Macromedia Director 8.5 – Lingo
Advertisements

1 JavaScript: Control Structures II. 2 whileCounter.html 1 2
PHP Functions and Control Structures. 2 Defining Functions Functions are groups of statements that you can execute as a single unit Function definitions.
Tutorial 12 Working with Arrays, Loops, and Conditional Statements
Iteration This week we will learn how to use iteration in C++ Iteration is the repetition of a statement or block of statements in a program. C++ has three.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Outline IS400: Development of Business Applications on the Internet Fall 2004 Instructor: Dr. Boris Jukic JavaScript: Control Structures: continued.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 6: Loop Control Structures.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 12 – Craps Game Application: Introducing Random.
Outline IS400: Development of Business Applications on the Internet Fall 2004 Instructor: Dr. Boris Jukic JavaScript: Functions Part I.
JavaScript Switch Statement. Switch JavaScript Switch Statement If you have a lot of conditions, you can use a switch statement instead of an if…elseif…
 2004 Prentice Hall, Inc. All rights reserved. 1 Chapter 10 - JavaScript: Functions Outline 10.1 Introduction 10.2 Program Modules in JavaScript 10.3.
Control Structures II. Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas.
Chapter 4: Control Structures II
JavaScript Events and Event Handlers 1 An event is an action that occurs within a Web browser or Web document. An event handler is a statement that tells.
Arrays and Control Structures CST 200 – JavaScript 2 –
* What kind of loop would I use to complete the following: A. Output all of the prime numbers that are less than 100,000 B. Output the Fibonacci sequence.
CISC474 - JavaScript 03/02/2011. Some Background… Great JavaScript Guides: –
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 10 - JavaScript: Functions Outline 10.1 Introduction 10.2 Program Modules in JavaScript 10.3.
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.
CIS 375—Web App Dev II JavaScript II.
PHP Programming with MySQL Slide 4-1 CHAPTER 4 Functions and Control Structures.
WDMD 170 – UW Stevens Point 1 WDMD 170 Internet Languages eLesson: Decision Making with Control Structures and Statements (non-audio version) © Dr. David.
Chapter 2 Functions and Control Structures PHP Programming with MySQL 2 nd Edition.
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.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
Repetition Structures Repetition Structures allow you to write programs that will repeat program steps multiple times. –Also called Loops –Counter controlled.
Chapter 4: Control Structures II
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
JavaScript, Fourth Edition
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
More loops while and do-while. Recall the for loop in general for (initialization; boolean_expression; update) { }
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure /
Control Structures, Blocks and Compound Statements A good programming language allows you to control the flow of your program in three ways: - execute.
1 Chapter 3: Loops and Logic. 2 Control Statements If statement Example NumberCheck.java Relational operators (, >=, ==, !=) Using code blocks with If.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Craps Game Application Introducing Random-Number Generation and Enum.
Working with Loops, Conditional Statements, and Arrays.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
For…Next and Do...While Loops! Happy St. Patrick’s Day!
CONTROL STRUCTURE Chapter 3. CONTROL STRUCTURES ONE-WAY SELECTION Syntax: if (expression) statement Expression referred to as decision maker. Statement.
BMTRY 789 Lecture 6: Proc Sort, Random Number Generators, and Do Loops Readings – Chapters 5 & 6 Lab Problem - Brain Teaser Homework Due – HW 2 Homework.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
XP Tutorial 3 New Perspectives on JavaScript, Comprehensive1 Working with Arrays, Loops, and Conditional Statements Creating a Monthly Calendar.
JavaScript and Ajax (Control Structures) Week 4 Web site:
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
JavaScript, Sixth Edition
Arrays and Loops. Learning Objectives By the end of this lecture, you should be able to: – Understand what a loop is – Appreciate the need for loops and.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
JavaScript JavaScript ( Condition and Loops ). Conditional Statements If Statement If...else Statement if (condition) { code to be executed if condition.
Repetition Looping. Types for while do..while for The for loop allows you to iterate through a variable for a specific range of values. You must supply.
Chapter 22 Selection and Iteration if ( Boolean Expression ) true statement ; else false statement ; OperatorMeaning ! Logical not < Less than
Tutorial 12 Working with Arrays, Loops, and Conditional Statements
JavaScript: Control Statements I
Ch 7: JavaScript Control Statements I.
JavaScript: Control Statements.
Chapter 10 - JavaScript: Functions
LESSON 11 – WHILE LOOPS UNIT 5 – 1/10/17.
CS149D Elements of Computer Science
Loop Control Structure.
Algorithms Take a look at the worksheet. What do we already know, and what will we have to learn in this term?
Decisions, decisions, decisions
How to allow the program to know when to stop a loop.
Chapter 8 JavaScript: Control Statements, Part 2
Presentation transcript:

Tonight’s JavaScript Topics 1 Conditional Statements: if and switch The Array Object Looping Statements: for, while and do-while

Conditional Statements in JavaScript 2 A conditional statement is a statement that runs a command or command block only when certain conditions are met.

The if Statement in JavaScript 3 To test a single condition, use: if (condition) { commands } To test between two conditions, use: if (condition) { commands if condition is true } else { commands if otherwise }

The if Statement in JavaScript (cont.) 4 To test multiple conditions, use: if (condition 1) { first command block } else if (condition 2) { second command block } else if (condition 3) { third command block } else { default command block }

The Switch Statement in JavaScript 5 To test for different values of an expression, use: switch (expression) { case label1: commands1 break; case label2: commands2 break; case label3: commands3 break;... default: default commands } The break statement terminates any program loop or conditional.

A Complete Example A Complete Example 6 Let’s modify last week’s dice program to play a simplified version of craps: –if the dice roll is 2, 3 or 12, the user loses (craps) –if the dice roll is 7 or 11, the user wins –any other number and the user continues rolling We’ll use if statements to do the job. As an outside-class exercise, try changing the program to use a switch statement.

The Array Object in JavaScript 7 An array is a collection of data values organized under a single name –Each individual data value is identified by an index To create an array: var array = new Array(length); To populate an element of the array: array[i] = value; To create and populate an array: var array = new Array(values); To get the size of the array, use the property array.length

Some Useful Array Methods Some Useful Array Methods 8

A Program Example Using an Array A Program Example Using an Array 9 Let’s write a JavaScript program that does the following: –displays a button on the web page –whenever the user presses the button, a random fortune will be displayed to the user. –the list of “random” fortunes will be stored in an array

Looping in JavaScript: The for Loop 10 A program loop is a set of commands executed repeatedly until a stopping condition has been met for loops use a counter variable to track the number of times a block of commands is run To create a for loop, use: for (start at; continue until; update by){ commands to repeat }

A for Loop Example A for Loop Example 11

Another for Loop Example Another for Loop Example 12 for loops are often used to cycle through the different values contained within an array:

Loops in JavaScript: The while Loop 13 A while loop runs as long as a specific condition is true To create a while loop, use: while(continue until condition){ commands to repeat } Example: var sum = 0; while(sum < 10) { sum = sum + Math.random(); }

Loops in JavaScript: The do-while Loop 14 A do-while loop runs as long as a specific condition is true but the condition is at the end To create a do-while loop, use: do { commands to repeat while(continue until condition); A do-while loop is always executed at least one time which isn’t necessarily true of while loops. Watch out for infinite loops!

A Final Example: Rock, Paper, Scissors For player 1, generate a random # between 1 and 3. 1= rock, 2=paper, 3-scissors. 2. For player 2, generate a random # between 1 and 3. 1= rock, 2=paper, 3-scissors. 3. Figure out which player wins by the rules: - paper covers rock (paper wins) - scissors cuts paper (scissors win) - rock breaks scissors (rock wins) - both players have the same item (tie) 4. Display an alert message like: “Player 1 has rock. Player 2 has paper. Player 2 wins.” 5. Keep playing if they tie until one player wins.