SlideShare une entreprise Scribd logo
1  sur  51
1
CS101 Introduction to Computing
Lecture 26Arrays
(Web Development Lecture 9)
2
During the last lecture we had a
discussion on Flow Control & Loops
• We discussed the concept of flow control using
the “if” and “switch” structures
• And also the concept behind the “while” and
“for” looping structures
• We also solved simple problems using flow
control and loop structures
3
if…else --?-- switch
• If the action to be taken of the value of a single
variable (or a single expression), use ‘switch’
• When the action depends on the values of
multiple variables (or expressions), use the
‘if...else’ structure
4
Compound Statements
• At times, we need to put multiple statements at
places where JavaScript expects only one
• For those situations, JavaScript provides a way
of grouping a number of statements into a
single statement, called a “statement block”
• This is done simply by enclosing any number of
statements within curly braces, { }
• NOTE: Although the statements within the
block end in semicolons, the block itself doesn’t
5
for: Example 1
x = 1 ;
while ( x < 6000 ) {
document.write ( x ) ;
x = x + 1 ;
} for ( x = 1 ; x < 6000 ; x = x + 1 ) {
document.write ( x ) ;
}
Initial count Condition Operation
6
for --?-- while
• When the exact number of iterations is
known, use the ‘for’ loop
• When the number of iterations depend
upon a condition being met, use the
‘while’ loop
7
‘for’ loops become especially useful
when used in conjunction with arrays
We’ll find out about arrays today, and
we’ll probe their usefulness as part of
‘for’ loop structures
8
Today’s Topic:
Arrays
• We will find out why we need arrays
• We will become able to use arrays for solving
simple problems
9
Array?
10
Array
An indexed list of elements
We said that a variable is a container that
holds a value.
Similarly, an Array can be considered a
container as well, but this one can hold
multiple values
11
Array
An indexed list of elements
Example: There are many ways of
assigning identifiers to the following fruit
strawberry
fruit1
fruit[ 0 ]
orange
fruit2
fruit[ 1 ]
apple
fruit3
fruit[ 2 ]
watermelon
fruit4
fruit[ 3 ]
12
Array
An indexed list of elements
• fruit[ 0 ], fruit[ 1 ], fruit[ 2 ], and fruit[ 3 ] are the
elements of an array
• ‘fruit’ is the identifier for that array
• The length of the ‘fruit’ array is 4, i.e. ‘fruit’ has
four elements
13
Array
fruit[ 0 ]
Identifier
Square
bracket
Index
14
Let’s now take look at one of the
advantages of using arrays
15
var student1, student2, student3, student4 ;
student1 = “Waseem” ;
student2 = “Waqar” ;
student3 = “Saqlain” ;
student4 = “Daanish” ;
document.write( student1 ) ;
document.write( student2 ) ;
document.write( student3 ) ;
document.write( student4 ) ;
16
student = new Array( 4 ) ; //array declaration
student[ 0 ] = “Waseem” ;
student[ 1 ] = “Waqar” ;
student[ 2 ] = “Saqlain” ;
student[ 3 ] = “Daanish” ;
for ( x = 0 ; x < 4 ; x = x + 1 ) {
document.write( student[ x ] ) ;
}
Can you see
the advantage
of using arrays
along with the
‘for’ loop?
17
Arrays in JavaScript
• In JavaScript, arrays are implemented in the
form of the ‘Array’ object
• The key property of the ‘Array’ object is ‘length’,
i.e the number of elements in an array
• Two of the key ‘Array’ methods are:
– reverse( )
– sort( )
• Elements of an array can be of any type; you
can even have an array containing other arrays
18
Declaring a New Instance of the Array Object
• ‘student’ is an instance of the ‘Array’ object
• ‘student’ was declared such that it is of length ‘4’
• That is, student is an array having 4 elements
• The four elements of the array are: ‘student[ 0 ]’,
‘student[ 1 ]’, ‘student[ 2 ]’, and ‘student[ 3 ]’
19
student = new Array( 4 )
This is the
identifier of the
new instance
The ‘new’
operator creates
an instance
This is the parent object (or
class) of the new instance
Length of the
new instance
of ‘Array’
Pair of
paren-
theses
The ‘assignment’
operator
20
An Object
21
‘Instances’ of an Object
22
All instances
of an object are
objects themselves!
23
‘Property’ Values of the Instances May Differ
24
student = new Array( 4 )
25
Array Identifiers
The naming rules for Array identifiers are
the same as were discussed for variable
identifiers
26
Assigning Values to Array Elements
a[ 1 ] = 5 ; //the second element
name[ 5 ] = “bhola” ;
number = 5 ;
name[ number ] = name[ 5 ] ;
for ( x = 0 ; x < 10 ; x = x + 1 ) {
y[ x ] = x * x ;
}
27
Remember: just like C, C++ and Java,
the first element of an array has an
index number equal to zero
28
JavaScript Arrays are Heterogeneous
Unlike many other popular languages,
a JavaScript Array can hold elements
of multiple data types, simultaneously
a = new Array( 9 ) ;
b = new Array( 13 ) ;
b[ 0 ] = 23.7 ;
b[ 1 ] = “Bhola Continental Hotel” ;
b[ 2 ] = a ;
29
The ‘length’ Property of Arrays
d = new Array ( 5 ) ;
document.write( d.length ) ;
‘d’ is an
instance of the
‘Array’ object
‘length’ is a
property of
the object ‘d’
30
The ‘length’ Property of Arrays
x = new Array ( 10 ) ;
for ( x = 0 ; x < 10 ; x = x + 1 ) {
y[ x ] = x * x ;
}
x = new Array ( 10 ) ;
for ( x = 0 ; x < x.length ; x = x + 1 ) {
y[ x ] = x * x ;
}
What is
advantage
of using
‘x.length’
here
instead of
using the
literal ‘10’?
31
Array Methods: sort( )
Sorts the elements in alphabetical order
x = new Array ( 4 ) ;
x[ 0 ] = “Waseem” ;
x[ 1 ] = “Waqar” ;
x[ 2 ] = “Saqlain” ;
x[ 3 ] = “Shoaib” ;
x.sort( ) ;
for ( k = 0 ; k < x.length; k = k + 1 ) {
document.write( x[ k ] + “<BR>” ) ;
}
Saqlain
Shoaib
Waqar
Waseem
32
Were the elements sorted in
ascending or descending order?
What if you wanted to arrange them
in the reverse order?
33
Array Methods: reverse( )
Reverses the order of the elements
x = new Array ( 4 ) ;
x[ 0 ] = “Waseem” ;
x[ 1 ] = “Waqar” ;
x[ 2 ] = “Saqlain” ;
x[ 3 ] = “Shoaib” ;
x.reverse( ) ;
x.sort( ) ;
for ( k = 0 ; k < x.length; k = k + 1 ) {
document.write( x[ k ] + “<BR>”) ;
}
Saqlain
Shoaib
Waqar
Waseem
Is this the
required
result?
34
Array Methods: reverse( )
Reverses the order of the elements
x = new Array ( 4 ) ;
x[ 0 ] = “Waseem” ;
x[ 1 ] = “Waqar” ;
x[ 2 ] = “Saqlain” ;
x[ 3 ] = “Shoaib” ;
x.sort( ) ;
x.reverse( ) ;
for ( k = 0 ; k < x.length; k = k + 1 ) {
document.write( x[ k ] + “<BR>”) ;
}
Waseem
Waqar
Shoaib
Saqlain
35
Let’s Now Do a More Substantial Example
Develop a Web page that prompts the
user for 10 words, and then displays them
in form of a list in two different ways:
1. In the order in which the words were
entered
2. In a sorted order
We will try to show you the complete code - the
JavaScript part as well as the HTML part - for this
example
36
Before looking at code, let’s first
understand what is that code
supposed to do
37
38
39
Pseudo Code
1. Declare the array that will be used for
storing the words
2. Prompt the user and read the user
input into the elements of the array
3. Now write the array to the document
4. Sort the array
5. Write the sorted array to the document
40
<HTML>
<HEAD>
<TITLE>Sort Ten Words</TITLE>
<SCRIPT>
words = new Array ( 10 ) ;
for ( k = 0 ; k < words.length ; k = k + 1 ) {
words[ k ] = window.prompt( "Enter word # " + k, "" ) ;
}
document.write( "UNSORTED WORDS:" + "<BR>" ) ;
for ( k = 0 ; k < words.length ; k = k + 1 ) {
document.write( words[ k ] + "<BR>" ) ;
}
words.sort( ) ;
document.write( "SORTED WORDS:" + "<BR>" ) ;
for ( k = 0 ; k < words.length ; k = k + 1 ) {
document.write( words[ k ] + "<BR>" ) ;
}
</SCRIPT>
</HEAD>
<BODY>
</BODY>
</HTML>
41
<HTML>
<HEAD>
<TITLE>Sort Ten Words</TITLE>
<SCRIPT>
//JavaScript Code
</SCRIPT>
</HEAD>
<BODY>
</BODY>
</HTML>
42
The next three slides show the
JavaScript code that goes between
the <SCRIPT>, </SCRIPT> tags
43
Pseudo Code
1. Declare the array that will be used for
storing the words
2. Prompt the user and read the user
input into the elements of the array
3. Now write the array to the document
4. Sort the array
5. Write the sorted array to the document
44
words = new Array ( 10 ) ;
for ( k = 0 ; k < words.length ; k = k + 1 ) {
words[ k ] = window.prompt(
"Enter word # " + k, "" ) ;
}
This method is used
for collecting data
from the user. It can
display a message
and provides a field
in which the user can
enter data
45
Pseudo Code
1. Declare the array that will be used for
storing the words
2. Prompt the user and read the user
input into the elements of the array
3. Now write the array to the document
4. Sort the array
5. Write the sorted array to the document
46
document.write( "Unsorted Words:" + "<BR>" ) ;
for ( k = 0 ; k < words.length ; k = k + 1 ) {
document.write( words[ k ] + "<BR>" ) ;
}
47
Pseudo Code
1. Declare the array that will be used for
storing the words
2. Prompt the user and read the user
input into the elements of the array
3. Now write the array to the document
4. Sort the array
5. Write the sorted array to the document
48
words.sort( ) ;
document.write( "Sorted Words:" + "<BR>" ) ;
for ( k = 0 ; k < words.length ; k = k + 1 ) {
document.write( words[ k ] + "<BR>" ) ;
}
49
Assignment #9
Build a Web page that implements the Bubble
Sort algorithm discussed during the 17th
lecture
(Algorithms II)
The numbers to be sorted will be provided to you
and should be hard coded in the JavaScript code.
Your page should display a button labeled “Run
Bubble Sort”. When that button is clicked, the
page should display the sorted list of numbers
Further information on this assignment will be provide on
the CS101 Web site
50
During Today’s Lecture …
• We found out why we need arrays
• We became able to use arrays for solving
simple problems
51
Next (the 10th
) Web Dev Lecture:
Functions & Variable Scope
• To become familiar with some of JavaScript’s
built-in functions
• To be able to understand the concept of user-
defined functions and their use for solving
simple problems
• To become familiar with the concept of local
and global variables

