Outline IS400: Development of Business Applications on the Internet Fall 2004 Instructor: Dr. Boris Jukic JavaScript: Control Structures: continued
Objectives In this lesson, you will learn: – To be able to use the for and do … while repetition statements to execute statements in a program repeatedly. – To understand multiple selection using the switch selection statement. – To be able to use the break and continue program-control statements. – To be able to use the logical operators.
Essentials of Counter-Controlled Repetition Counter-controlled repetition – Name of a control – Initial value – Increment or decrement – Final value
for Repetition Statement for repetition statement – Handles all the details of counter-controlled repetition – Contains the counter declaration – Contains the explicit incrementing expression – Contains the loop stopping/continuation condition (final value)
Outline ForCounter.html (1 of 1)
for Repetition Statement for (var counter =1; counter <=7; ++counter ) Initial value of control variable Increment of control variable Control variable name Final value of control variable for which the condition is true for keyword Loop-continuation condition Fig. 9.3 for statement header components.
for Loop : Compound Interest Example The code that will create a table containing showing the total amount of money in each of the next 10 years assuming that – annual interest is 5% – no withdrawals have occurred – Starting principal is $1,000 Amount Year = Principal (1+Interest) Year
Outline Interest.html (1 of 2)
Outline Interest.html (2 of 2)
In-class exercise Modify the previous JavaScript code in following fashion – Instead of fixed principal amount, interest rate and number of years it should prompt user for those three values an create the table accordingly
switch Multiple-Selection Statement Controlling expression – typically a variable with multiple possible values Case labels – Typically, each case label will correspond to one of the possible values of the controlling expression Case block of statements – No brackets needed – Always finish with a break statement Default case – Optional, specifies what to do if the value of control expression is something else than specified in case labels
switch Multiple-Selection Statement case a a action(s) true false... break case b action(s) break false case z z action(s) break default action(s) true case b
Outline SwitchTest.html (1 of 3)
Outline SwitchTest.html (2 of 3) Note that there are NO “{“ and “}” brackets at the beginning of each case block
Outline SwitchTest.html (3 of 3)
In-class exercise 2 Modify the previous JavaScript code in following fashion – Add one more choice for the user to choose from 4 (squared): it should display an unordered list where items are marked by squares instead of bullets. The type declaration is:
Logical Operators More logical operators – Logical AND ( && ) – Logical OR ( || ) – Logical NOT ( ! )
Logical Operators
Outline LogicalOperators.html (1 of 2)
Outline LogicalOperators.html (2 of 2)
Summary of Structured Programming Flowcharts – Reveal the structured nature of programs Single-entry/single-exit control structures – Only one way to enter and one way to exit each control structure Control structure stacking – The exit point of one control structure is connected to the entry point of the next control structure
Recommended Rules of Structured Programming:
Description of “Action” may get more specific
Action being replaced by control structure
Unstructured Flowchart Such outcome should never occur if recommended rules are followed