Decision Control, For Loop and Operators
Decision Control, For Loop and Operators
When we want to execute the more than one statements in response of the condition which
is true then these types of statements are surrounded by curly braces. The effect of
wrapping which wrap the many statements as a single statement is called the compound
statement.
A block is a set of statements enclosed in braces as single statement. It is used with the
decision making or looping statements. If condition is true then whole block will be executed
otherwise whole block will be skipped.
The decision-making construct like if, else, switch executes the blocks once if condition is true
or skip the block. The looping construct repeatedly executes the block until condition is false
or loop is not terminated.
5.2 Control Structures in JavaScript JavaScript has a similar set of control structures like the
other languages such as c, c++ and Java. conditional statements are supported by if and else
nal
statements as defected :
2) else (statement) specify a block of code to be executed, if the same condition is false
3)else if (statement) specify a new condition to test, if the first condition is false
else
For example:
<html>
<head>
<script type="text/javascript">
if(a >=50 )
document.write("Number is greater");
else
</script></head></html
Nested if
The ‘else if’ Statement is used to specify a new condition if the first condition is false.
syntax:
if (condition1)
{
block of code to be executed if condition1 is true
} else if (condition2)
{
block of code to be executed if the condition1 is false and condition2 is true
} else
{
block of code to be executed if the condition1 is false and condition2 is fals
}
For example:
<html>
<head>
<script type="text/javascript">
var a= prompt("Enter a letter");
if(a=='R' || a=='r' )
{
document.write("Red colour");
}
else if(a=='P' || a=='p')
{
document.write("Pink Colour");
}
else if(a=='B' || a=='b')
{
document.write("Blue colour");
}
else
{
document.write("Colour is not in the list");
}
</script>
</head>
</html>
The switch statement
The switch statement is used to select one of many blocks of the code to be executed in a program
described below. The switch expression is evaluated once and the value of the expression is
compared with the values of each case. If there is a match found, then the associated block of code
is executed otherwise default code block is executed.
Syntax:
switch (expression)
break;
break;
For example:
<html>
<head>
<script type="text/javascript">
var week= Number(prompt("Enter week number(1-7): "));
switch(week)
{
case 1:
document.write("Monday");
break;
case 2:
document.write("Tuesday");
break;
case 3:
document.write(" Wednesday");
break;
case 4:
document.write(" Thursday");
break;
case 5:
document.write("Friday");
break;
case 6:
document.write("Saturday");
break;
case 7:
document.write(" Sunday");
break;
default:
document.write("Unkown day");
}
</script>
</head>
</html>
For Loop
The for loop is best suited when you already know the number of times
the statements should be executed. the loop executes until the condition
becomes false.
Syntax:
for(initialization; condition; increment)
{
//statements
}
When a for loop executes
1. The initializing expression is get executed and this expression usually
initializes one or more loop variables.
<!DOCTYPE html>
<html>
<body>
<p> For loop Example</p>
<script type=”text/javascript”>
for (i = 0; i < 5; i++) {
document.write("Hello Life! You are so Good" +"<br>"); }
</script>
</body>
</html>
Example explained
From the above example we can notice that
Statement 2 defines the condition for the loop to run (i must be less
than5).
statement 3 increases a value (i++) each time the code block in the loop
has beenexecuted. in JavaScript, we normally use statement 1 to initiate
the variable used in theloop (i = 0). although, this is not always the case,
JavaScript doesn’t care. statement 1is optional.
We can write the same program which was discussed in For Loop
section with the While loop. Major difference is For loop is easy to
write with one line syntax whereas in while loop the parts of loop are
written separately.
Example: This is the same example of For Loop Section but executed
with while loop rather than for loop.
<!DOCTYPE html>
<html>
<body>
<p>While loop Example</p>
<script type=”text/javascript”>
var i=0;
while( i <5)
{
document.write("Hello Life! You are so Good" +"<br>");
i++;
}
</script>
</body>
</html>
Another example
<html>
<body>
<p> While loop Example</p>
<script type=”text/javascript”>
var t = prompt("Enter Table to calculate up to 10");
x = parseInt(t);
var i=1;
while( i <= 10)
{
value = x *i
document.write( x +" * " + i + " = "+value +"<br>");
i++;
}
</script>
</body>
</html>
do While loop
This is another kind of loop and different from the for loop and the while
loop. this loop will execute the statement at least once that is the
statements inside the loopwill always get executed at least once, even if
the condition is false. the condition is checked happens after the loop
has been executed. the loop will continue to execute orwill terminate
according to on the condition as shown in figure 6.3. do while syntax:
var i = 1;
do {
alert(i);
i = i + 1;
} while (i<10)
Example: This example shows the difference between while and do-while.
<!DOCTYPE html>
<html>
<body>
<p> Do-While loop Example</p>
<script
type=”text/javascript”>
var i=1;
while( i < 0)
{
document.write("Hello Life! You are so Good" +"<br>"); i++;}
</script>
</body></html>
We use the condition which is false initially. If you use this condition in
while loop it won’t be executed but in do-while it will execute at least
once.
Output:
Hello Life! You are so Good
Example: This is the same example of For Loop Section but executed
with while loop rather than for loop.
Example: Calculate the factorial of a value entered by the user.
<html>
<body>
<p> Do-While loop Example</p>
<script>
var d = prompt ("Enter number to calculate factorial"); n =
parseInt(d); var f=1; var i=1; do
{ f = f* i;
i++;
} while( i <= n);
document.write("factorial of value " + n + " is " + f );
</script>
</body>
</html>
Section-3: Operators
3.1 Operators
An operator is a symbol that is used to perform an operation. in
this section we will learn how to use JavaScript operators.
1) Assignment Operators
Assignment operators help in assigning values to a variable. the
following table shows
how assignment operators work in JavaScript.here we consider two
variables x and y assuming x=90 and y=20. upon execution,
theassignment operator will generate the following results.
operator example Same as result
= x=y x=90
4) Logical Operators
There are following logical operators supported by JavaScript language.
assume variable a hold 10 and variable b holds 20 then:
operator description example
called bitwise shift left operator. it shift all the bits in its first
operand to the left by the number of places specified in the
second operand. New bits are filled with zeros. shifting a value
left by one position is equivalent to multiplying by 2, shifting
two positions is equivalent to multiplying by 4 so on.
<< (a << 1) is 4.
called bitwise shift right with sign operator. it shift all the bits
in its first operand to the right by the number of places
specified in the second operand. The bits filled in
on the left depend on the sign bit of the original operand, in
order to preserve the sign of the result. If the first operand is
positive, the result has zeros placed in the high bits; if the first
operand is negative, the result has ones placed in the high bits.
>> shifting a value right one place is equivalent to dividing by 2 (a >> 1) is 1
(discarding the remainder), shifting right two places is
equivalent to integer division by 4, and soon.
6) Assignment Operators
There are following assignment operators supported by JavaScript
language:
operator description example
7) Special Operator
Conditional operator (? :)
This operator first evaluates an expression for a true or false value and
then execute one of the two given statements depending upon the result
of the evaluation.
operator description example
typeof operator
the typeof is a unary operator that is placed before a single operand,
which can be of any type. its value is a string indicating the data type of
the operand. the type of operator evaluates to “number”, “string”, or
“Boolean” if its operand is a number, string, or Boolean value and
returns true or false based on the evaluation.
here is the list of return values for the type of operator:
number “number”
string “string”
boolean “boolean”
object “object”
Function “function”
Undefined “undefined”
null “object”
parseInt
(“40.33”)
returns 40
parseInt (“19
Covid”)
returns 19
parseInt
(“Covid 19”)
returns NaN
Converting String
String() method is used to convert any type to string value whereas
possible by accepting one argument.
• If argument is integer type then it converted to string. For
example, string (40) converted to “40”
• If argument is Boolean type then string(false) is converted to
“false”.
• If argument is null then string(null) is converted to “null”.
Converting Boolean
Boolean() method convert any type to Boolean type which is supported.
Q5) What is the difference in the outputs that you get when you use the following
operators =; = = ;
Q.9: explain what you understand by modulo operator.