Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Assignment Operator: a = q; means value of q is assigned into a variable deleting

the previous value of a.


Assignment Operator is used to assign values into a
variable. Apart from = there are compound assignment Now a* = 3; is equivalent to a = a*3; and like that all other
operators as follows- compound assignment operator behaves.

*= /= %=

+= -= <<=

>>= >>>= &=

^= |=

IT & ITES : COPA (NSQF Level - 4) - Related Theory for Exercise 2.1.96B 13

Copyright Free under CC BY Licence


IT & ITES Related Theory for Exercise 2.1.97A & 2.1.97B
COPA - JavaScript and creating Web page

Control statements, Loops and Popup boxes in JavaScript


Objectives : At the end of this lesson you shall be able to
• explain control statements
• discuss about various Loops
• explain the uses of Popup boxes.

Control Statements: When we write code for a particular block of code to be executed if the condition is false
program, we sometimes takes various decisions for
}
executing different action. These can be done through
conditional/control statements. Example 2
In JavaScript we have the following conditional statements:
If the time is less than 18:00, create a "Good day" greeting,
otherwise "Good evening":
Use if to specify a block of code to be executed, if a
specified condition is true if (time < 18) {
greeting = "Good day";
Use else to specify a block of code to be executed, if the
same condition is false } else {
greeting = "Good evening";
Use else if to specify a new condition to test, if the first
condition is false }
The result of greeting will be:
Use switch to specify many alternative blocks of code to
be executed. Good day

The if Statement The else if Statement

Use the if statement to specify a block of JavaScript code Use the else if statement to specify a new condition if the
to be executed if a condition is true. first condition is false.
Syntax Syntax
If (condition) { if (condition1) {
block of code to be executed if the condition is true block of code to be executed if condition1 is true
} } else if (condition2) {
block of code to be executed if the condition1 is false
Example 1
and condition2 is true
Make a "Good day" greeting if the time is less than 18:00: } else {
if (time < 18) { block of code to be executed if the condition1 is false
and condition2 is false
greeting = "Good day";
}
}
The result of greeting will be: Example 3
Good day
If time is less than 10:00, create a "Good morning"
greeting, if not, but time is less than 18:00, create a "Good
The else Statement
day" greeting, otherwise a "Good evening":
Use the else statement to specify a block of code to be
if (time < 10) {
executed if the condition is false.
greeting = "Good morning";
if (condition) {
} else if (time < 18) {
block of code to be executed if the condition is true
greeting = "Good day";
} else {
} else {

14

Copyright Free under CC BY Licence


greeting = "Good evening"; case 4:
} day = "Thursday";
The result of x will be: break;
Good day case 5:
day = "Friday";
The JavaScript Switch Statement
break;
Use the switch statement to select one of many blocks of
case 6:
code to be executed.
day = "Saturday";
Syntax
break;
switch(expression) {
}
case n1:
The result of day will be:
code block
Tuesday
break;
case n2: The break Keyword
code block
When the JavaScript code interpreter reaches a break
break; keyword, it breaks out of the switch block.
default: This will stop the execution of more execution of code
and/or case testing inside the block.
default code block
} The default Keyword

This is how it works: The default keyword specifies the code to run if there is no
case match:
• The switch expression is evaluated once.
Example 5
• The value of the expression is compared with the values
of each case.
If today is neither Saturday nor Sunday, write a default
• If there is a match, the associated block of code is message:
executed.
switch (new Date().getDay()) {
Example 4
case 6:
Use today's weekday number to calculate weekday name: text = "Today is Saturday";
(Sunday=0, Monday=1, Tuesday=2, ...)
break;
switch (new Date().getDay()) {
case 0:
case 0:
text = "Today is Sunday";
day = "Sunday";
break;
break;
default:
case 1:
text = "Looking forward to the Weekend";
day = "Monday";
}
break;
The result of text will be:
case 2:
Looking forward to the Weekend
day = "Tuesday";
Common Code and Fall-Through
break;
case 3: Sometimes, in a switch block, you will want different cases
to use the same code, or fall-through to a common default.
day = "Wednesday";
Note from the next example, that cases can share the
break;
same code block and that the default case does not have
to be the last case in a switch block:

IT & ITES : COPA (NSQF Level - 4) - Related Theory for Exercise 2.1.97A & 2.1.97B 15

Copyright Free under CC BY Licence


Example 6 The For Loop

switch (new Date().getDay()) { The for loop is often the tool you will use when you want to
create a loop.
case 1:
case 2: The for loop has the following syntax:
case 3:
for (statement 1; statement 2; statement 3) {
default:
code block to be executed
text = "Weekend is coming";
break; }
case 4:
Statement 1 is executed before the loop (the code block)
case 5: starts. It is called Initialisation Part
text = "Weekend is soon";
Statement 2 defines the condition for running the loop (the
break; code block).It is called condition part.
case 0:
Statement 3 is executed each time after the loop (the
case 6: code block) has been executed. It is called increment/
decrement part.
text = "Now in Weekend";
} Example 7

JavaScript Loops for (i = 0; i < 5; i++) {

Loops are handy, if you want to run the same code over text += "The number is " + i + "<br>";
and over again, each time with a different value.
}
Often this is the case when working with arrays:
Instead of writing: From the example above, you can read:
text += train[0] + "<br>"; Statement 1 sets a variable before the loop starts
text += train [1] + "<br>"; (var i = 0).
text += train [2] + "<br>"; Statement 2 defines the condition for the loop to run
text += train [3] + "<br>"; (i must be less than 5).
text += train [4] + "<br>"; Statement 3 increases a value (i++) each time the code
text += train [5] + "<br>"; block in the loop has been executed.
You can write:
Initialisation Part
for (i = 0; i < train.length; i++) {
Normally you will use statement 1 to initiate the variable
text += train [i] + "<br>";
used in the loop (var i = 0).
} This is not always the case, JavaScript doesn't care.
Statement 1 is optional.
Different Kinds of Loops
You can initiate many values in statement 1 (separated
JavaScript supports different kinds of loops:
by comma):
• for - loops through a block of code a number of times
Example 8
• for/in - loops through the properties of an object
for (i = 0, len = train.length, text = ""; i < len; i++) {
• while - loops through a block of code while a specified
condition is true
text += train [i] + "<br>";
• do/while - also loops through a block of code while a
specified condition is true }

And you can omit statement 1 (like when your values are
set before the loop starts):

16 IT & ITES : COPA (NSQF Level - 4) - Related Theory for Exercise 2.1.97A & 2.1.97B

Copyright Free under CC BY Licence


Example 9 text += person[x];
}
var i = 2;
var len = train.length; While Loop
var text = "";
The while loop loops through a block of code as long as a
for (; i < len; i++) { specified condition is true.
text += train [i] + "<br>"; Syntax
} while (condition) {
code block to be executed
Condition Part
}
Often statement 2 is used to evaluate the condition of the
initial variable. In the following example, the code in the loop will run, over
and over again, as long as a variable (i) is less than 10:
This is not always the case, JavaScript doesn't care.
Statement 2 is also optional. Example 11