Contenu connexe

Tendances

functional groovy
functional groovyfunctional groovy
functional groovyPaul King
 
11. session 11 functions and objects
11. session 11   functions and objects11. session 11   functions and objects
11. session 11 functions and objectsPhúc Đỗ
 
concurrency with GPars
concurrency with GParsconcurrency with GPars
concurrency with GParsPaul King
 
Map-Reduce and Apache Hadoop
Map-Reduce and Apache HadoopMap-Reduce and Apache Hadoop
Map-Reduce and Apache HadoopSvetlin Nakov
 
concurrency gpars
concurrency gparsconcurrency gpars
concurrency gparsPaul King
 
Mythbusting: Understanding How We Measure Performance at MongoDB
Mythbusting: Understanding How We Measure Performance at MongoDBMythbusting: Understanding How We Measure Performance at MongoDB
Mythbusting: Understanding How We Measure Performance at MongoDBMongoDB
 
GPars (Groovy Parallel Systems)
GPars (Groovy Parallel Systems)GPars (Groovy Parallel Systems)
GPars (Groovy Parallel Systems)Gagan Agrawal
 
Introduction To Groovy
Introduction To GroovyIntroduction To Groovy
Introduction To Groovymanishkp84
 
Java script objects 1
Java script objects 1Java script objects 1
Java script objects 1H K
 
