Computing Concepts Sample if statements
Tom Linton, Fall 2001
The basic syntax for an if statement in JavaScript is:
last statement;
if (boolean test condition) {
statement1;
statement2;
.
.
.
statementN;
}
next statement;
Notice that there is NO semicolon for the test condition, and to help readability
we will indent all of the conditional statements (by three or four spaces).
When the JavaScript brain executes this section of code, one of two possible
things happen,
-
First, the last statement is executed,
-
second, the boolean test condition is evaluated resulting in a true
or false value.
-
If the result is false, statements 1 to N are skipped.
-
If the result is true, statements 1 to N are executed.
-
In both cases, the next statement is executed.
In this simple form, one can execute, or skip, the statements inside curly
brackets. A more common situation arises when one would like to carry out
two different sets of statements depending on whether a test condition
is true or false (in the above setting, the second set of statements is
empty, as nothing extra is done when the test is false). Here is a template
for executing different blocks of statements depending on whether a test
condition is true or false. When the test condition is true, the first
block of statements will be executed and the second block will be skipped.
When the test is false, the second block of statements will be executed
and the first block will be skipped.
last statement;
if (boolean test condition) {
true statement1;
true statement2;
:
true statementN;
}
else {
false statement1;
false statement2;
:
false statementk;
}
next statement;
Here are some examples of JavaScript if statements. Of course, to work,
these would need to be included inside <SCRIPT> tags in an HTML document.
-
Ask the user for a number and tell them if it is positive or not positive.
var number;
number = prompt("Please enter a number.","");
number = parseFloat(number);
//this is a comment, check if number is positive.
if (number > 0) {
alert("You entered a positive number!");
}
//check if number is not positive.
if ( !(number > 0) ) {
alert("You entered a non-positive number!");
}
-
Same as above, but use an if-else block.
var number;
number = prompt("Please enter a number.","");
number = parseFloat(number);
//this is a comment, check if number is positive.
if (number > 0) {
alert("You entered a positive number!");
}
else {
alert("You entered a non-positive number!");
}
-
Ask the user for a number and tell them if it is odd or even. Note, n
% k gives the remainder when the number n is divided by k. For
example, 17 % 6 gives 5, since when 17 is divided by 6 the result is 2
with a remainder of 5, or equivalently, 17 = 2*6 + 5. The remainder operator
is used a lot in programming, and in particular, the number k is even exactly
when k % 2 is equal to zero.
var number;
number = prompt("Please enter a number."."");
number = parseFloat(number);
//check if number is even.
if ((number % 2) == 0) {
alert("You entered an even number!");
}
//if a number is not even, it must be odd.
else {
alert("You entered an odd number!");
}
-
Ask the user for two numbers and print them out from smallest to largest.
var num1;
var num2;
num1 = prompt("Please enter your first number."."");
num1 = parseFloat(num1);
num2 = prompt("Please enter your second number."."");
num2 = parseFloat(num2);
//variables for the smaller and larger numbers
var min;
var max;
if (num1 <= num2) {
//num1 is the smaller
min=num1;
max=num2;
}
//otherwise num2 is the smaller one.
else {
min=num2;
max=num1;
}
document.write("The smaller number is " + min + ", and the larger
is " + max + "!");
-
Ask the user to enter a 1, 2, or 3 and then print out the corresponding
ordinal number first, second, or third. Add a simple check to see that
they entered a legal number.
var number;
number = prompt("Please enter a 1, 2, or 3.","");
number = parseFloat(number);
//check if it is NOT a 1,2 or 3
if ( !((number<=3) && (number >= 1)) ) {
alert("You must enter a 1, 2, or 3 ONLY!");
number=prompt("Please enter a 1, 2, or 3.","");
number = parseFloat(number);
}
//a variable for the ordinal version of number
var place;
//check if it is a one or two, and use three otherwise.
if (number == 1) {
place="first";
}
else if (number==2) {
place="second";
}
else {
place = "third";
}
//output the results
document.write("You finished in " + place + " place!");