If statement 2 returns true, the loop will start over again, if while (i < 10) {
it returns false, the loop will end.
text += "The number is " + i;
If you omit statement 2, you must provide a break inside i++;
the loop. Otherwise the loop will never end. This will crash
}
your browser. Read about breaks in a later chapter of this
tutorial. If you forget to increase the variable used in the condition,
the loop will never end. This will crash your browser.
Increment/Decrement Part
The Do/While Loop
Often statement 3 increases the initial variable.
The do/while loop is a variant of the while loop. This loop
This is not always the case, JavaScript doesn't care, and
will execute the code block once, before checking if the
statement 3 is optional.
condition is true, then it will repeat the loop as long as the
condition is true.
Statement 3 can do anything like negative increment (i--),
or larger increment (i = i + 15), or anything else. Syntax

Statement 3 can also be omitted (like when you increment do {


your values inside the loop):
code block to be executed
Example 10 }
while (condition);
var i = 0;
len = train.length; The example below uses a do/while loop. The loop will
always be executed at least once, even if the condition is
for (; i < len; ) {
false, because the code block is executed before the
text += train [i] + "<br>"; condition is tested:
i++;
Example 12
}
do {
For/In Loop
text += "The number is " + i;
The JavaScript for/in statement loops through the properties i++;
of an object:
}
var person = {fname:"Raja", lname:"Sen", age:35}; while (i < 10);
var text = ""; Do not forget to increase the variable used in the condition,
otherwise the loop will never end!
var x;
for (x in person) {
IT & ITES : COPA (NSQF Level - 4) - Related Theory for Exercise 2.1.97A & 2.1.97B 17

Copyright Free under CC BY Licence


Comparing For and While Popup Boxes
JavaScript has three kind of popup boxes. They are
If you have read the previous chapter, about the for loop,
you will discover that a while loop is much the same as a
1 Alert box
for loop, with statement 1 and statement 3 omitted.
2 Confirm box and
The loop in this example uses a for loop to collect the car
names from the train array: 3 Prompt box.

Example 13 Alert Box

train = ["Duronto","Satabdi","Garib Rath","Rajdhani"]; An alert box is often used if you want to make sure
information comes through to the user. When an alert box
var i = 0;
pops up, the user will have to click “OK” to proceed.
var text = "";
Syntax
for (;train[i];) {
text += train[i] + "<br>"; window.alert(“sometext”);
i++;
Note: The window.alert() method can be written
} without the window prefix.

The loop in this example uses a while loop to collect the Example 15
car names from the train array:
alert (“Welcome to Java Script Coding!;)
train = ["Duronto","Satabdi","Garib Rath","Rajdhani"];
The result is shown in Fig 1.
var i = 0; Fig 1
var text = "";
while (train[i]) {
text += train[i] + "<br>";
i++;
}

The Break Statement in Loop Confirm Box

Break statement is used to terminate a loop before its A confirm box is often used to verify or accept
completion. It saves machine time for not iterating a loop something.When a confirm box pops up, the user will have
uselessly. to click either “OK” or “Cancel” to proceed.If the user clicks
“OK”, the box returns true. If the user clicks “Cancel”, the
For example: In linear search, if we find the item then we box returns false.
can break the loop as no point of runnign it unnecessary. Syntax

Example 14 window.confirm(“sometext”);

for(i=0;i<l;i++ { Note: The window.confirm() method can be


written without the window prefix.
if(arr[i]==item) {
alert("Found at :"+i); Example 16

fl=1; if (confirm(“Click a button!”))


break; {
} txt = “ You clicked OK!”;
if(fl==0) alert("Not Found"); }

Here, if the item is found, loop breaks and CPU time is else
saved. {
txt = “You clicked Cancel!”;

}
18 IT & ITES : COPA (NSQF Level - 4) - Related Theory for Exercise 2.1.97A & 2.1.97B

Copyright Free under CC BY Licence

You might also like