Apache Spark - Key-Value RDD | Big Data Hadoop Spark Tutorial | CloudxLab
Apache Spark - Key-Value RDD | Big Data Hadoop Spark Tutorial | CloudxLabApache Spark - Key-Value RDD | Big Data Hadoop Spark Tutorial | CloudxLab
Apache Spark - Key-Value RDD | Big Data Hadoop Spark Tutorial | CloudxLabCloudxLab
 
Kotlin – the future of android
Kotlin – the future of androidKotlin – the future of android
Kotlin – the future of androidDJ Rausch
 
Programming with Millions of Examples (HRL)
Programming with Millions of Examples (HRL)Programming with Millions of Examples (HRL)
Programming with Millions of Examples (HRL)Eran Yahav
 
XSpect, a lightweight library to make your code reusable and maintainable.
XSpect, a lightweight library to make your code reusable and maintainable.XSpect, a lightweight library to make your code reusable and maintainable.
XSpect, a lightweight library to make your code reusable and maintainable.岡諭 李
 
Scala for Java programmers
Scala for Java programmersScala for Java programmers
Scala for Java programmers輝 子安
 
The Ring programming language version 1.8 book - Part 80 of 202
The Ring programming language version 1.8 book - Part 80 of 202The Ring programming language version 1.8 book - Part 80 of 202
The Ring programming language version 1.8 book - Part 80 of 202Mahmoud Samir Fayed
 
Gpars concepts explained
Gpars concepts explainedGpars concepts explained
Gpars concepts explainedVaclav Pech
 
The Ring programming language version 1.7 book - Part 39 of 196
The Ring programming language version 1.7 book - Part 39 of 196The Ring programming language version 1.7 book - Part 39 of 196
The Ring programming language version 1.7 book - Part 39 of 196Mahmoud Samir Fayed
 

Tendances (20)

functional groovy
functional groovyfunctional groovy
functional groovy
 
Ggplot2 v3
Ggplot2 v3Ggplot2 v3
Ggplot2 v3
 
11. session 11 functions and objects
11. session 11   functions and objects11. session 11   functions and objects
11. session 11 functions and objects
 
Scala DSLの作り方
Scala DSLの作り方Scala DSLの作り方
Scala DSLの作り方
 
concurrency with GPars
concurrency with GParsconcurrency with GPars
concurrency with GPars
 
Map-Reduce and Apache Hadoop
Map-Reduce and Apache HadoopMap-Reduce and Apache Hadoop
Map-Reduce and Apache Hadoop
 
concurrency gpars
concurrency gparsconcurrency gpars
concurrency gpars
 
Mythbusting: Understanding How We Measure Performance at MongoDB
Mythbusting: Understanding How We Measure Performance at MongoDBMythbusting: Understanding How We Measure Performance at MongoDB
Mythbusting: Understanding How We Measure Performance at MongoDB
 
GPars (Groovy Parallel Systems)
GPars (Groovy Parallel Systems)GPars (Groovy Parallel Systems)
GPars (Groovy Parallel Systems)
 
Introduction To Groovy
Introduction To GroovyIntroduction To Groovy
Introduction To Groovy
 
Java script objects 1
Java script objects 1Java script objects 1
Java script objects 1
 
Apache Spark - Key-Value RDD | Big Data Hadoop Spark Tutorial | CloudxLab
Apache Spark - Key-Value RDD | Big Data Hadoop Spark Tutorial | CloudxLabApache Spark - Key-Value RDD | Big Data Hadoop Spark Tutorial | CloudxLab
Apache Spark - Key-Value RDD | Big Data Hadoop Spark Tutorial | CloudxLab
 
Kotlin – the future of android
Kotlin – the future of androidKotlin – the future of android
Kotlin – the future of android
 
Programming with Millions of Examples (HRL)
Programming with Millions of Examples (HRL)Programming with Millions of Examples (HRL)
Programming with Millions of Examples (HRL)
 
XSpect, a lightweight library to make your code reusable and maintainable.
XSpect, a lightweight library to make your code reusable and maintainable.XSpect, a lightweight library to make your code reusable and maintainable.
XSpect, a lightweight library to make your code reusable and maintainable.
 
Scala for Java programmers
Scala for Java programmersScala for Java programmers
Scala for Java programmers
 
The Ring programming language version 1.8 book - Part 80 of 202
The Ring programming language version 1.8 book - Part 80 of 202The Ring programming language version 1.8 book - Part 80 of 202
The Ring programming language version 1.8 book - Part 80 of 202
 
Scala
ScalaScala
Scala
 
Gpars concepts explained
Gpars concepts explainedGpars concepts explained
Gpars concepts explained
 
The Ring programming language version 1.7 book - Part 39 of 196
The Ring programming language version 1.7 book - Part 39 of 196The Ring programming language version 1.7 book - Part 39 of 196
The Ring programming language version 1.7 book - Part 39 of 196
 

En vedette

MGT101 - Financial Accounting- Lecture 45
MGT101 - Financial Accounting- Lecture 45MGT101 - Financial Accounting- Lecture 45
MGT101 - Financial Accounting- Lecture 45Bilal Ahmed
 
ENG101- English Comprehension- Lecture 23
ENG101- English Comprehension- Lecture 23ENG101- English Comprehension- Lecture 23
ENG101- English Comprehension- Lecture 23Bilal Ahmed
 
MGT101 - Financial Accounting- Lecture 27
MGT101 - Financial Accounting- Lecture 27MGT101 - Financial Accounting- Lecture 27
MGT101 - Financial Accounting- Lecture 27Bilal Ahmed
 
CS201- Introduction to Programming- Lecture 06
CS201- Introduction to Programming- Lecture 06CS201- Introduction to Programming- Lecture 06
CS201- Introduction to Programming- Lecture 06Bilal Ahmed
 
CS201- Introduction to Programming- Lecture 35
CS201- Introduction to Programming- Lecture 35CS201- Introduction to Programming- Lecture 35
CS201- Introduction to Programming- Lecture 35Bilal Ahmed
 
CS201- Introduction to Programming- Lecture 11
CS201- Introduction to Programming- Lecture 11CS201- Introduction to Programming- Lecture 11
CS201- Introduction to Programming- Lecture 11Bilal Ahmed
 
MGT101 - Financial Accounting- Lecture 33
MGT101 - Financial Accounting- Lecture 33MGT101 - Financial Accounting- Lecture 33
MGT101 - Financial Accounting- Lecture 33Bilal Ahmed
 
