working with loops, conditional statements, and arrays

Download Working with Loops, Conditional Statements, and Arrays

If you can't read please download the document

Upload: shon-parker

Post on 17-Jan-2018

249 views

Category:

Documents


1 download

DESCRIPTION

The For Loop Can be nested inside another Command block –Collection of commands that is run each time through a loop –Distinguished by opening and closing curly braces { }

TRANSCRIPT

Working with Loops, Conditional Statements, and Arrays Working with Program Loops Program loop Set of commands that executes repeatedly until a stopping condition is met The For loop Counter variable is used to track number of times a set of commands is run General structure for (start; continue; update) { commands } The For Loop Can be nested inside another Command block Collection of commands that is run each time through a loop Distinguished by opening and closing curly braces { } Counter Values in the For loop The While Loop A command block is run as long as a specific condition is met Condition does not depend on value of a counter variable General syntax while (continue) { commands } The Do/While Loop Test to determine whether to continue to loop is made after the command block is run Structure do { commands } while (continue); Working with Conditional Statements Conditional statement Runs a command or command block only when certain conditions are met If statement Most common conditional statement The If Statement Syntax if (condition) { commands } Modulus operator Returns the integer remainder after dividing one integer by another Nesting If Statements General structure if (thisYear % 4 == 0) { if statement for century years } The If...Else Statement General structure if (condition) { commands if true } else { commands if false } Using Multiple Else...If Statements General structure 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 Syntax switch (expression) { case label1: commands1 break; case label2: commands2 break; case label3: commands3 break;... default: default commands } Managing Program Loops and Conditional Statements Break command Used to terminate any program loop or conditional statement Often used to exit a program loop Syntax: break; The continue Command Stops processing commands in current iteration of loop and jumps to next iteration Example var total = 0; for (var i=0; i < data.length; i++) { if (data[i]==null) continue; // continue with the next iteration total += data[i]; } Statement Labels Labels Used to identify statements in your code Often used with break and continue commands to direct a program to a particular statement Syntax: label: statement Some programmers Discourage use of break, continue, and label statements Working with Arrays Array Collection of data values organized under a single name Each data value has a number or index General form array[I] Creating and Populating an Array To create an array var array = new Array(length); Arrays without a defined length can take up more memory Creating and populating an array var array = new Array(values); Array literal var array = [values]; Working with Array Length JavaScript arrays Not required to stay at a fixed size Definition of a value not required for every item Sparse arrays Arrays with several missing or null items Reversing an Array Arrays Associated with a collection of methods that allow you to change their contents, order, and size Syntax for applying methods array.method() Methods for changing order of array items after array is created reverse() and sort() Sorting an Array sort() method Sorts array items in alphabetical order Compare function Used to sort nontextual data correctly Compares values of two adjacent items in the array at a time Applying compare function to sort() method array.sort(function) Extracting and Inserting Array Items Subarray A section of an array slice() method Extracts a part of an array Syntax: array.slice(start, stop) Can be used to insert new items into an array array.splice(start, size, values) Extracting and Inserting Array Items Most efficient methods to insert or remove items push() pop() unshift() shift() Array Methods For Loops and Arrays For loops Used to cycle through different values contained within an array General structure for accessing each value in array for (var i=0; i < array.length; i++) { commands involving array[i] } Tips for Arrays, Program Loops, and Conditional Statements Save space in your program code by Declaring and populating each array in a single new Array() declaration Use array methods like sort() and reverse() to quickly rearrange contents of arrays Use a For loop when Your loop contains a counter variable Tips for Arrays, Program Loops, and Conditional Statements Use While loop for More general stopping conditions To simplify your code Avoid nesting too many levels of If statements Use Switch statement for Conditional statements that involve variables with several different possible values Tips for Arrays, Program Loops, and Conditional Statements Avoid using break and continue statements to Cut off loops unless necessary Instead Set break conditions in the conditional expression for a loop