Practical-2 1
Practical-2 1
3. JavaScript Controls
JS Conditions: If , Else If & Switch, JS Loop: For, For In, While & Do While, JS Break,
JS Type Conversion, JS Errors: Try, Catch & Throw
JavaScript If-else
The JavaScript if-else statement is used to execute the code whether condition is true or
false. There are three forms of if statement in JavaScript.
1. If Statement
2. If else statement
3. if else if statement
JavaScript If statement
It evaluates the content only if expression is true. The signature of JavaScript if statement is
given below.
if(expression){
//content to be evaluated
}
Example:
AIM: Write a javascript code to check number is positive or not.
File: if_demo.js
var x = prompt("Enter value that you want to check", "");
if(x>=0)
{
document.writeln("Positive");
}
document.writeln("Done");
Output:
if(expression){
//content to be evaluated if condition is true
}
else{
//content to be evaluated if condition is false
}
if(x % 2 == 1)
{
document.write("Number is odd");
}
else
{
document.write("Number is even");
}
OUTPUT:
if(expression1){
//content to be evaluated if expression1 is true
}
else if(expression2){
//content to be evaluated if expression2 is true
}
else if(expression3){
//content to be evaluated if expression3 is true
}
else{
//content to be evaluated if no expression is true
}
else
{
document.writeln("Max value is "+z);
}
OUTPUT:
JavaScript Switch
The JavaScript switch statement is used to execute one code from multiple expressions. It is
just like else if statement. But it is convenient than if..else..if because it can be used with
numbers, characters etc.
switch(expression){
case value1:
code to be executed;
break;
case value2:
code to be executed;
break;
......
default:
code to be executed if above values are not matched;
}
var x = prompt("Enter 1 for sum \nEnter 2 for Sub\nEnter 3 for MUL \nEnter 4 for div", "");
x = parseInt(x);
switch(x)
{
case 1:
ans = a + b;
break;
case 2:
ans = a - b;
break;
case 3:
ans = a * b;
break;
case 4:
ans = a / b;
break;
default:
document.write("You have entered wrong value")
}
document.write("Answer = "+ans);
OUTPUT:
JavaScript Loops
The JavaScript loops are used to iterate the piece of code using for, while, do while or for-in
loops. It makes the code compact. It is mostly used in array.
There are four types of loops in JavaScript.
1. for loop
2. while loop
3. do-while loop
4. for-in loop
AIM: Write a JavaScript code to display message 5 times using for loop.
var i;
for(i=0;i<5;i++)
{
document.writeln("Hello "+i);
}
OUTPUT:
while (condition)
{
code to be executed
}
var i=11;
while(i<=15)
{
document.writeln(i);
i++;
}
OUTPUT:
do{
code to be executed
}while (condition);
var i=11;
do
{
document.writeln(i);
i++;
}while(i<=15);
OUTPUT:
JS Break
The break statement "jumps out" of a loop.
Example:
for(i=0;i<10;i++)
{
if(i==3)
{
break;
}
document.writeln("Hello "+i);
}
OUTPUT:
Example:
for(i=0;i<10;i++)
{
if(i==3)
{
continue;
}
document.writeln("The number is "+i);
}
OUTPUT:
JS Type Conversion:
Type conversion is the ability to convert from one data type to another. TypeScript provides
us with built-in functions to perform type conversion.
JavaScript variables can be converted to a new variable and another data type:
Example
Example
x.toString()
(123).toString()
(100 + 23).toString()
In the similar way, Boolean and date type of data can also be converted into string.
The try statement lets you test a block of code for errors.
The catch statement lets you handle the error.
The throw statement lets you create custom errors.
The finally statement lets you execute code, after try and catch, regardless of the result.
<html>
<head>
try {
if ( b == 0 ) {
throw( "Divide by zero error." );
} else {
var c = a / b;
}
}
catch ( e ) {
alert("Error: " + e );
}
finally {
alert("Finally block will always execute!" );
}
}
</script>
</head>
<body>
<p>Click the following to see the result:</p>
<form>
<input type = "button" value = "Click Me" onclick = "myFunc();" />
</form>
</body>
</html>
EXERCISE:
1. Write an “if” condition to check that age is between 14 and 90 inclusively.
(“Inclusively” means that age can reach the edges 14 or 90.)
2. Write a program to enter one character from user and display message “Your
Character is Vowel”, if it’s a vowel. (Note: Example of if else Statement).
3. Write the code which asks for a login with prompt. If the visitor enters "Admin",
then prompt for a password, if the input is an empty line or Esc – show
“Canceled.”, if it’s another string – then show “I don’t know you”.
Please use nested if blocks. Mind the overall readability of the code.
Hint: passing an empty input to a prompt returns an empty string ''. Pressing ESC
during a prompt returns null.
4. Write a program to take number of unit consumed in a month and calculate the
electricity bill by using charges specified as below:
6. For every loop iteration, write down which value it outputs. Both loops alert the
same values, or not?
let i = 0;
let i = 0;
7. Write the code which outputs prime numbers in the interval from 2 to n.