If any statement executed depends on a condition is called
conditional statements and it is also known as control structures
JS supports 6 types of control structures
1. If
When the condition is true if statement will execute
2. If else
When the condition is true if statement will execute and
condition false statement is execute both are displaying on same condition
3. Ternary
It is also similar to if-else and also known as single line
condition
4. Nested if else
If any condition exist within other condition is called
nested if else to execute child condition parent condition must true
5. Else if later
It will compare the multiple conditions and returns only one
out of multiple
6. Switch
It is also similar to else if ladder but it will treat
conditions like cases
<html>
<head>
<script
language="javascript">
var
age = prompt("Please enter your age");
document.write("<h1>IF</h1>");
if(age
< 18){
document.write("Minor!
You can't Vote Now")
}
document.write("<h1>IF-Else</h1>");
if(age
< 18){
document.write("Minor!
You can't Vote Now")
}else{
document.write("Major!
You cant Vote");
}
document.write("<h1>Ternary</h1>");
(age
< 18)? document.write("Minor"):document.write("Major");
document.write("<h1>Nested
IF-Else</h1>");
if(age){
if(age
<= 10 && age >= 0){
document.write("Childage<br>")
}else{
document.write("Not
In Childage<br>");
}
if(age
<= 20 && age >= 11){
document.write("Teenage<br>")
}else{
document.write("Not
In Teenage<br>");
}
if(age
<= 30 && age >= 21){
document.write("Youngage<br>")
}else{
document.write("Not
In Youngage<br>");
}
}else{
document.write("Please
enter your age");
}
document.write("<h1>Else
IF Ladder</h1>");
if(age
<= 10 && age >= 0){
document.write("Childage<br>")
}else
if(age <= 20 && age >= 11){
document.write("Teenaage<br>")
}else
if(age <= 30 && age >= 21){
document.write("Youngage<br>")
}
document.write("<h1>Switch</h1>");
switch(age){
case
'10':
document.write("age
is 10");
break;
case
'20':
document.write("age
is 20");
break;
case
'30':
document.write("age
is 30");
break;
default:
document.write("All
Cases failed");
}
</script>
</head>
<body>
</body>
</html>
No comments:
Post a Comment