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.

Slides:



Advertisements
Similar presentations
Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
Advertisements

Chapter 4: Control Structures I (Selection)
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 5 Selection Statements Animated Version.
Decisions If statements in C.
The "if structure" is used to execute statement(s) only if the given condition is satisfied.
CS&E 1111 ExIFs IFs and Nested IFs in Excel Objectives: Using the If function in spreadsheets Nesting If functions.
Logic & program control part 3: Compound selection structures.
Python Programming Language
3 Decision Making: Equality and Relational Operators A condition is an expression that can be either true or false. Conditions can be formed using the.
Selection (decision) control structure Learning objective
Flow Control if, while, do-while Juan Marquez (03_flow_control.ppt)
Objectives AND logic OR logic Evaluating compound conditions with multiple logical operators Precedence when combining AND and OR operators Efficiency.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
James Tam Making Decisions In Python In this section of notes you will learn how to have your Pascal programs choose between alternative courses of action.
2-1 Making Decisions Sample assignment statements PayAmount = Hours * Rate PayAmount = 40*Rate + (Hours – 40)*Rate*1.5.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
true (any other value but zero) false (zero) expression Statement 2
1 Selection Structures. 2 Making Decisions Sample assignment statements to figure worker pay with possible overtime PayAmount = Hours * Rate PayAmount.
Programming Logic and Design Sixth Edition
Chapter 3 Making Decisions
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Structural Program Development: If, If-Else Outline.
UniMAP Sem II-09/10EKT120: Computer Programming1 Week 3 – Selection Structures.
Institute for Personal Robots in Education (IPRE)‏ CSC 170 Computing: Science and Creativity.
1. We’ve learned that our programs are read by the compiler in order, from top to bottom, just as they are written The order of statement execution is.
Controlling Execution Programming Right from the Start with Visual Basic.NET 1/e 8.
CHAPTER 4: Selection Control Structure. Objectives  Use the relational comparison operators  Learn about AND logic  Learn about OR logic  Make selections.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
Chapter 5 Logic; Got Any?. Flow of Control The order in which the computer executes statements in a program Control Structure A statement used to alter.
Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement.
Computer Science: A Structured Programming Approach Using C1 5-2 Two-Way Selection The decision is described to the computer as a conditional statement.
Computer Science: A Structured Programming Approach Using C1 5-2 Two-Way Selection The decision is described to the computer as a conditional statement.
Decisions in Python Boolean functions. A Boolean function This is a function which returns a bool result (True or False). The function can certainly work.
While and If-Else Loops ROBOTC Software. While Loops While loop is a structure within ROBOTC Allows a section of code to be repeated as long as a certain.
Week 4 Program Control Structure
An Object-Oriented Approach to Programming Logic and Design Chapter 5 Making Decisions.
Condition Testing. Condition testing is a test case design method that exercises the logical conditions contained in a program module. A simple condition.
Decision Making and Branching
Conditional statements and boolean expressions Arithmetic, relational and logical operators.
CHAPTER 3 CONTROL STRUCTURES ( SELECTION ) I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
Principles of Programming - NI July Chapter 4: Basic C Operators In this chapter, you will learn about: Assignment operators Arithmetic operators.
Control Flow (Python) Dr. José M. Reyes Álamo.
More on the Selection Structure
Sequence, Selection, Iteration The IF Statement
Condition Testing.
Selection—Making Decisions
Operator Precedence Operators Precedence Parentheses () unary
DKT121: Fundamental of Computer Programming
The order in which statements are executed is called the flow of control. Most of the time, a running program starts at the first programming statement,
Programming Fundamentals
JavaScript: Control Statements I
Control Structures.
Microsoft Visual Basic 2005 BASICS
Chapter 4 Control Statements: Part I
Conditionals & Boolean Expressions
2-1 Making Decisions Sample assignment statements
While Loops and If-Else Structures
Scratch: selection / branching/ if / If…else / compound conditionals / error trapping by Mr. Clausen.
Visual Basic – Decision Statements
Selection Statements.
Chapter 4: Control Structures I (Selection)
Conditional Logic Presentation Name Course Name
Python Programming Language
Chapter8: Statement-Level Control Structures April 9, 2019
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
CHAPTER 5: Control Flow Tools (if statement)
CSC215 Lecture Control Flow.
Flow of Control Flow of control is the order in which a program performs actions. Up to this point, the order has been sequential. A branching statement.
Presentation transcript:

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 program.

The If-Then-Else Statement With it, we can ask a question and choose a course of action.With it, we can ask a question and choose a course of action. If a certain condition exists, then perform one action, else perform another action.If a certain condition exists, then perform one action, else perform another action. The computer performs just one of the two actions, depending on the result of the condition being tested.The computer performs just one of the two actions, depending on the result of the condition being tested.

The If-Then-Else form if ( Expression ) Statement-A else Statement-B

If ( expression ) The expression in parentheses can be of any simple data type. Almost without exception this will be a logical (Boolean) expression.The expression in parentheses can be of any simple data type. Almost without exception this will be a logical (Boolean) expression. The expression gets evaluated at run time and based on the condition tested, results in being either TRUE or FALSE.The expression gets evaluated at run time and based on the condition tested, results in being either TRUE or FALSE.

If ( Expression ) Statement-A else Statement-B At run time, the computer evaluates the expression.At run time, the computer evaluates the expression. If the value is TRUE, Statement-A is executed.If the value is TRUE, Statement-A is executed. If the value is FALSE, Statement-B is executed.If the value is FALSE, Statement-B is executed.

If-Then-Else Flow of Control false true

