The code declares two variables, i and sum, which are initialized to value 0. The variable, i, is a counter variable, whose value increases for every execution of loop. The condition in the while loop checks that the value of the counter variable, i, is less than or equal to 10. If this condition is true, the value of the sum variable is added to the value of i variable. The value of the variable i is incremented by 1. Then, the program control is passed to the while statement to check the condition again. When the value of i becomes 11, the while loop terminates as the loop condition becomes false.
NexTGen Web Session: 14 Loops and Arrays Objectives Explain while loop Explain for loop Explain while loop Explain break and continue statement Explain single-dimensional arrays Explain multi-dimensional arrays Explain for in loop © Aptech Ltd Loops and Arrays / Session 14 Introduction Loops allow you to execute a single statement or a block of statements multiple times They are widely used when you want to display a series of numbers and accept repetitive input A loop construct consists of a condition that instructs the compiler the number of times a specific block of code will be executed If the condition is not specified within the construct, the loop continues infinitely Such loop constructs are referred to as infinite loops JavaScript supports three types of loops that are as follows:Loop while for Loop do-while Loop © Aptech Ltd Loops and Arrays / Session 14 while Loop 1-4 Following figure shows the flow of execution of the while loop © Aptech Ltd Loops and Arrays / Session 14 while Loop 2-4 The syntax for the while loop is as follows: Syntax: while (condition) { where, // statements; condition: Is a boolean expression } The Code Snippet displays the sum of numbers from to 10 by using the while loop var i = 0; var sum = 0; © Aptech Ltd Loops and Arrays / Session 14 while Loop 3-4 ile(i If this condition is true, the value of the sum variable is added to the value of i variable The value of the variable i is incremented by Then, the program control is passed to the while © Aptech Ltd Loops and Arrays / Session 14 while Loop 4-4 Following figure shows the output © Aptech Ltd Loops and Arrays / Session 14 for Loop 1-4 The for loop is similar to the while loop as it executes the statements within the loop as long as the given condition is true Unlike the while loop, the for loop specifies the loop control statements at the top instead in the body of the loop The for loop begins with the for keyword, which is followed by parentheses containing three expressions, each of which are separated by a semicolon The three expressions are referred to as initialization expression, condition expression, and increment/decrement expression respectively The syntax for the for loop is as follows: Syntax: initialization; condition; increment/decreme atements; © Aptech Ltd Loops and Arrays / Session 14 initialization: Initializes the variable(s) for Loop 2-4 that will be used in the condition condition: Comprises the condition that is checked before the statements in the loop are executed increment/decrement: Comprises the statement that changes the value of the variable(s) on every execution of the Following figure showssuccessful the for loop loop to ensure that the condition specified in the condition section is reached © Aptech Ltd Loops and Arrays / Session 14 for Loop 3-4 The Code Snippet demonstrates the script that accepts a number from the user and displays the first ten multiples of that number var inputNum = prompt(‘Enter any number:’); var result = 0; In the code, a variable, document.write (‘Multiples ‘ + and inputNum,of: is created inputNum />’); initialized +to ‘[...]... loop Following figure shows the output of the continue statement © Aptech Ltd Loops and Arrays / Session 14 Arrays Following figure shows the effective usage of memory achieved using an array © Aptech Ltd Loops and Arrays / Session 14 Single-dimensional Array 1-3 JavaScript supports two types of arrays that are as follows: InSingle-dimensional a single-dimensional array, the elements are array. .. two-dimensional array is an example of the multidimensional array Following figure shows the representation of a multi-dimensional array A two-dimensional array is an array of arrays This means, for a two-dimensional array, first a main array is declared and then, an array is created for each element of the main array © Aptech Ltd Loops and Arrays / Session 14 Multi-dimensional Array 2-2 The... Aptech Ltd Loops and Arrays / Session 14 do-while Loop 2-4 Following figure shows the do-while loop © Aptech Ltd Loops and Arrays / Session 14 do-while Loop 3-4 The syntax for the do-while loop is as follows: Syntax: do { where, statements; condition: Is ademonstrates boolean expression The Code Snippet the script to accept the capital of United States from the user using the do-while loop }while(condition);... Employee © Aptech Ltd Loops and Arrays / Session 14 Details ’); Array( 3) creates an array of size 3 Accessing Two-dimensional Arrays 2-4 The declaration employees[0] = new Array( ‘John’,’23’,’New Jersey’) creates an array at the 0th row of the employees array Similarly, employees[1] = new Array( ‘David’,’21’,’California’) creates an array at the first row of the employees array Following figure... Aptech Ltd Loops and Arrays / Session 14 Single-dimensional Array 3-3 The Code Snippet shows the different ways to declare and initialize a single-dimensional array //Declaration using Array Object and then Initialization var marital_status = new Array( 3); marital_status[0] = ‘Single’; marital_status[1] = ‘Married’; marital_status[2] = ‘Divorced’; © Aptech Ltd Loops and Arrays / Session 14 Accessing... and Arrays / Session 14 Accessing Single-dimensional Arrays 2-4 Following figure displays the names of the students JavaScript allows you to access array elements by Accessing Elements With Loops using Array different loops Thus, you can access each array element by putting a counter variable of the loop as the index of an element However, this requires the count of the elements in an array. .. marks sum = sum + marks[i]; © Aptech Ltd Loops and Arrays / Session 14 Accessing Single-dimensional Arrays 4-4 Following figure displays the average of the marks, 90, 75, 85, 95, and 82 accepted from the user in the prompt box © Aptech Ltd Loops and Arrays / Session 14 A multi-dimensional array stores a combination of Multi-dimensional values of a single typeArray in two 1-2 or more dimensions These... Following figure displays the employee details © Aptech Ltd Loops and Arrays / Session 14 Accessing Two-dimensional Arrays 3-4 Accessing Array Elements With Loops The Code Snippet creates a two-dimensional array to display the product details var products = new Array( 2); products[0] = new Array( ‘Monitor’, ‘236.75’); products[1] = new Array( ‘Keyboard’, ‘45.50’); document.write(‘Name... Accessing Two-dimensional Arrays 1-4 Multi-dimensional arrays can be accessed by using the index of main array variable along with index of Accessing Array Elements Without Loops sub -array The Code Snippet creates a two-dimensional array that displays the employee details var employees = new Array( 3); employees[0] = new Array( ‘John’, ‘25’, ‘New Jersey’); employees[1] = new Array( ‘David’, ‘21’,... border=1>Name © Aptech Ltd Price’); Loops and Arrays / Session 14 Array( ‘Keyboard’,’45.50’) creates an array at the first row of the products Accessing Two-dimensional Arraysarray 4-4 The condition, i < products.length, specifies that the counter variable, i, should be less than the number of rows in the array variable, products For each row in the array, the condition, j < products[i].length ... Objectives Explain while loop Explain for loop Explain while loop Explain break and continue statement Explain single-dimensional arrays Explain multi-dimensional arrays Explain for in loop. .. Aptech Ltd Loops and Arrays / Session 14 while Loop 4-4 Following figure shows the output © Aptech Ltd Loops and Arrays / Session 14 for Loop 1-4 The for loop is similar to the while loop as it... Ltd Loops and Arrays / Session 14 do-while Loop 2-4 Following figure shows the do-while loop © Aptech Ltd Loops and Arrays / Session 14 do-while Loop 3-4 The syntax for the do-while loop