Home » Online Computer Education » Learn Java Script, JavaScript and JScript Statements, The If Statements, Loops and Switch, Lesson No. 19

Learn Java Script, JavaScript and JScript Statements, The If Statements, Loops and Switch, Lesson No. 19

 

 

JavaScripts and JScripts consist of a series of statements. These statements are the programming instructions, or logic, that you write to tell the scripts what you want them to do.

Using Conditional Statements to Alter Script Flow
Conditional statements enable you to test for various conditions and take action based on the results of the test. Specifically, conditional statements execute when the tested condition proves to be true. Conditional statements include the if, if…else, and switch statements.

The if Statement
The if statement checks whether a logical condition is true; if it is, it then executes one or more statements. The basic syntax of the if statements is shown here:

if (condition)
statement
For example, the following pair of statements shows a simple if statement. The first line sets up a conditional test. If the test proves true (for example, if the counter variable is currently less than 10), the statement immediately following is executed. In this example, the current value of the variable counter is then incremented by 1.

if (counter < 10)
counter++;
There will be times when executing a single statement is just not enough. In this case, you can write your if statement using the following format:

if (counter > 10) {
counter++;
window.alert(“This is a test”);
}
The preceding example shows an if statement where the condition is followed immediately by the { sign and terminated with the } sign. Between the braces can be any number of script statements.

The following sample script uses the two types of if statements you just looked at plus a third variation:

<HTML>
<HEAD>
<TITLE>Script 2.9 – Demonstration of if statements</TITLE>
</HEAD>
<BODY>
<SCRIPT LANGUAGE=”JavaScript” TYPE=”Text/JavaScript”>
<!– Start hiding JavaScript statements
var accountStatus = open; //assume all accounts are
open by default
//Prompt the user for an account name
var accountName = window.prompt(“Enter your account
name:”);
//Test whether the supplied account name equals
Morganstern
if (accountName == “Morganstern”)
document.write(“The account number for this account
is 12321. <BR>”);
//Set accountStatus equal to warning if the account
name equals Davidson
if (accountName == “Davidson”) {
document.write(“The account number for this account
is 88844. <BR>”);
accountStatus = “warning”;
}
//Display one of two messages based on the value of
accountStatus
if (accountStatus != “warning”) {
document.write(“You may accept new orders from this
customer.”);
}
else {
document.write(“Do not accept new orders for this
account.”);
}
// End hiding JavaScript statements –>
</SCRIPT>
</BODY>
</HTML>
This example starts by displaying a prompt dialog box asking the user to input the name of a business account using the window.prompt() method. Next, it executes three if statements, each of which compares the user’s input against three separate criteria. Any conditional test that proves true results in some action. As you can see, the if statement enables you to create different logical flows based on user input.

The third if statement adds a new twist by adding an else statement. In the event that the if statement’s conditional test proves true, the statements following the if statement are executed, and the else statement is skipped. But if the if statement proves false, the logic in the else statement is processed. The else statement enables you to provide an alternative course of action for any conditional test.

Figures 2.6 and 2.7 show the logical flow of the script when the user inputs the account name of Morganstern.

 

5

 

6
Before I move on, there is one final variation of the if statement I want to mention. This is the nested if statement. A nested if statement is a series of two or more if statements nested or embedded within one another. The following example shows a nested if that is two if statements deep. The second if statement is executed only if the first if statement proves true:

if (accountStatus != “warning”) {
if (accountNumber > 10000) {
document.write(“You may accept new orders from this
customer.”);
}
else {
document.write(“Invalid account number. Notify bank
security.”);
}
}
Else {
document.write(“This Account has been marked with a
warning!”);
}

About

The main objective of this website is to provide quality study material to all students (from 1st to 12th class of any board) irrespective of their background as our motto is “Education for Everyone”. It is also a very good platform for teachers who want to share their valuable knowledge.

Leave a Reply

Your email address will not be published. Required fields are marked *