Sunteți pe pagina 1din 3

Chapter 2 Control Structures

The most common control structure is the if structure.


if (conditions) {
statement(s) if condition is true }
else {
statement(s) if condition is false}

This structure can be read as:


If the condition is true then write the true statement, else
if the condition is not true write the false statement.
It is important to notice the use of the face brackets { and } that are used to enclose each
statement. If there is only one statement the face brackets can be omitted. If there is more
than one statement you must use the brackets to enclose the statements.

Example of IF Control Structure


We want to check whether a year is, or is not, a leap year. However before we write
the program we must understand the rules which make a year a leap year
The rules:
If a year is divisible by 4 it is a leap year except if the year number has two trailing
zeros, i.e. 1900 or 1600, when
if the year is divisible by 100 and 400 it is a leap year.
Using these rules we can find if a year is a leap year.

LEAP YEAR
<html>
<script>
function checkLeap() {
year = parseInt(document.first.year.value,10);

//definition of what is NOT a leap year


// if year remainder(4) is not zero, NOT a leap year

if (year%4>0)
leap="not a leap year";
else {

// if year remainder(100) is zero AND remainder(400) is not zero, NOT a leap


year

if ((year%100==0) && (year%400!=0))


leap="not a leap year";
// end definition of what is NOT a leap year
// every other year IS a leap year

else
leap="is a leap year";
}
document.first.leap.value = leap;

// end of function
}
</script>

<body>
<form name = "first">
<p> Input year :<input type=text name="year" size=4> </p>
<p> <input type=text name="leap" size=20 > </p>
<p> <input type=button value="Leap Year?" onClick="checkLeap()"> </p>
</form>
</body>
</html>

These points are important:


The most common mistake is to use an assignment equals, = , in the condition rather than
a comparison equals, == .
We mean to say is: (year%100==0) year modulo 100 is the same value as zero.
Mixing cases in names of thing, i.e. onClick = "checkleap()" will not find the function
checkLeap().
The second point is important. Generally JavaScript programs, and HTML code are best
written in lower case, with the exception of string messages, and functions, objects and
variable names. This is a good practice to adopt.
Lastly notice the use of the forward slashes // in the script section. These are used to
denote a comment that is not read by the program.
Now try modifying the program
If the user does not enter a number in the year input box what happens? The leap input
box should display the message 'is a leap year'. This message is of course wrong. What
we need is a message to tell the user that they must enter a year value. We can
accomplish this by getting the script to check whether the value in the year box is empty,
and if it is, put an alert box on the screen.
To do this we surround the current script with an if: If the year input is the same as an
empty string:
if(document.first.year.value == "") {
alert("no input")
}else{
Now the original code.
}
Notice that the new if condition is enclosed by face brackets, and the else statement
begins begins the leap year calculation with a face bracket which ends immediately before
the function end face bracket.
Try this exercise
Try modifying the original script to check that the user has entered a year number.
To accomplish this we need to add a line to the script section before setting the input year
to an integer. We use an if structure to check if the input value document.first.leap.value
is the same as nothing, i.e. an empty string “”.
If it is an empty string we use an alert box message to inform the user:
alert(“no year input”).

If (conditions) {
alert(“no year input”)
}

We do not need the else part of the structure because if the input is not an empty string
then then everything is fine.

We should also check that the user has input a number rather than text, i.e. 2004 rather
than two thousand and four. This check is discussed in chapter 3.

S-ar putea să vă placă și