CS201- Introduction to Programming- Lecture 43
CS201- Introduction to Programming- Lecture 43CS201- Introduction to Programming- Lecture 43
CS201- Introduction to Programming- Lecture 43Bilal Ahmed
 
MTH101 - Calculus and Analytical Geometry- Lecture 41
MTH101 - Calculus and Analytical Geometry- Lecture 41MTH101 - Calculus and Analytical Geometry- Lecture 41
MTH101 - Calculus and Analytical Geometry- Lecture 41Bilal Ahmed
 
ENG101- English Comprehension- Lecture 40
ENG101- English Comprehension- Lecture 40ENG101- English Comprehension- Lecture 40
ENG101- English Comprehension- Lecture 40Bilal Ahmed
 
CS101- Introduction to Computing- Lecture 32
CS101- Introduction to Computing- Lecture 32CS101- Introduction to Computing- Lecture 32
CS101- Introduction to Computing- Lecture 32Bilal Ahmed
 
ENG101- English Comprehension- Lecture 30
ENG101- English Comprehension- Lecture 30ENG101- English Comprehension- Lecture 30
ENG101- English Comprehension- Lecture 30Bilal Ahmed
 
MTH101 - Calculus and Analytical Geometry- Lecture 44
MTH101 - Calculus and Analytical Geometry- Lecture 44MTH101 - Calculus and Analytical Geometry- Lecture 44
MTH101 - Calculus and Analytical Geometry- Lecture 44Bilal Ahmed
 
CS101- Introduction to Computing- Lecture 31
CS101- Introduction to Computing- Lecture 31CS101- Introduction to Computing- Lecture 31
CS101- Introduction to Computing- Lecture 31Bilal Ahmed
 
MGT101 - Financial Accounting- Lecture 38
MGT101 - Financial Accounting- Lecture 38MGT101 - Financial Accounting- Lecture 38
MGT101 - Financial Accounting- Lecture 38Bilal Ahmed
 
MGT101 - Financial Accounting- Lecture 28
MGT101 - Financial Accounting- Lecture 28MGT101 - Financial Accounting- Lecture 28
MGT101 - Financial Accounting- Lecture 28Bilal Ahmed
 
CS101- Introduction to Computing- Lecture 44
CS101- Introduction to Computing- Lecture 44CS101- Introduction to Computing- Lecture 44
CS101- Introduction to Computing- Lecture 44Bilal Ahmed
 
MGT101 - Financial Accounting- Lecture 40
MGT101 - Financial Accounting- Lecture 40MGT101 - Financial Accounting- Lecture 40
MGT101 - Financial Accounting- Lecture 40Bilal Ahmed
 
CS101- Introduction to Computing- Lecture 29
CS101- Introduction to Computing- Lecture 29CS101- Introduction to Computing- Lecture 29
CS101- Introduction to Computing- Lecture 29Bilal Ahmed
 
CS201- Introduction to Programming- Lecture 33
CS201- Introduction to Programming- Lecture 33CS201- Introduction to Programming- Lecture 33
CS201- Introduction to Programming- Lecture 33Bilal Ahmed
 

En vedette (20)

MGT101 - Financial Accounting- Lecture 45
MGT101 - Financial Accounting- Lecture 45MGT101 - Financial Accounting- Lecture 45
MGT101 - Financial Accounting- Lecture 45
 
ENG101- English Comprehension- Lecture 23
ENG101- English Comprehension- Lecture 23ENG101- English Comprehension- Lecture 23
ENG101- English Comprehension- Lecture 23
 
MGT101 - Financial Accounting- Lecture 27
MGT101 - Financial Accounting- Lecture 27MGT101 - Financial Accounting- Lecture 27
MGT101 - Financial Accounting- Lecture 27
 
CS201- Introduction to Programming- Lecture 06
CS201- Introduction to Programming- Lecture 06CS201- Introduction to Programming- Lecture 06
CS201- Introduction to Programming- Lecture 06
 
CS201- Introduction to Programming- Lecture 35
CS201- Introduction to Programming- Lecture 35CS201- Introduction to Programming- Lecture 35
CS201- Introduction to Programming- Lecture 35
 
CS201- Introduction to Programming- Lecture 11
CS201- Introduction to Programming- Lecture 11CS201- Introduction to Programming- Lecture 11
CS201- Introduction to Programming- Lecture 11
 
MGT101 - Financial Accounting- Lecture 33
MGT101 - Financial Accounting- Lecture 33MGT101 - Financial Accounting- Lecture 33
MGT101 - Financial Accounting- Lecture 33
 
CS201- Introduction to Programming- Lecture 43
CS201- Introduction to Programming- Lecture 43CS201- Introduction to Programming- Lecture 43
CS201- Introduction to Programming- Lecture 43
 
MTH101 - Calculus and Analytical Geometry- Lecture 41
MTH101 - Calculus and Analytical Geometry- Lecture 41MTH101 - Calculus and Analytical Geometry- Lecture 41
MTH101 - Calculus and Analytical Geometry- Lecture 41
 
ENG101- English Comprehension- Lecture 40
ENG101- English Comprehension- Lecture 40ENG101- English Comprehension- Lecture 40
ENG101- English Comprehension- Lecture 40
 
CS101- Introduction to Computing- Lecture 32
CS101- Introduction to Computing- Lecture 32CS101- Introduction to Computing- Lecture 32
CS101- Introduction to Computing- Lecture 32
 
ENG101- English Comprehension- Lecture 30
ENG101- English Comprehension- Lecture 30ENG101- English Comprehension- Lecture 30
ENG101- English Comprehension- Lecture 30
 
MTH101 - Calculus and Analytical Geometry- Lecture 44
MTH101 - Calculus and Analytical Geometry- Lecture 44MTH101 - Calculus and Analytical Geometry- Lecture 44
MTH101 - Calculus and Analytical Geometry- Lecture 44
 
CS101- Introduction to Computing- Lecture 31
CS101- Introduction to Computing- Lecture 31CS101- Introduction to Computing- Lecture 31
CS101- Introduction to Computing- Lecture 31
 
MGT101 - Financial Accounting- Lecture 38
MGT101 - Financial Accounting- Lecture 38MGT101 - Financial Accounting- Lecture 38
MGT101 - Financial Accounting- Lecture 38
 
