![]() |
|
![]() |
When talking about loops, we need to understand something about conditional statements. In the "for loop" definition is a conditional statement labeled end test which gives a true value until the loop end is hit.
Conditional statements are also used elsewhere in JavaScript programs to conditionally execute a block of code.
A conditional statement has only two possible outcomes - true or false. When dealing with numerical results this usually translates in to a non-zero or zero value.
Conditional operators:
Example:
work/cond.html
<HTML>
<BODY>
<H1>Conditional statements
<SCRIPT LANGUAGE="JavaScript">
var zero = 0;
var one = 1;
var same = '1'; /* string - JavaScript will do the type conversion */
document.writeln("<PRE>");
document.writeln("==== different values ====");
document.writeln("zero == one : ",zero == one);
document.writeln("zero != one : ",zero != one);
document.writeln("zero < one : ",zero < one);
document.writeln("zero <= one : ",zero <= one);
document.writeln("zero >= one : ",zero >= one);
document.writeln("zero > one : ",zero > one);
document.writeln("==== same values ====");
document.writeln("same == one : ",same == one);
document.writeln("same != one : ",same != one);
document.writeln("same < one : ",same < one);
document.writeln("same <= one : ",same <= one);
document.writeln("same >= one : ",same >= one);
document.writeln("same > one : ",same > one);
document.writeln("==== same values, but different types ====");
document.writeln("same === one : ",same === one);
document.writeln("same !== one : ",same !== one);
document.writeln("==== logical operators (truth tables) ====");
document.writeln(" A B | A && B A || B");
document.writeln("false false | ",false && false," ", false || false);
document.writeln("false true | ",false && true," ", false || true);
document.writeln("true false | ",true && false," ", true || false);
document.writeln("true true | ",true && true," ", true || true);
document.writeln("</PRE>");
</SCRIPT>
</BODY>
</HTML>