Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
45 views

11CA 15 Control Structures in Java Script V02

The document discusses control structures in JavaScript including conditional statements like if/else and switch case statements as well as looping statements like for, while, and do-while loops. It provides examples and explanations of these statements and how they can be used.

Uploaded by

kms195kds2007
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views

11CA 15 Control Structures in Java Script V02

The document discusses control structures in JavaScript including conditional statements like if/else and switch case statements as well as looping statements like for, while, and do-while loops. It provides examples and explanations of these statements and how they can be used.

Uploaded by

kms195kds2007
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

11th Standard / Computer Applications

Chapter 15 : Control Structures in Java Scripts

Part-I
Choose the correct answers:
1) Which conditional statement is used to transfer control from current statement to
another statement?
(a) Branching (b) Sequencing (c) Looping (d) Iterating
2) _______ statement can be used as alterative to if-else statement.
(a) While (b) If (c) Else-if (d) Switch
3) Which statement in switch case is used to exit the statement once the appropriate choice
is found?
(a) Exit (b) Default (c) Case (d) Break
4) Which of the following is not a looping statement?
(a) Switch (b) While (c) Do-While (d) For
5) Which part of the loop statement determines the number of times, the loop will be
iterated? (MOT)
(a) First (b) Second (c) Third (d) Final
6) Which of the following is not a branching statement?
(a) Loop (b) If-else (c) Switch (d) For
7) What will be the output for the following snippet:
for (var n=0; n<10; n+1)
{
if (n==3)
{
break;
}
document write (n+"<br>");
}
(a) 0 1 2 (b) 0 1 2 3 (c) 0 1 2 3 4 (d) 0, 1, 3
8) In which loop the condition is evaluated, before executing a statement?
(a) While (b) Do while (c) Break (d) Continue
9) The _______ statement is especially useful when testing all the possible results of an
expression.
(a) While (b) Do while (c) Switch (d) If
10) In the _____ loop, body of the loop is always executed at least once before the condition
is evaluated
(a) For (b) While (c) If (d) Do while
11) <script type = "text / javascript">
x = 6 + "3";
document write (x);
<script> what will be the output?

(a) 6 (b) 9 (c) 63 (d) Error

Part – II
Very Short Answers
1) What are the different types of control statement used in JavaScript?
Branching or Selection statement
Looping or Repetitive statement

11CA /15 Control Structures /Page 1 of 6


2) What is meant by conditional statements in JavaScript?
Conditional statements are based on a condition. They execute or skip based on the Boolean
values
3) List out the various branching statements in JavaScript?
If, if…else, else if, switch
4) Write the general syntax for switch statement
switch ( expression )
{
case label 1:
statements 1;
break ;
case label 2:
statements 2;
break ;
. . .
. . .
default :
statements;
}

5) Differentiate the break and continue statement.


Break Continue
Used to terminate the loop Used to continue the next iteration in the loop
Control transferred outside the loop Control remains in the same loop
Used in switch and looping statements Used in looping statements

Part-III
Short Answers
1) What is if statement and write its types.
The “if” statement is a branching statement which transfer the control based on a condition
Types : simple “if”, “if…else”.
Simple “if”
Syntax : if(condition)
{
Statements 1 [ True block ] ;
}
 When the condition returns “True”, then the statements under true block will be
executed.
 When the condition returns “False”, the control will move to next statement
Eg : if(mark>=50)
alert(“pass”)
If…else
Syntax : if(condition)
{
statements 1 [True Block ];
}
else
{
statements 2 [ False block ];
}

11CA /15 Control Structures /Page 2 of 6


 When the condition returns “True”, then the statements under “true block” will be
executed.
 When the condition returns “False”, then the statements under “false block” will be
executed
Eg: : if(mark>=50)
{ alert(“Pass”); }
else
{ alert(“Fail”); }

2) Write the syntax for else-if statement.


if ( condition 1)
{ statements block 1 }
else if ( condition 2 )
{ statements block 2 }
else if ( condition 3 )
{ statements block 3 }
…..
else
{ statements block n; }

3) What is called a loop and what are its types?


 Loops will execute set of statements based on a condition.
 Entry check loop : the condition is checked at the beginning of the loop
Eg : “for” loop, “ while” loop ,
 Exit check loop : the condition is checked at the end of the loop
Eg : “do-while” loop

4) Differentiate between while and do while statements


While loop Do-while loop
Condition checked at the beginning Condition checked at the end
If condition fails at the beginning, the loop If condition fails at the beginning , the loop
will not be executed will be executed at-least once

5) What message will be displayed, if the input for age is given as 20, for the following
snippet.
if (age> = 18 )
{ alert ("you are eligible to get Driving licence"); }
else
{ alert ("you are not eligible to get driving licence"); }
OUTPUT :
You are eligible to get Driving Licence

Part –IV
Explain in Detail
1) Explain for loop with example
“for” loop will execute a number of times. It is an entry check loop
The syntax
for ( initialization ; condition ; increment/decrement )
{
Body of the loop;
}

11CA /15 Control Structures /Page 3 of 6


 The first part of the loop initialize a variable which is also called as control variable.
In most case the control variable is declared as well as initialized.
 The second part is the conditional statement that determines how many times the
loop will be iterated.
 The third and final part determines how the value of control variable is changed
(Incremented/Decremented)
The condition is checked at the beginning, when the condition is “true” the body of
the “for loop” will be executed, then the control will move to the “updating” variable.
Eg :
for( var n=0;n<=5;n++)
{ document.write(n); }
OUTPUT : 0 1 2 3 4 5

2) Explain switch case statement with example


 Switch statement is an alternative for “if…else” statement
 It checks with multiple values for an expression
switch(expression)
{
case label1:
statements1;
break;
case label2:
statements2;
break;
....
....
....
case labeln;
statements - N;
break;
default:
statements;
}
 When the expression returns a value, it will be checked with the “case labels”.
 If it matches, then the statements under that case will be executed upto the “break”
statement.
 If none of the cases matches, then the statements under “default” will be executed.
Example :
<Html>
<Body>
<script language="javascript" type="text/javascript">
var num;
num=prompt("Enter a number ");
switch(num)
{
case "1":
alert("ONE");
break;
case "2":
alert("TWO");
break;

11CA /15 Control Structures /Page 4 of 6


case "3":
alert("THREE");
break;
default:
alert("Enter between 1 and 3")
}
</script> </Body> </Html>

3) Write the output for the following program

<Html>
<Head>
<Title> for statement</title>
<Head>
<Body>
<script language= "javascript" type = "text/javascript")>
var no1=prompt("please enter table you want:", "0" );
document.write("<h2> multiplication for your need </h2>");
for(var no2=0; no2<=10; no2++)
{ document.write(no1+ "x" + no2+ "=" + no1*no2+ "<br>");}
</script>
</body>
</Html>

Output :

multiplication for your need


3x0=0
3x1=3
3x2=6
3x3=9
3x4=12
3x5=15
3x6=18
3x7=21
3x8=24
3x9=27
3x10=30

11CA /15 Control Structures /Page 5 of 6


4) Write a Java Script program using while statement to display 10 numbers.

<Html>
<Head>
<Title> while</title>
<Head>
<Body>
<script language= "javascript" type = "text/javascript")>
var n=1;
while(n<=10)
{
document.write(n);
n++;
}
</script>
</body>
</Html>

Output : 1 2 3 4 5 6 7 8 9 10

11CA /15 Control Structures /Page 6 of 6

You might also like