4QL2
4QL2
Fourth Quarter
Lesson 2
Conditions, Controls and Loops
Conditional Statements
In JS, conditional statements perform different actions for
different decisions.
If Statement
The “If Statement” is used to check or verify a condition and
execute a set of statements only if the condition is true. This
should be referred as a statement and not as a function.
The If statement is one of the most popular and most important
conditional constructs in JS and in many other programming
languages. This conditional statement construct evaluates a
condition to True or False. It then runs a specific code depending
on the result of this evaluation.
<!DOCTYPE html>
<head><title>IF</title> Try this Sample script
<script type=“text/javascript”>
function check()
{if (document.example.answer.value==“Web Scripting”)
{document.write(“You answered correctly!”);}}
</script></head>
<body>
<p>What is the title of this book?</p>
<form name=“example”>
<input type=“text” name=“answer” value=“”>
<input type=“button” name=“answerbutton” value=“SUBMIT”
onClick=check()>
</form></body>
</html>
Nested If Statement
If-Else statement has also the same format as “if statement”. The
only thing, is that an Else part is added to an If statement. So, if the
condition satisfies the statement inside an If statement, part of the
code will be executed. And Else statement inside Else part only will
be executed.
The If-Else statement is similar to the If statement, except that we
are giving an alternative instruction in case the arguments isn’t
TRUE.
Syntax:
if(condition)
{//set of statements if condition satisfies}
else
{//set of statements if condition fails}
<!DOCTYPE html>
<head><title>If Else</title>
</head> Try this Sample script
<body>
<script language=“javascript”>
var grade=60;
if(grade>=75) Note: In this example, the condition
is to check if the variable grade
{document.write(“Passed”);}
equals(>=)75. If the condition is not
else satisfied as assigned in the
{document.write(“Failed”);} initialization condition, then the Else
</script> part is executed. In this case, the
</body> variable grade was initialized at 60
</html> which makes the condition false,
hence the else portion is
implemented giving the result
“Failed”.
<!DOCTYPE html>
Try this 2 nd Sample script
<head><title>If Else</title>
<script type=“text/javascript”>
function check()
{
if (document.example.answer.value==”Web Scripting”)
{document.write(“You answered correctly!”);}
else
{document.write(“Sorry. Try again.”);}}
</script>
</head>
<body>
<p>What is the title of this book?</p>
<form name=“example”>
<input type=“text” name=“answer” value=“”>
<input type=“button” name=“answerbutton” value=“SUBMIT”
onClick=check()></form></body>
</html>
If Else If Statement
When you use the If Else If statement, you are simply telling the
browser that is something is True, then do something else, if the
other thing is True, then do something else, and so on.
<!DOCTYPE html>
<head><title>If Else</title>
<script type=“text/javascript”> Try this Sample script
function check()
{
if (document.example.answer.value==”Web Scripting”)
{document.write(“You answered correctly!”);}
else if (document.example.answer.value==“”)
{document.write(“You did not answer the question!”);}
else
{document.write(“Sorry. Try again!”);}}
</script>
</head>
<body>
<p>What is the title of this book?</p>
<form name=“example”>
<input type=“text” name=“answer” value=“”>
<input type=“button” name=“answerbutton” value=“SUBMIT” onClick=check()>
</form></body>
</html>
Switch Statement
switch(condition)
{
case result1:
//operation for result 1
case result2:
//operation for result 2
.
.
default:
//If result belongs to none of the case specified. }
<!DOCTYPE html>
<head><title>Switch</title> Try this Sample script
<script type=“text/javascript”>
function bookcheck()
{ var book=document.frmbook.cmbbook.value
switch (book) <body>
{case “1”: alert(“J.R.R Tolkien”) <p>Select a book below to find the name of
break the author</p>
case “2”: alert(“C.S. Lewis”) <form name=formbook>
break <select name=cmbbook>
case “3”: alert(“J.K Rowling”) <option value=1>Lord of the Rings</option>
break <option value=2>Chronicles of Narnia
case “4”: alert(“Mark Twain”) </option>
break <option value=3>Harry Potter</option>
default: alert(“undetermined”)}} <option value=4>The Adventures of
</script></head> Huckleberry Finn</option></select>
<input type=“button” name=“combo”
value=“SUBMIT” onClick=bookcheck()>
</form>
</body>
</html>
In the example, we started by creating a custom function named
bookcheck(). Within that function we declared a variable declaring a
variable called “book” and using that variable to get the value of the
selected item in the combo box. We opened a Switch statement,
passing in the variable we want to test which is “book”. This is is
followed by a set of cases. The “break” construct after each case
prevents the code from running into the next case. Prior to the
closing bracket, we placed a default condition. This will be
executed if none of the previous cases is true.