KEMBAR78
Javascript 3 | PDF | Control Flow | Computer Programming
0% found this document useful (0 votes)
2 views25 pages

Javascript 3

Uploaded by

Srëdha Sp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views25 pages

Javascript 3

Uploaded by

Srëdha Sp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 25

Javascript-Part 3

while ( student <= 10 )


{
result = window.prompt( "Enter result (1=pass,2=fail)", "0" );
if ( result == "1" )
passes = passes + 1;
else
failures = failures + 1;
student=student+1;
}
 Note that the if…else statement in the loop tests only
whether the exam result was 1; it assumes that all other
exam results are 2.
 variable = variable operator expression;
 JavaScript provides several additional assignment

operators (called compound assignment operators) for


abbreviating assignment expressions.
 c = c + 3; can be abbreviated with the addition assignment

operator, +=, as
c+ =3;
variable operator = expression;
 JavaScript provides the unary increment operator (++) and
decrement operator (--)
 If an increment or decrement operator is placed before a variable,
it’s referred to as the preincrement or predecrement
operator,respectively. If an increment or decrement operator is
placed after a variable, it’s referred to as the postincrement or
postdecrement operator, respectively.
 Preincrementing (or predecrementing) a variable causes the script to
increment (decrement) the variable by 1, then use the new value of
the variable in the expression in which it appears.
 Postincrementing (postdecrementing) the variable causes the script
to use the current value of the variable in the expression in which it
appears, then increment (decrement)
 the variable by 1.
 c = 5;
document.writeln("<h3>Postincrementing</h3>" );
document.writeln(c ); // prints 5
document.writeln(c++ ); //prints 5 and then increments
document.writeln(c); // prints 6
****************************************
 c = 5;

document.writeln("<h3>Preincrementing</h3>" );
document.writeln(c ); // prints 5
document.writeln(++c ); //prints 6
document.writeln(c); // prints 6
Operator Associativity
++ -- Right to left
* / % Left to right
+ - Left to right

< <= > >= Left to right

== != === !== Left to right

?: Right to left
= += -= *= /= %= Right to left
 Counter-controlled repetition requires:
 1. The name of a control variable (or loop counter).
 2. The initial value of the control variable.
 3. The increment (or decrement) by which the control
variable is modified each time
 through the loop (also known as each iteration of the loop).
 4. The condition that tests for the final value of the control
variable to determine whether looping should continue.
<head>
<script type=“text/javascript”>
for ( var counter = 1; counter <= 7; ++counter )
document.write(“Hello”);
</script>
</head>
 When the for statement begins executing the control variable
counter is declared and initialized to 1.
 Next, the loop-continuation condition, counter <= 7, is checked.

The condition contains the final value of the control variable.


 The initial value of counter is 1.

 Therefore, the condition is satisfied (i.e., true), so the body

statement writes a paragraph element in the body of the HTML5


document.
 Then, variable counter is incremented in the expression ++counter

and the loop continues execution with the loop-continuation test


for ( initialization; loopContinuationTest; increment )
 statements
for ( var j = x; j <= 4 * x * y; j += y / x ).
for ( var i = 100; i >= 1; --i )
for ( var i = 7; i <= 77; i += 7 )
<head>
<script type=“text/javascript”>
var sum = 0;
for ( var number = 2; number <= 100; number += 2 )
sum=sum+number;
document.write(“Sum is “,+sum);
</script>
</head>
Program to compute a = p(1+r) n
 <style type = "text/css">

table { width: 300px;}


table, td, th { border: 1px solid black; padding: 4px; }
th { text-align: left;color: white;background-color: blue; }
tr.oddrow { background-color: white; }
tr.evenrow { background-color: grey; }
</style>
<script type=“text/javascript”>
var amount;
var principal=1000.00;
var rate=0.05;
 document.writeln("<table>" ); // begin the table
document.writeln("<caption>Calculating Compound
Interest</caption>" );
document.writeln( "<tr><th>Year</th>" ); // year column heading
document.writeln("<th>Amount on deposit</th>" ); // amount column
document.writeln( "</tr> <tbody>" );
for ( var year = 1; year <= 10; year ++)
{
amount = principal * Math.pow(1.0+rate,year) ;
if ( year %2 !=0)
document.writeln( "<tr class='oddrow'> <td>" + year +
"</td><td>" + amount.toFixed(2) + "</td></tr>" );
else
document.writeln( "<tr class=‘evenrow’><td>" + year + "</td><td>"
+ amount.toFixed(2) + "</td></tr>" );
}
document.writeln( "</tbody></table>" );
</script>
</head>

Note: pow (x,y)= x y


 An algorithm will contain a series of decisions in
which a variable or expression is tested separately for
each of the values it may assume, and different actions
are taken for each value.
 JavaScript provides the switch multiple-selection

statement to handle such decision making.


<head>
<script>
var starttag;
var endtag;
var validinput=true;
var choice = window.prompt( "Select a list style:\n" +"1
(numbered), 2 (lettered)”);
switch ( choice )
{
case "1":
startTag = "<ol>";
endTag = "</ol>";
listType = "<h1>Numbered List</h1>";
break;
case “2”:
startTag = "<ol style = 'list-style-type: upper-alpha'>";
endTag = "</ol>";
listType = "<h1>Lettered List</h1>";
break;
default:
validinput=false;
break;
}
if ( validinput === true )
{
document.writeln( listType + startTag );
for ( var i = 1; i <= 3; ++i )
document.writeln( "<li>List item " + i + "</li>" );
document.writeln(endTag);
}
</script>
</head>
 The do…while statement tests the loop-continuation condition
after the loop body executes—therefore, the loop body always
executes at least once.
<script type=“text/javascript”>
var counter=1;
do{
document.writeln( "<h“ + counter + “>This is an h" + counter + "
level head" + "</h" +counter + ">" );
++counter;
}while(counter<=6);
</script>
 The break statement, when executed in a while, for,
do…while or switch statement, causes immediate exit
from the statement.
 for ( var count = 1; count <= 10; ++count )
{
if ( count == 5 )
break;
document.writeln( count + " " );
} //end for
document.writeln("<p>Broke out of loop at count = " + count
+ "</p>" );
 The continue statement, when executed in a while, for
or do…while statement, skips the remaining statements
in the body of the statement and proceeds with the next
iteration of the loop.
for ( var count = 1; count <= 10; ++count )
{
if ( count == 5)
continue; // skip remaining loop code only if count == 5
document.writeln( count + " " );
}
 JavaScript provides logical operators that can be used to
form more complex conditions by combining simple
conditions.
 The logical operators are && (logical AND), || (logical OR)

and ! (logical NOT, also called logical negation).


if ( gender == 1 && age >= 65 )
++seniorFemales;
if ( semesterAverage >= 90 || finalExam >= 90 )
document.writeln( "Student grade is A" );
if ( ! ( grade == sentinelValue ) )
document.writeln( "The next grade is " + grade );

You might also like