MGT101 - Financial Accounting- Lecture 28
MGT101 - Financial Accounting- Lecture 28MGT101 - Financial Accounting- Lecture 28
MGT101 - Financial Accounting- Lecture 28
 
CS101- Introduction to Computing- Lecture 44
CS101- Introduction to Computing- Lecture 44CS101- Introduction to Computing- Lecture 44
CS101- Introduction to Computing- Lecture 44
 
MGT101 - Financial Accounting- Lecture 40
MGT101 - Financial Accounting- Lecture 40MGT101 - Financial Accounting- Lecture 40
MGT101 - Financial Accounting- Lecture 40
 
CS101- Introduction to Computing- Lecture 29
CS101- Introduction to Computing- Lecture 29CS101- Introduction to Computing- Lecture 29
CS101- Introduction to Computing- Lecture 29
 
CS201- Introduction to Programming- Lecture 33
CS201- Introduction to Programming- Lecture 33CS201- Introduction to Programming- Lecture 33
CS201- Introduction to Programming- Lecture 33
 

Similaire à CS101- Introduction to Computing- Lecture 26

Javascript Arrays
Javascript ArraysJavascript Arrays
Javascript Arraysshaheenakv
 
Handout - Introduction to Programming
Handout - Introduction to ProgrammingHandout - Introduction to Programming
Handout - Introduction to ProgrammingCindy Royal
 
Functional Programming With Scala
Functional Programming With ScalaFunctional Programming With Scala
Functional Programming With ScalaKnoldus Inc.
 
Pi j3.4 data-structures
Pi j3.4 data-structuresPi j3.4 data-structures
Pi j3.4 data-structuresmcollison
 
Introduction to web programming for java and c# programmers by @drpicox
Introduction to web programming for java and c# programmers by @drpicoxIntroduction to web programming for java and c# programmers by @drpicox
Introduction to web programming for java and c# programmers by @drpicoxDavid Rodenas
 
Functional programming with Scala
Functional programming with ScalaFunctional programming with Scala
Functional programming with ScalaNeelkanth Sachdeva
 
Javascript variables and datatypes
Javascript variables and datatypesJavascript variables and datatypes
Javascript variables and datatypesVarun C M
 
Introduction to Ruby Programming Language
Introduction to Ruby Programming LanguageIntroduction to Ruby Programming Language
Introduction to Ruby Programming LanguageNicolò Calcavecchia
 
Rich Internet Applications con JavaFX e NetBeans
Rich Internet Applications  con JavaFX e NetBeans Rich Internet Applications  con JavaFX e NetBeans
Rich Internet Applications con JavaFX e NetBeans Fabrizio Giudici
 
Taxonomy of Scala
Taxonomy of ScalaTaxonomy of Scala
Taxonomy of Scalashinolajla
 
Scala for Java Programmers
Scala for Java ProgrammersScala for Java Programmers
Scala for Java ProgrammersEric Pederson
 

Similaire à CS101- Introduction to Computing- Lecture 26 (20)

Javascript Arrays
Javascript ArraysJavascript Arrays
Javascript Arrays
 
Handout - Introduction to Programming
Handout - Introduction to ProgrammingHandout - Introduction to Programming
Handout - Introduction to Programming
 
CSC PPT 13.pptx
CSC PPT 13.pptxCSC PPT 13.pptx
CSC PPT 13.pptx
 
Javascript 101
Javascript 101Javascript 101
Javascript 101
 
Functional Programming With Scala
Functional Programming With ScalaFunctional Programming With Scala
Functional Programming With Scala
 
Pi j3.4 data-structures
Pi j3.4 data-structuresPi j3.4 data-structures
Pi j3.4 data-structures
 
Ruby Basics
Ruby BasicsRuby Basics
Ruby Basics
 
CAP615-Unit1.pptx
CAP615-Unit1.pptxCAP615-Unit1.pptx
CAP615-Unit1.pptx
 
C# p9
C# p9C# p9
C# p9
 
Introduction to web programming for java and c# programmers by @drpicox
Introduction to web programming for java and c# programmers by @drpicoxIntroduction to web programming for java and c# programmers by @drpicox
Introduction to web programming for java and c# programmers by @drpicox
 
Coding in Style
Coding in StyleCoding in Style
Coding in Style
 
Functional programming with Scala
Functional programming with ScalaFunctional programming with Scala
Functional programming with Scala
 
Javascript variables and datatypes
Javascript variables and datatypesJavascript variables and datatypes
Javascript variables and datatypes
 
Introduction to Ruby Programming Language
Introduction to Ruby Programming LanguageIntroduction to Ruby Programming Language
Introduction to Ruby Programming Language
 
Understanding linq
Understanding linqUnderstanding linq
Understanding linq
 
Chapter 4
Chapter 4Chapter 4
Chapter 4
 
Rich Internet Applications con JavaFX e NetBeans
Rich Internet Applications  con JavaFX e NetBeans Rich Internet Applications  con JavaFX e NetBeans
Rich Internet Applications con JavaFX e NetBeans
 
Taxonomy of Scala
Taxonomy of ScalaTaxonomy of Scala
Taxonomy of Scala
 
CH1 ARRAY (1).pptx
CH1 ARRAY (1).pptxCH1 ARRAY (1).pptx
CH1 ARRAY (1).pptx
 
Scala for Java Programmers
Scala for Java ProgrammersScala for Java Programmers
Scala for Java Programmers
 

Plus de Bilal Ahmed

CS201- Introduction to Programming- Lecture 45
CS201- Introduction to Programming- Lecture 45CS201- Introduction to Programming- Lecture 45
CS201- Introduction to Programming- Lecture 45Bilal Ahmed
 
CS201- Introduction to Programming- Lecture 44
CS201- Introduction to Programming- Lecture 44CS201- Introduction to Programming- Lecture 44
CS201- Introduction to Programming- Lecture 44Bilal Ahmed
 
CS201- Introduction to Programming- Lecture 42
CS201- Introduction to Programming- Lecture 42CS201- Introduction to Programming- Lecture 42
CS201- Introduction to Programming- Lecture 42Bilal Ahmed
 
CS201- Introduction to Programming- Lecture 41
CS201- Introduction to Programming- Lecture 41CS201- Introduction to Programming- Lecture 41
CS201- Introduction to Programming- Lecture 41Bilal Ahmed
 
