2 JS STMT Loop Func (Unit 3)
2 JS STMT Loop Func (Unit 3)
KCS-602
by
❑ 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
<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";
}
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:
<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)
<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)
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