0% found this document useful (0 votes)
10 views

2 JS STMT Loop Func (Unit 3)

Uploaded by

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

2 JS STMT Loop Func (Unit 3)

Uploaded by

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

WEB TECHNOLOGY

KCS-602

by

Dr. Seema Maitrey


M.Tech(CE), Ph.D(CSE)
Department of Computer Science & Engineering
Contents
❑ Conditional Statements

❑ Loops

❑ Functions

❑ Built-in Functions
CONDITIONAL STATEMENTS
In JavaScript we have the following conditional statements:
• if statement - to execute some code only if a specified condition is true

• if...else statement - to execute some code if the condition is true and another code if the

condition is false

• if...else if....else statement - to select one of many blocks of code to be executed

• switch statement - to select one of many blocks of code to be executed


Conditional Statements(1)
if (condition) {
(1) Example of if statement: code to be executed if condition is true }

<script> Output: value of a is greater than 10


var a=20;
if(a>10){
document.write("value of a is greater than 10");
}
</script>

<script>
(2) Example of if …else statement: var a=20;
if(a%2==0){
if (condition)
{ document.write("a is even
code to be executed if condition is number");
true
} }
else
{ else{
code to be executed if condition is not document.write("a is odd
true
} number");
}
Output: a is even number
</script>
Conditional Statements(2)
<script>
(3) Example of if…else…if statement: var a=20;
if(expression1){
if(a==10){
//content to be evaluated if expression1 is true document.write("a is equal to 10");
} }
else if(expression2){
//content to be evaluated if expression2 is true
else if(a==15){
} document.write("a is equal to 15");
else if(expression3){ }
//content to be evaluated if expression3 is true
}
else if(a==20){
else{ document.write("a is equal to 20");
//content to be evaluated if no expression is true }
}
else{
document.write("a is not equal to 10, 15 or 20");
}
</script>

Output: a is equal to
20
Conditional Statements(3) <script>
var grade='B';
(4) Example of switch statement: var result;
switch(grade){
switch(expression){ case 'A':
case value1:
code to be executed; result="A Grade";
break; break;
case value2: case 'B':
code to be executed;
break; result="B Grade";
...... break;
case 'C':
default:
code to be executed if above values are not result="C Grade";
matched; break;
} default:
result="No Grade";
}

document.write(result); Output: B Grade


</script>
LOOPS
Loops are used to iterate the piece of code using for, while, do while or for-in loops.
It makes the code compact.

4 Types of loops: for loop loops through <script>


1. for loop a block of code a var text = "";
2. for-in number of times var i;
3. while loop for (i = 0; i < 5; i++) {
4. do-while loop text += "The number is " + i + "<br>";
}

for/in statement , Loop var numbers = [45, 4, 9, 16, 25];


over the properties of var txt = "";
an object var x;
for (x in numbers) {
txt += numbers[x] + "<br>"; }
LOOPS

Types of loops:
while loop loops through a <script>
block of code as long as a var text = "";
specified condition is true. var i = 0;
while (i < 10) {
text += "<br>The number is " +
3. while loop i;
4. do-while loop i++;
}
do/while loop will execute the <script>
code block once, before var text = ""
checking ; if the condition is var i = 0;
true, then it will repeat the loop do {
as long as the condition is true. text += "<br>The number is " +
i;
i++;
} while (i < 10);
FUNCTIONS
Used to perform operations.
We can call function many times to reuse the code.

Advantage:

There are mainly two advantages of functions:

• Code reusability: We can call a function several times so it save coding.


• Less coding: It makes our program compact. We don’t need to write many lines
of code each time to perform a common task.

Syntax: function functionName([arg1, arg2, ...argN]){


//code to be executed
}

Functions can have 0 or more arguments.


Functions(1)
Functions can have 0 or more arguments:

Example of call function by passing arguments:

<script>
Example of function without arguments: function getcube(number){
<script> alert(number*number*number);
}
function msg(){
</script>
alert("hello! this is message"); <form>
} <input type="button" value="click" onclick="getcube(4)"/>
</script> </form>
<input type="button" onclick="msg()"
value="call function"/> <script>
function getInfo(a,b){
return a*b ; }
</script>
Ex. of function with Return Value:
<script>
var x=getInfo(4,5);
document.write(x);
</script>
BUILT-IN FUNCTIONS
(I) isNaN(x): Determines whether a value is “Not a Number”
<html>
OP :
<title>My first Javascript code</title>
EX.: </head>
<script>
document.write(isNaN(0));
document.write("</br>");
document.write(isNaN(-1.891));
document.write("</br>");
document.write(isNaN("12"));
document.write("</br>");
document.write(isNaN("2015/4/8"));
document.write("</br>");
document.write(isNaN("KIET"));
</script>
</body>
</html>
Built-in Functions(1)

(II) eval(expr) : evaluates an expression or statement

<script> OP.:
Ex.: function myFunction() {
var x = 10;
var y = 20;
var a = eval("x * y") + "<br>";
var b = eval("2 + 2") + "<br>";
var c = eval("x + 17") + "<br>";

var res = a + b + c;
document.write(res);
}
</script>
Built-in Functions(2)
(III) is Finite(x) : Determines if a number is finite

Example Output
<html> <head> <title>Javascript code</title> </head> True
<script> False
document.write(isFinite("12345")); False
document.write("</br>");
document.write(isFinite("ABCD"));
document.write("</br>");
document.write(isFinite("123_456"));
</script>
</html>
Built-in Functions(3)

(IV) parseInt: Takes string as a parameter and converts to integer

Example Output
<html> <head> <title>Javascript code</title> </head>
<script>
document.write(parseInt("95"));
document.write("</br>");
document.write(parseInt("180 rooms"));
document.write("</br>");
document.write(parseInt("Todays count is 88"));
</script> </html>
We Covered:
▪ Conditional Statements

▪ Loops , Functions

▪ Built-in Functions
Thank
You

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy