the if then else statement using javascript tip the if statement is the fundamental control...

28
The If Then Else The If Then Else Statement Statement Using JavaScript Using JavaScript TIP The If Statement is the fundamental control structure that allows branches in the flow of control in a program.

Upload: corinne-bayles

Post on 01-Apr-2015

219 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: The If Then Else Statement Using JavaScript TIP The If Statement is the fundamental control structure that allows branches in the flow of control in a

The If Then Else The If Then Else StatementStatement

Using JavaScriptUsing JavaScript

TIP The If Statement is the

fundamental control structurethat allows branches in the

flow of control ina program.

Page 2: The If Then Else Statement Using JavaScript TIP The If Statement is the fundamental control structure that allows branches in the flow of control in a

The If-Then-Else The If-Then-Else StatementStatement

• With it , we can ask a question and With it , we can ask a question and choose a course of action.choose a course of action.

• IfIf a certain condition exists, a certain condition exists, thenthen perform one action, perform one action, elseelse perform another perform another action.action.

• The computer performs just one of the The computer performs just one of the two actions, depending on the result of two actions, depending on the result of the condition being tested.the condition being tested.

Page 3: The If Then Else Statement Using JavaScript TIP The If Statement is the fundamental control structure that allows branches in the flow of control in a

The If-Then-Else formThe If-Then-Else form

if ( Expression )

Statement-A

else

Statement-B

Page 4: The If Then Else Statement Using JavaScript TIP The If Statement is the fundamental control structure that allows branches in the flow of control in a

If ( expression )If ( expression )

• The expression in parentheses can be The expression in parentheses can be of any simple data type. Almost of any simple data type. Almost without exception this will be a logical without exception this will be a logical (Boolean) expression.(Boolean) expression.

• The expression gets evaluated at run The expression gets evaluated at run time and based on the condition time and based on the condition tested, results in being either TRUE or tested, results in being either TRUE or FALSE.FALSE.

Page 5: The If Then Else Statement Using JavaScript TIP The If Statement is the fundamental control structure that allows branches in the flow of control in a

IfIf ( Expression ) ( Expression )Statement-AStatement-A

elseelseStatement-BStatement-B

• At run time, the computer evaluates At run time, the computer evaluates the expression. the expression.

• IfIf the value is TRUE, Statement-A is the value is TRUE, Statement-A is executed.executed.

• IfIf the value is FALSE the value is FALSE,, Statement-B Statement-B is executed.is executed.

Page 6: The If Then Else Statement Using JavaScript TIP The If Statement is the fundamental control structure that allows branches in the flow of control in a

If-Then-Else Flow of If-Then-Else Flow of ControlControl

false true

Page 7: The If Then Else Statement Using JavaScript TIP The If Statement is the fundamental control structure that allows branches in the flow of control in a

NoticeNotice

• In JavaScript the If statement uses In JavaScript the If statement uses the reserved words the reserved words ifif and and elseelse but but does not include the word does not include the word thenthen..

• The term The term If-Then-ElseIf-Then-Else corresponds corresponds to how we say things in English: “to how we say things in English: “IfIf something is true, something is true, thenthen do this, do this, elseelse do that.” do that.”

Page 8: The If Then Else Statement Using JavaScript TIP The If Statement is the fundamental control structure that allows branches in the flow of control in a

Code Fragment to Code Fragment to illustrate If-Then-Elseillustrate If-Then-Else

If ( hours <= 40.0 )If ( hours <= 40.0 )

pay = rate * hours;pay = rate * hours;

elseelse

pay = rate * ( 40.0 + ( hours - 40.0 ) * 1.5 );pay = rate * ( 40.0 + ( hours - 40.0 ) * 1.5 );

document.write(pay + “<br />”);document.write(pay + “<br />”);

Observe the indentation of the then-clause and the else-clause, which makes it easy to read.Observe the indentation of the then-clause and the else-clause, which makes it easy to read.

Page 9: The If Then Else Statement Using JavaScript TIP The If Statement is the fundamental control structure that allows branches in the flow of control in a

If-Then-Else Flow of If-Then-Else Flow of ControlControl

false true

Page 10: The If Then Else Statement Using JavaScript TIP The If Statement is the fundamental control structure that allows branches in the flow of control in a

Other Uses for If-Then-Other Uses for If-Then-ElseElse