CS201- Introduction to Programming- Lecture 40
CS201- Introduction to Programming- Lecture 40CS201- Introduction to Programming- Lecture 40
CS201- Introduction to Programming- Lecture 40Bilal Ahmed
 
CS201- Introduction to Programming- Lecture 39
CS201- Introduction to Programming- Lecture 39CS201- Introduction to Programming- Lecture 39
CS201- Introduction to Programming- Lecture 39Bilal Ahmed
 
CS201- Introduction to Programming- Lecture 38
CS201- Introduction to Programming- Lecture 38CS201- Introduction to Programming- Lecture 38
CS201- Introduction to Programming- Lecture 38Bilal Ahmed
 
CS201- Introduction to Programming- Lecture 37
CS201- Introduction to Programming- Lecture 37CS201- Introduction to Programming- Lecture 37
CS201- Introduction to Programming- Lecture 37Bilal Ahmed
 
CS201- Introduction to Programming- Lecture 36
CS201- Introduction to Programming- Lecture 36CS201- Introduction to Programming- Lecture 36
CS201- Introduction to Programming- Lecture 36Bilal Ahmed
 
CS201- Introduction to Programming- Lecture 34
CS201- Introduction to Programming- Lecture 34CS201- Introduction to Programming- Lecture 34
CS201- Introduction to Programming- Lecture 34Bilal Ahmed
 
CS201- Introduction to Programming- Lecture 32
CS201- Introduction to Programming- Lecture 32CS201- Introduction to Programming- Lecture 32
CS201- Introduction to Programming- Lecture 32Bilal Ahmed
 
CS201- Introduction to Programming- Lecture 31
CS201- Introduction to Programming- Lecture 31CS201- Introduction to Programming- Lecture 31
CS201- Introduction to Programming- Lecture 31Bilal Ahmed
 
CS201- Introduction to Programming- Lecture 30
CS201- Introduction to Programming- Lecture 30CS201- Introduction to Programming- Lecture 30
CS201- Introduction to Programming- Lecture 30Bilal Ahmed
 
CS201- Introduction to Programming- Lecture 29
CS201- Introduction to Programming- Lecture 29CS201- Introduction to Programming- Lecture 29
CS201- Introduction to Programming- Lecture 29Bilal Ahmed
 
CS201- Introduction to Programming- Lecture 28
CS201- Introduction to Programming- Lecture 28CS201- Introduction to Programming- Lecture 28
CS201- Introduction to Programming- Lecture 28Bilal Ahmed
 
CS201- Introduction to Programming- Lecture 27
CS201- Introduction to Programming- Lecture 27CS201- Introduction to Programming- Lecture 27
CS201- Introduction to Programming- Lecture 27Bilal Ahmed
 
CS201- Introduction to Programming- Lecture 26
CS201- Introduction to Programming- Lecture 26CS201- Introduction to Programming- Lecture 26
CS201- Introduction to Programming- Lecture 26Bilal Ahmed
 
CS201- Introduction to Programming- Lecture 25
CS201- Introduction to Programming- Lecture 25CS201- Introduction to Programming- Lecture 25
CS201- Introduction to Programming- Lecture 25Bilal Ahmed
 
CS201- Introduction to Programming- Lecture 24
CS201- Introduction to Programming- Lecture 24CS201- Introduction to Programming- Lecture 24
CS201- Introduction to Programming- Lecture 24Bilal Ahmed
 
CS201- Introduction to Programming- Lecture 23
CS201- Introduction to Programming- Lecture 23CS201- Introduction to Programming- Lecture 23
CS201- Introduction to Programming- Lecture 23Bilal Ahmed
 

Plus de Bilal Ahmed (20)

CS201- Introduction to Programming- Lecture 45
CS201- Introduction to Programming- Lecture 45CS201- Introduction to Programming- Lecture 45
CS201- Introduction to Programming- Lecture 45
 
CS201- Introduction to Programming- Lecture 44
CS201- Introduction to Programming- Lecture 44CS201- Introduction to Programming- Lecture 44
CS201- Introduction to Programming- Lecture 44
 
CS201- Introduction to Programming- Lecture 42
CS201- Introduction to Programming- Lecture 42CS201- Introduction to Programming- Lecture 42
CS201- Introduction to Programming- Lecture 42
 
CS201- Introduction to Programming- Lecture 41
CS201- Introduction to Programming- Lecture 41CS201- Introduction to Programming- Lecture 41
CS201- Introduction to Programming- Lecture 41
 
CS201- Introduction to Programming- Lecture 40
CS201- Introduction to Programming- Lecture 40CS201- Introduction to Programming- Lecture 40
CS201- Introduction to Programming- Lecture 40
 
CS201- Introduction to Programming- Lecture 39
CS201- Introduction to Programming- Lecture 39CS201- Introduction to Programming- Lecture 39
CS201- Introduction to Programming- Lecture 39
 
CS201- Introduction to Programming- Lecture 38
CS201- Introduction to Programming- Lecture 38CS201- Introduction to Programming- Lecture 38
CS201- Introduction to Programming- Lecture 38
 
CS201- Introduction to Programming- Lecture 37
CS201- Introduction to Programming- Lecture 37CS201- Introduction to Programming- Lecture 37
CS201- Introduction to Programming- Lecture 37
 
CS201- Introduction to Programming- Lecture 36
CS201- Introduction to Programming- Lecture 36CS201- Introduction to Programming- Lecture 36
CS201- Introduction to Programming- Lecture 36
 
CS201- Introduction to Programming- Lecture 34
CS201- Introduction to Programming- Lecture 34CS201- Introduction to Programming- Lecture 34
CS201- Introduction to Programming- Lecture 34
 
CS201- Introduction to Programming- Lecture 32
CS201- Introduction to Programming- Lecture 32CS201- Introduction to Programming- Lecture 32
CS201- Introduction to Programming- Lecture 32
 
CS201- Introduction to Programming- Lecture 31
CS201- Introduction to Programming- Lecture 31CS201- Introduction to Programming- Lecture 31
CS201- Introduction to Programming- Lecture 31
 
CS201- Introduction to Programming- Lecture 30
CS201- Introduction to Programming- Lecture 30CS201- Introduction to Programming- Lecture 30
CS201- Introduction to Programming- Lecture 30
 
