Download Looping in Javascript

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Addition wikipedia , lookup

Mathematics of radio engineering wikipedia , lookup

Transcript
Class 9 Choose the correct answer:
1.
Which one of the following is a looping statement ?
a) if
b) break
c) continue
d) for
2.
A sequence of instructions that may be repeatedly executed is
a) Loop
b) if … else
c) switch
d) None of these
3.
This loop provides specific locations for the three statements that comprise the loop
a) while
b) for
c) do . . . whle
d) None of these
4.
The statement that skips the remaining code in a loop is
a) break
b) switch
c) continue
d) all of these
5.
A statement that terminates a loop is
a) while
b) for
c) break
d) none of these
6.
How many times the following loop will be executed ? for(i=1;i<6;i++)
a) 1
b) 5
c) 6
d) 7
7.
__________ is called as exit-check loop.
a) do . . . while
b) while
c) for
d) if . . . else
8.
How many times will the given loop be executed ?
for(I=20;I>=5;I-=6)
document.write(“I=” +I)
a) 1
b) 2
c) 3
d) 4
Fill in the Blanks :
1.
2.
3.
4.
5.
6.
The statement(s) will be executed repeatedly as long as the condition is true.
The parenthesis following the keyword for contains 3 expressions separated by semicolons.
In do . . . while loop, the body of the loop is executed and then the condition is checked.
The while loop is repeated when the condition is true.
The loop that never ends is called a/an infinite loop.
The for and while loops are called entry-controlled loops.
2 mark questions:
1.
Define loop.
A loop is a sequence of instructions that may be repeatedly executed. The loop statement contains that
determines when the loop will terminate.
2.
3.
4.
5.
6.
7.
8.
What are the three types of loops available in JavaScript ?
Three types of loops available in JavaScript are :
 while loop
 do....while loop
 for loop
The code that is executed repeatedly is called the loop body.
Write the general format of the while loop.
initialization
while(condition)
{
loop body
increment/decrement
}
Write the general format of for loop.
for(initialization;condition;increment/decrement)
{
loop body
}
Write the general format of do . . . while loop.
initialization
do
{
loop body
increment/decrement
}while(condition)
State the working of the for loop?
 Firstly, the initialization expression is executed
 Then then test expression is checked. If it is true ,the loop continues otherwise the loop ends and
the body of the loop is skipped (not executed)
 Finally ,whatever is specified in the increment/decrement expression is executed and the loop
returns to step 2
What is the use of continue statement?
The continue statement loop skips the remaining code of the current iteration and forces the loop to reevaluate the looping-condition for the next iteration of the loop.
Write the following code using for loop.
var i=10;
while(i>=1)
{
document.writeln(i);
i=i-1;
}
var i
for(i=10;i>=1;i--)
{
document.writeln(i);
}
9.
Write the following code using do . . . while loop:
for(var i=1;i<=100;i=i+2)
document.write(i+ “<br>”)
10.
var i=1
do
{
document.write(i+ “<br>”)
i+=2
}while(i<=100)
What will be the output of the following code ?
var i=1;S=0;
while(i<=10)
{
Sum = 25
S=S+i;
i=i+2;
}
document.write(“Sum = “ +S);
Also, write how many times the loop will be executed.
5 mark questions:
1.
2.
3.
Explain for loop in JavaScript.
Explain while loop in JavaScript.
Explain do … while loop in JavaScript.
Lab Exercises
1.
Write a JavaScript code to accept a number from the user and display its factorial using any looping
statement. If the user enters a negative number, the loop should terminate and print a message ‘Please
enter a Positive Number’.
<HTML> <HEAD> <TITLE>Factorial</TITLE> </HEAD> <BODY> <H1>Factorial</H1> <SCRIPT Language="Javascript" TYPE="text/javascript"> var i=1 var n var fact=1 n=prompt("Enter N Value:","0") if(n<0) document.write("Please enter a Positive Number") else { while(i<=n) { fact=fact*i i++ } document.write("Factorial=" +fact) } </SCRIPT> </BODY> </HTML> 2.
Write a JavaScript code to display even numbers from 1 to 100 using any looping statement.
<HTML> <HEAD> <TITLE>Javascript ‐ Even Numbers</TITLE> </HEAD> <BODY> <H1>Even Numbers</H1> <SCRIPT Language="Javascript" TYPE="text/javascript"> var i for(i=0;i<=100;i+=2) { document.write(i+ "<br>" ); } </SCRIPT> </BODY> </HTML> 3.
Write a JavaScript code to find the sum of numbers between 1 to 20 when incremented by 3.
(S=1+4+7+10+13+16+19) Use while loop for doing this.
<HTML> <HEAD> <TITLE>Sum of Series</TITLE></HEAD> <BODY> <H1>Sum of Series</H1> <SCRIPT Language="Javascript" TYPE="text/javascript"> var sum=0 var i=1 while(i<=20) { sum=sum+i i+=3 } document.write("Sum of Series = " +sum) </SCRIPT> </BODY> </HTML> 4.
Write a JavaScript code to print all the odd numbers from 15 backwards to 3.
<HTML> <HEAD> <TITLE>Javascript ‐ Odd Numbers</TITLE> </HEAD> <BODY> <H1>Odd Numbers</H1> <SCRIPT Language="Javascript" TYPE="text/javascript"> var i for(i=15;i>=3;i‐=2) { document.write(i+ "<br>" ); } </SCRIPT> </BODY> </HTML> 5.
Write a JavaScript code to print tables of numbers from 1 to 10.
<HTML> <HEAD> <TITLE>Multiplication Table</TITLE> </HEAD> <BODY> <H1>Multiplication Table</H1> <SCRIPT Language="Javascript" TYPE="text/javascript"> var t var i=1 t=prompt("What Table:","0") while(i<=10) { document.write(i+ " X " +t+ " = " +i*t+ "<br>") i++ } </SCRIPT> </BODY> </HTML>