• The If-Then-Else often is used to check The If-Then-Else often is used to check the validity of input. For example, the validity of input. For example, before we can ask the computer to before we can ask the computer to divide by a data value, we should be divide by a data value, we should be sure that the value is not zero.sure that the value is not zero.

• If you try, most computers will halt the If you try, most computers will halt the execution of the program.execution of the program.

Page 11: The If Then Else Statement Using JavaScript TIP The If Statement is the fundamental control structure that allows branches in the flow of control in a

Code for checking User Code for checking User InputInput

If ( divisor != 0 )If ( divisor != 0 )

result = dividend / divisor;result = dividend / divisor;

elseelse

document.write(“Division by zero is not document.write(“Division by zero is not allowed”);allowed”);

Page 12: The If Then Else Statement Using JavaScript TIP The If Statement is the fundamental control structure that allows branches in the flow of control in a

The If-Then FormThe If-Then Form

• Sometimes you run into a situation where you Sometimes you run into a situation where you want to say. “If a certain condition exists, then want to say. “If a certain condition exists, then perform some action; otherwise, don’t do perform some action; otherwise, don’t do anything.”anything.”

• In other words, you want the computer to skip In other words, you want the computer to skip a sequence of instructions if a certain condition a sequence of instructions if a certain condition isn’t met.isn’t met.

• You can do this by leaving the else branch You can do this by leaving the else branch empty.empty.

Page 13: The If Then Else Statement Using JavaScript TIP The If Statement is the fundamental control structure that allows branches in the flow of control in a

If Statement (the If-Then If Statement (the If-Then form)form)

IfIf ( expression ) ( expression )

statementstatement

Page 14: The If Then Else Statement Using JavaScript TIP The If Statement is the fundamental control structure that allows branches in the flow of control in a

Example of If-Then FormExample of If-Then Form

if if (age < 18) (age < 18)

document.write(“Not an eligible”);document.write(“Not an eligible”);

document.write(“ voter.”); document.write(“ voter.”);

This statement means that if age is less than This statement means that if age is less than 18, first print “Not an eligible “ and then 18, first print “Not an eligible “ and then print “voter.” If age is not less than 18, skip print “voter.” If age is not less than 18, skip the first statement and go directly to print the first statement and go directly to print “voter.”“voter.”

Page 15: The If Then Else Statement Using JavaScript TIP The If Statement is the fundamental control structure that allows branches in the flow of control in a

If-Then Flow of ControlIf-Then Flow of Control

false true

Page 16: The If Then Else Statement Using JavaScript TIP The If Statement is the fundamental control structure that allows branches in the flow of control in a

Nested If StatmentsNested If Statments

• There are no restrictions on what There are no restrictions on what the statements in an If can be.the statements in an If can be.

• In general, any problem that In general, any problem that involves a multiway branch (more involves a multiway branch (more than two alternative courses of than two alternative courses of action) can be coded using nested If action) can be coded using nested If statements.statements.

Page 17: The If Then Else Statement Using JavaScript TIP The If Statement is the fundamental control structure that allows branches in the flow of control in a

Nested If StatementsNested If Statementsif ( month == 1 )if ( month == 1 )

document.write(“January”);document.write(“January”);

if ( month == 2 )if ( month == 2 )

document.write(“February”);document.write(“February”);

if ( month == 3 )if ( month == 3 )

document.write( “March”);document.write( “March”);

......

if ( month == 12 )if ( month == 12 )

document.write( “December”);document.write( “December”);

Page 18: The If Then Else Statement Using JavaScript TIP The If Statement is the fundamental control structure that allows branches in the flow of control in a

Equivalent Nested If Equivalent Nested If StatementsStatementsif ( month == 1 )if ( month == 1 )

document.write( “January”);document.write( “January”);

elseelse

if ( month == 2 )if ( month == 2 )

document.write( “February”);document.write( “February”);

elseelse

if ( month == 3 )if ( month == 3 )

document.write( “March”);document.write( “March”);

elseelse

if ( month == 4 ) // and so on….if ( month == 4 ) // and so on….

......

Page 19: The If Then Else Statement Using JavaScript TIP The If Statement is the fundamental control structure that allows branches in the flow of control in a

Nested If StatmentsNested If Statments

• The preceding Nested If Structure is The preceding Nested If Structure is more efficient because it makes more efficient because it makes fewer comparisons. fewer comparisons.