CS201- Introduction to Programming- Lecture 29
CS201- Introduction to Programming- Lecture 29CS201- Introduction to Programming- Lecture 29
CS201- Introduction to Programming- Lecture 29
 
CS201- Introduction to Programming- Lecture 28
CS201- Introduction to Programming- Lecture 28CS201- Introduction to Programming- Lecture 28
CS201- Introduction to Programming- Lecture 28
 
CS201- Introduction to Programming- Lecture 27
CS201- Introduction to Programming- Lecture 27CS201- Introduction to Programming- Lecture 27
CS201- Introduction to Programming- Lecture 27
 
CS201- Introduction to Programming- Lecture 26
CS201- Introduction to Programming- Lecture 26CS201- Introduction to Programming- Lecture 26
CS201- Introduction to Programming- Lecture 26
 
CS201- Introduction to Programming- Lecture 25
CS201- Introduction to Programming- Lecture 25CS201- Introduction to Programming- Lecture 25
CS201- Introduction to Programming- Lecture 25
 
CS201- Introduction to Programming- Lecture 24
CS201- Introduction to Programming- Lecture 24CS201- Introduction to Programming- Lecture 24
CS201- Introduction to Programming- Lecture 24
 
CS201- Introduction to Programming- Lecture 23
CS201- Introduction to Programming- Lecture 23CS201- Introduction to Programming- Lecture 23
CS201- Introduction to Programming- Lecture 23
 

Dernier

1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...RKavithamani
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 

Dernier (20)

1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 

