Professional Documents
Culture Documents
Conditional Statements and Loops
Conditional Statements and Loops
and Loops
if (comparison here)
We want to see if a variable named boats is
equal to 3.
Problem 1
Send an alert that says You have the right
number of boats if the variable boats is
equal to three. If it is not, we want to send
an alert that says You do not have the
right number of boats instead.
<script language="JavaScript">
<!-if (boats == 3)
{
window.alert("You have the right number of boats.");
}
else
{
window.alert("You do not have the right number of boats.");
}
//-->
</script>
<script language="JavaScript">
<!-var boats = 3;
if (boats == 3)
{
window.alert("You have the right number of boats.");
}
else
{
window.alert("You do not have the right number of boats.");
}
//-->
</script>
<script language="JavaScript">
<!-var boats = 0;
if (boats == 3)
{
window.alert("You have the right number of boats.");
}
else
{
window.alert("You do not have the right number of boats.");
}
//-->
</script>
Problem 2
If variable named car is equal to yes, and if a
variable named licence is equal to yes, send an
alert that says You can drive to the browser.
If variable named car is equal to yes, but if a
variable named licence is not equal to yes,
send an alert that says You cannot drive to the
browser; otherwise send an alert that says You
need a car.
<script language="JavaScript">
<!-if (car == "yes")
{
if (licence == "yes")
{
window.alert("You can drive.");
}
else
{
window.alert("You need a licence to drive.");
}
else
{
window.alert("You need a car.");
}
//-->
</script>
<script language="JavaScript">
<!-if (car == "yes")
{
if (licence == "yes")
{
window.alert("You can drive.");
}
else
{
window.alert("You need a licence to drive.");
}
}
else
{
window.alert("You need a car.");
}
//-->
</script>
<script language="JavaScript">
<!-var car = "yes";
var licence = "yes";
if (car == "yes")
{
if (licence == "yes")
{
window.alert("You can drive.");
}
else
{
window.alert("You need a licence to drive.");
}
}
else
{
window.alert("You need a car.");
}
//-->
</script>
<script language="JavaScript">
<!-var car = "yes";
var licence = no";
if (car == "yes")
{
if (licence == "yes")
{
window.alert("You can drive.");
}
else
{
window.alert("You need a licence to drive.");
}
}
else
{
window.alert("You need a car.");
}
//-->
</script>
<script language="JavaScript">
<!-var car = "no";
var licence = "no";
if (car == "yes")
{
if (licence == "yes")
{
window.alert("You can drive.");
}
else
{
window.alert("You need a licence to drive.");
}
}
else
{
window.alert("You need a car.");
}
//-->
</script>
Another example
if (car == "yes")
{
if (licence == "yes")
{
window.alert("You can drive.");
}
else
{
window.alert("You need a licence to drive.");
}
}
else
{
if (helicopter == "yes")
{
window.alert("Use the helicopter.");
}
else
{
window.alert("You need a car.");
}
}
Switch Statements
Allows us to take a single variable value
and execute a different line of code based
on the value of the variable.
Example
var thename = "Salman";
switch (thename)
{
case Naveed:
window.alert("Naveed is an OK name.");
break;
case Salman:
window.alert(Salman is a great name!");
window.alert("Hi Salman!");
break;
default:
window.alert("You have a unique name.");
}
What is a Loop?
A block of code that allows us to repeat a
section of code a certain number of times,
perhaps changing certain variable values
each time the code is executed.
Types of Loops
For loops
While loops
For Loops
Structure
Block Nesting
While Loops
Looks at short comparison and repeats
until comparison is no longer true
var count = 1;
while (count < 11)
{
document.write(Im part of a loop.<br>);
count +=1;
}
Do While Loops
Loop executed at least once , even if
comparison used return false the first time.
var count = 1;
do
{
document.write(Hi!);
count +=1;
} while (count < 6);