• The first version --- the sequence of The first version --- the sequence of independent If statements --- always independent If statements --- always tests every condition ( all 12 of them tests every condition ( all 12 of them , even if the first one is satisfied., even if the first one is satisfied.

Page 20: The If Then Else Statement Using JavaScript TIP The If Statement is the fundamental control structure that allows branches in the flow of control in a

Nested If StatmentsNested If Statments

• In contrast, the nested if solution In contrast, the nested if solution skips all remaining comparisons skips all remaining comparisons after one alternative has been after one alternative has been selected.selected.

• What do you think the advantage is What do you think the advantage is using the second example ?using the second example ?

Page 21: The If Then Else Statement Using JavaScript TIP The If Statement is the fundamental control structure that allows branches in the flow of control in a

Nested If StatementsNested If Statements

• As fast as modern computers are, As fast as modern computers are, many applications require so much many applications require so much computation that inefficient computation that inefficient algorithms can waste hours of algorithms can waste hours of computing time.computing time.

• Always be on the lookout for ways to Always be on the lookout for ways to make your programs more efficient.make your programs more efficient.

Page 22: The If Then Else Statement Using JavaScript TIP The If Statement is the fundamental control structure that allows branches in the flow of control in a

Blocks (Compound Blocks (Compound Statements)Statements)

• If you put a { } pair around the If you put a { } pair around the sequence of statements you want in sequence of statements you want in a branch of the If statement, the a branch of the If statement, the sequence of statements becomes a sequence of statements becomes a single block.single block.

Page 23: The If Then Else Statement Using JavaScript TIP The If Statement is the fundamental control structure that allows branches in the flow of control in a

Blocks (Compound Blocks (Compound Statements)Statements)

If ( divisor != 0 )If ( divisor != 0 )

results = dividend / divisor;results = dividend / divisor;

elseelse

{{

document.write(“Division by zero is not allowed”);document.write(“Division by zero is not allowed”);

result = 9999;result = 9999;

}}

Page 24: The If Then Else Statement Using JavaScript TIP The If Statement is the fundamental control structure that allows branches in the flow of control in a

The Dangling ElseThe Dangling Else

• When if statements are nested, you may When if statements are nested, you may find yourself confused about the find yourself confused about the If-ThenIf-Then pairings. pairings.

• To which To which IfIf does an does an elseelse belong ? belong ?

• THE RULE: In the absence of braces, an THE RULE: In the absence of braces, an elseelse is always paired with the closest is always paired with the closest ifif that doesn’t already have an that doesn’t already have an elseelse paired paired with it.with it.

Page 25: The If Then Else Statement Using JavaScript TIP The If Statement is the fundamental control structure that allows branches in the flow of control in a

The Dangling ElseThe Dangling Else

if ( average >= 60.0 ) // Incorrect versionif ( average >= 60.0 ) // Incorrect version

if ( average < 70.0 )if ( average < 70.0 )

document.write(“Passing but document.write(“Passing but marginal”);marginal”);

elseelse

document.write(“Failing”);document.write(“Failing”);

Page 26: The If Then Else Statement Using JavaScript TIP The If Statement is the fundamental control structure that allows branches in the flow of control in a

The Dangling ElseThe Dangling ElseIf ( average >= 60.0 ) // correct versionIf ( average >= 60.0 ) // correct version

{{

if ( average < 70.0 )if ( average < 70.0 )

document.write(“Passing but marginal”);document.write(“Passing but marginal”);

}}

elseelse

document.write(“Failing”);document.write(“Failing”);

The { } pair indicates that the inner If statement is complete, so the The { } pair indicates that the inner If statement is complete, so the else must belong to the outer if.else must belong to the outer if.

Page 27: The If Then Else Statement Using JavaScript TIP The If Statement is the fundamental control structure that allows branches in the flow of control in a

SummarySummary

• The The if statementif statement allows you to take allows you to take different paths through a program based different paths through a program based on the value of a logical expression.on the value of a logical expression.

• The The If-Then-ElseIf-Then-Else is used to choose is used to choose between two courses of actionbetween two courses of action

• The The If-ThenIf-Then is used to choose whether or is used to choose whether or not to take a “particular” course of action.not to take a “particular” course of action.

Page 28: The If Then Else Statement Using JavaScript TIP The If Statement is the fundamental control structure that allows branches in the flow of control in a

SummarySummary

• The branches of an The branches of an If-ThenIf-Then or or If-If-Then-ElseThen-Else structure can be any structure can be any statement, simple or compound.statement, simple or compound.

• They can even be other They can even be other IfIf statements.statements.