CS101- Introduction to Computing- Lecture 26

  • 1. 1 CS101 Introduction to Computing Lecture 26Arrays (Web Development Lecture 9)
  • 2. 2 During the last lecture we had a discussion on Flow Control & Loops • We discussed the concept of flow control using the “if” and “switch” structures • And also the concept behind the “while” and “for” looping structures • We also solved simple problems using flow control and loop structures
  • 3. 3 if…else --?-- switch • If the action to be taken of the value of a single variable (or a single expression), use ‘switch’ • When the action depends on the values of multiple variables (or expressions), use the ‘if...else’ structure
  • 4. 4 Compound Statements • At times, we need to put multiple statements at places where JavaScript expects only one • For those situations, JavaScript provides a way of grouping a number of statements into a single statement, called a “statement block” • This is done simply by enclosing any number of statements within curly braces, { } • NOTE: Although the statements within the block end in semicolons, the block itself doesn’t
  • 5. 5 for: Example 1 x = 1 ; while ( x < 6000 ) { document.write ( x ) ; x = x + 1 ; } for ( x = 1 ; x < 6000 ; x = x + 1 ) { document.write ( x ) ; } Initial count Condition Operation
  • 6. 6 for --?-- while • When the exact number of iterations is known, use the ‘for’ loop • When the number of iterations depend upon a condition being met, use the ‘while’ loop
  • 7. 7 ‘for’ loops become especially useful when used in conjunction with arrays We’ll find out about arrays today, and we’ll probe their usefulness as part of ‘for’ loop structures
  • 8. 8 Today’s Topic: Arrays • We will find out why we need arrays • We will become able to use arrays for solving simple problems
  • 10. 10 Array An indexed list of elements We said that a variable is a container that holds a value. Similarly, an Array can be considered a container as well, but this one can hold multiple values
  • 11. 11 Array An indexed list of elements Example: There are many ways of assigning identifiers to the following fruit strawberry fruit1 fruit[ 0 ] orange fruit2 fruit[ 1 ] apple fruit3 fruit[ 2 ] watermelon fruit4 fruit[ 3 ]
  • 12. 12 Array An indexed list of elements • fruit[ 0 ], fruit[ 1 ], fruit[ 2 ], and fruit[ 3 ] are the elements of an array • ‘fruit’ is the identifier for that array • The length of the ‘fruit’ array is 4, i.e. ‘fruit’ has four elements
  • 14. 14 Let’s now take look at one of the advantages of using arrays
  • 15. 15 var student1, student2, student3, student4 ; student1 = “Waseem” ; student2 = “Waqar” ; student3 = “Saqlain” ; student4 = “Daanish” ; document.write( student1 ) ; document.write( student2 ) ; document.write( student3 ) ; document.write( student4 ) ;
  • 16. 16 student = new Array( 4 ) ; //array declaration student[ 0 ] = “Waseem” ; student[ 1 ] = “Waqar” ; student[ 2 ] = “Saqlain” ; student[ 3 ] = “Daanish” ; for ( x = 0 ; x < 4 ; x = x + 1 ) { document.write( student[ x ] ) ; } Can you see the advantage of using arrays along with the ‘for’ loop?
  • 17. 17 Arrays in JavaScript • In JavaScript, arrays are implemented in the form of the ‘Array’ object • The key property of the ‘Array’ object is ‘length’, i.e the number of elements in an array • Two of the key ‘Array’ methods are: – reverse( ) – sort( ) • Elements of an array can be of any type; you can even have an array containing other arrays
  • 18. 18 Declaring a New Instance of the Array Object • ‘student’ is an instance of the ‘Array’ object • ‘student’ was declared such that it is of length ‘4’ • That is, student is an array having 4 elements • The four elements of the array are: ‘student[ 0 ]’, ‘student[ 1 ]’, ‘student[ 2 ]’, and ‘student[ 3 ]’
  • 19. 19 student = new Array( 4 ) This is the identifier of the new instance The ‘new’ operator creates an instance This is the parent object (or class) of the new instance Length of the new instance of ‘Array’ Pair of paren- theses The ‘assignment’ operator
  • 22. 22 All instances of an object are objects themselves!
  • 23. 23 ‘Property’ Values of the Instances May Differ
  • 24. 24 student = new Array( 4 )
  • 25. 25 Array Identifiers The naming rules for Array identifiers are the same as were discussed for variable identifiers
  • 26. 26 Assigning Values to Array Elements a[ 1 ] = 5 ; //the second element name[ 5 ] = “bhola” ; number = 5 ; name[ number ] = name[ 5 ] ; for ( x = 0 ; x < 10 ; x = x + 1 ) { y[ x ] = x * x ; }
  • 27. 27 Remember: just like C, C++ and Java, the first element of an array has an index number equal to zero
  • 28. 28 JavaScript Arrays are Heterogeneous Unlike many other popular languages, a JavaScript Array can hold elements of multiple data types, simultaneously a = new Array( 9 ) ; b = new Array( 13 ) ; b[ 0 ] = 23.7 ; b[ 1 ] = “Bhola Continental Hotel” ; b[ 2 ] = a ;
  • 29. 29 The ‘length’ Property of Arrays d = new Array ( 5 ) ; document.write( d.length ) ; ‘d’ is an instance of the ‘Array’ object ‘length’ is a property of the object ‘d’
  • 30. 30 The ‘length’ Property of Arrays x = new Array ( 10 ) ; for ( x = 0 ; x < 10 ; x = x + 1 ) { y[ x ] = x * x ; } x = new Array ( 10 ) ; for ( x = 0 ; x < x.length ; x = x + 1 ) { y[ x ] = x * x ; } What is advantage of using ‘x.length’ here instead of using the literal ‘10’?
  • 31. 31 Array Methods: sort( ) Sorts the elements in alphabetical order x = new Array ( 4 ) ; x[ 0 ] = “Waseem” ; x[ 1 ] = “Waqar” ; x[ 2 ] = “Saqlain” ; x[ 3 ] = “Shoaib” ; x.sort( ) ; for ( k = 0 ; k < x.length; k = k + 1 ) { document.write( x[ k ] + “<BR>” ) ; } Saqlain Shoaib Waqar Waseem
  • 32. 32 Were the elements sorted in ascending or descending order? What if you wanted to arrange them in the reverse order?
  • 33. 33 Array Methods: reverse( ) Reverses the order of the elements x = new Array ( 4 ) ; x[ 0 ] = “Waseem” ; x[ 1 ] = “Waqar” ; x[ 2 ] = “Saqlain” ; x[ 3 ] = “Shoaib” ; x.reverse( ) ; x.sort( ) ; for ( k = 0 ; k < x.length; k = k + 1 ) { document.write( x[ k ] + “<BR>”) ; } Saqlain Shoaib Waqar Waseem Is this the required result?
  • 34. 34 Array Methods: reverse( ) Reverses the order of the elements x = new Array ( 4 ) ; x[ 0 ] = “Waseem” ; x[ 1 ] = “Waqar” ; x[ 2 ] = “Saqlain” ; x[ 3 ] = “Shoaib” ; x.sort( ) ; x.reverse( ) ; for ( k = 0 ; k < x.length; k = k + 1 ) { document.write( x[ k ] + “<BR>”) ; } Waseem Waqar Shoaib Saqlain
  • 35. 35 Let’s Now Do a More Substantial Example Develop a Web page that prompts the user for 10 words, and then displays them in form of a list in two different ways: 1. In the order in which the words were entered 2. In a sorted order We will try to show you the complete code - the JavaScript part as well as the HTML part - for this example
  • 36. 36 Before looking at code, let’s first understand what is that code supposed to do
  • 37. 37
  • 38. 38
  • 39. 39 Pseudo Code 1. Declare the array that will be used for storing the words 2. Prompt the user and read the user input into the elements of the array 3. Now write the array to the document 4. Sort the array 5. Write the sorted array to the document
  • 40. 40 <HTML> <HEAD> <TITLE>Sort Ten Words</TITLE> <SCRIPT> words = new Array ( 10 ) ; for ( k = 0 ; k < words.length ; k = k + 1 ) { words[ k ] = window.prompt( "Enter word # " + k, "" ) ; } document.write( "UNSORTED WORDS:" + "<BR>" ) ; for ( k = 0 ; k < words.length ; k = k + 1 ) { document.write( words[ k ] + "<BR>" ) ; } words.sort( ) ; document.write( "SORTED WORDS:" + "<BR>" ) ; for ( k = 0 ; k < words.length ; k = k + 1 ) { document.write( words[ k ] + "<BR>" ) ; } </SCRIPT> </HEAD> <BODY> </BODY> </HTML>
  • 41. 41 <HTML> <HEAD> <TITLE>Sort Ten Words</TITLE> <SCRIPT> //JavaScript Code </SCRIPT> </HEAD> <BODY> </BODY> </HTML>
  • 42. 42 The next three slides show the JavaScript code that goes between the <SCRIPT>, </SCRIPT> tags
  • 43. 43 Pseudo Code 1. Declare the array that will be used for storing the words 2. Prompt the user and read the user input into the elements of the array 3. Now write the array to the document 4. Sort the array 5. Write the sorted array to the document
  • 44. 44 words = new Array ( 10 ) ; for ( k = 0 ; k < words.length ; k = k + 1 ) { words[ k ] = window.prompt( "Enter word # " + k, "" ) ; } This method is used for collecting data from the user. It can display a message and provides a field in which the user can enter data
  • 45. 45 Pseudo Code 1. Declare the array that will be used for storing the words 2. Prompt the user and read the user input into the elements of the array 3. Now write the array to the document 4. Sort the array 5. Write the sorted array to the document
  • 46. 46 document.write( "Unsorted Words:" + "<BR>" ) ; for ( k = 0 ; k < words.length ; k = k + 1 ) { document.write( words[ k ] + "<BR>" ) ; }
  • 47. 47 Pseudo Code 1. Declare the array that will be used for storing the words 2. Prompt the user and read the user input into the elements of the array 3. Now write the array to the document 4. Sort the array 5. Write the sorted array to the document
  • 48. 48 words.sort( ) ; document.write( "Sorted Words:" + "<BR>" ) ; for ( k = 0 ; k < words.length ; k = k + 1 ) { document.write( words[ k ] + "<BR>" ) ; }
  • 49. 49 Assignment #9 Build a Web page that implements the Bubble Sort algorithm discussed during the 17th lecture (Algorithms II) The numbers to be sorted will be provided to you and should be hard coded in the JavaScript code. Your page should display a button labeled “Run Bubble Sort”. When that button is clicked, the page should display the sorted list of numbers Further information on this assignment will be provide on the CS101 Web site
  • 50. 50 During Today’s Lecture … • We found out why we need arrays • We became able to use arrays for solving simple problems
  • 51. 51 Next (the 10th ) Web Dev Lecture: Functions & Variable Scope • To become familiar with some of JavaScript’s built-in functions • To be able to understand the concept of user- defined functions and their use for solving simple problems • To become familiar with the concept of local and global variables