Notice In JavaScript the If statement uses the reserved words if and else but does not include the word then.In JavaScript the If statement uses the reserved words if and else but does not include the word then. The term If-Then-Else corresponds to how we say things in English: “If something is true, then do this, else do that.”The term If-Then-Else corresponds to how we say things in English: “If something is true, then do this, else do that.”

Code Fragment to illustrate If-Then-Else If ( hours <= 40.0 ) pay = rate * hours; else pay = rate * ( ( hours ) * 1.5 ); document.write(pay + “ ”); document.write(pay + “ ”); Observe the indentation of the then-clause and the else-clause, which makes it easy to read.

If-Then-Else Flow of Control false true

Other Uses for If-Then-Else The If-Then-Else often is used to check the validity of input. For example, before we can ask the computer to divide by a data value, we should be sure that the value is not zero.The If-Then-Else often is used to check the validity of input. For example, before we can ask the computer to divide by a data value, we should be sure that the value is not zero. If you try, most computers will halt the execution of the program.If you try, most computers will halt the execution of the program.

Code for checking User Input If ( divisor != 0 ) result = dividend / divisor; else document.write(“Division by zero is not allowed”);

The If-Then Form Sometimes you run into a situation where you want to say. “If a certain condition exists, then perform some action; otherwise, don’t do anything.”Sometimes you run into a situation where you want to say. “If a certain condition exists, then perform some action; otherwise, don’t do anything.” In other words, you want the computer to skip a sequence of instructions if a certain condition isn’t met.In other words, you want the computer to skip a sequence of instructions if a certain condition isn’t met. You can do this by leaving the else branch empty.You can do this by leaving the else branch empty.

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

Example of If-Then Form if (age < 18) document.write(“Not an eligible”); document.write(“ voter.”); This statement means that if age is less than 18, first print “Not an eligible “ and then print “voter.” If age is not less than 18, skip the first statement and go directly to print “voter.” This statement means that if age is less than 18, first print “Not an eligible “ and then print “voter.” If age is not less than 18, skip the first statement and go directly to print “voter.”

If-Then Flow of Control false true

Nested If Statments There are no restrictions on what the statements in an If can be.There are no restrictions on what the statements in an If can be. In general, any problem that involves a multiway branch (more than two alternative courses of action) can be coded using nested If statements.In general, any problem that involves a multiway branch (more than two alternative courses of action) can be coded using nested If statements.

Nested If Statements if ( month == 1 ) document.write(“January”); if ( month == 2 ) document.write(“February”); if ( month == 3 ) document.write( “March”);... if ( month == 12 ) document.write( “December”);

Equivalent Nested If Statements if ( month == 1 ) document.write( “January”); document.write( “January”);else if ( month == 2 ) document.write( “February”); else if ( month == 3 ) document.write( “March”); else if ( month == 4 ) // and so on…....

Nested If Statments The preceding Nested If Structure is more efficient because it makes fewer comparisons.The preceding Nested If Structure is more efficient because it makes fewer comparisons. The first version --- the sequence of independent If statements --- always tests every condition ( all 12 of them, even if the first one is satisfied.The first version --- the sequence of independent If statements --- always tests every condition ( all 12 of them, even if the first one is satisfied.

Nested If Statments In contrast, the nested if solution skips all remaining comparisons after one alternative has been selected.In contrast, the nested if solution skips all remaining comparisons after one alternative has been selected. What do you think the advantage is using the second example ?What do you think the advantage is using the second example ?

Nested If Statements As fast as modern computers are, many applications require so much computation that inefficient algorithms can waste hours of computing time.As fast as modern computers are, many applications require so much computation that inefficient algorithms can waste hours of computing time. Always be on the lookout for ways to make your programs more efficient.Always be on the lookout for ways to make your programs more efficient.

Blocks (Compound Statements) If you put a { } pair around the sequence of statements you want in a branch of the If statement, the sequence of statements becomes a single block.If you put a { } pair around the sequence of statements you want in a branch of the If statement, the sequence of statements becomes a single block.

Blocks (Compound Statements) If ( divisor != 0 ) results = dividend / divisor; else{ document.write(“Division by zero is not allowed”); result = 9999; }

The Dangling Else When if statements are nested, you may find yourself confused about the If-Then pairings.When if statements are nested, you may find yourself confused about the If-Then pairings. To which If does an else belong ?To which If does an else belong ? THE RULE: In the absence of braces, an else is always paired with the closest if that doesn’t already have an else paired with it.THE RULE: In the absence of braces, an else is always paired with the closest if that doesn’t already have an else paired with it.

The Dangling Else if ( average >= 60.0 ) // Incorrect version if ( average < 70.0 ) document.write(“Passing but marginal”); elsedocument.write(“Failing”);

The Dangling Else If ( average >= 60.0 ) // correct version { if ( average < 70.0 ) document.write(“Passing but marginal”); document.write(“Passing but marginal”);}elsedocument.write(“Failing”); The { } pair indicates that the inner If statement is complete, so the else must belong to the outer if. The { } pair indicates that the inner If statement is complete, so the else must belong to the outer if.

Summary The if statement allows you to take different paths through a program based on the value of a logical expression.The if statement allows you to take different paths through a program based on the value of a logical expression. The If-Then-Else is used to choose between two courses of actionThe If-Then-Else is used to choose between two courses of action The If-Then is used to choose whether or not to take a “particular” course of action.The If-Then is used to choose whether or not to take a “particular” course of action.

Summary The branches of an If-Then or If-Then- Else structure can be any statement, simple or compound.The branches of an If-Then or If-Then- Else structure can be any statement, simple or compound. They can even be other If statements.They can even be other If statements.