tik-20 computer and information science
DESCRIPTION
TIK-20 Computer and Information Science. April 3, 2009. Lesson 7a: Conditional Loops Homework. - PowerPoint PPT PresentationTRANSCRIPT
TIK-20 Computer and Information Science
April 3, 2009
1TIK20 - West Hill C.I. - Spring 2009
Lesson 7a: Conditional Loops Homework
1. A program accepts the price of a new car, trade-in-allowance, and down payment, and then tells how much is owing on the car. The program continues to ask the user if he/she would like to continue entering values (with a confirm dialogue box--see script examples for an explanation of what they are and how they work) and continues to perform the calculations if the user clicks OK, but exits if the person clicks CANCEL.
2TIK20 - West Hill C.I. - Spring 2009
Lesson 7a: Conditional Loops Homework
• Write a program segment containing a loop for a program that accepts the number of words in a classified ad as input and calculates the price of the ad based on the following criteria (using selection statements):
The output should be in the following format: The West Hill Classifieds The cost of your classified ad is $__________. The loop should continue to accept values until the user clicks CANCEL at the
confirm dialogue box prompt.
TIK20 - West Hill C.I. - Spring 2009 3
Number of Words Price
1 to 50 words $25.00
51 to 100 words $45.00
101 to 200 words $75.00
Today’s Agenda
1. >,<,>=,<=,==,!=2. for, while3. IPO and Lesson 7a Homework4. DO (1) and (2) at your workstations5. Show Mr. Robinson or Ms. Jansen that your
programs are working before you leave.
4TIK20 - West Hill C.I. - Spring 2009
A=1; B=1; C=2; D=2; E=3
Tell me if the following are true or false
A > B || C >= D true or false
B < C && C != D true or false
A <= B || B == E true or false
5TIK20 - West Hill C.I. - Spring 2009
For loops
Recall for loops from last class…
for (i = 0; i < 5; i++){ document.write(i + “<BR>”)}
6TIK20 - West Hill C.I. - Spring 2009
Now the while loop
Easier to use than the for loop is the while loop.
A while loop doesn't initialize or increment any fields automatically as part of the command.
It just tests a condition and executes the loop for as long as the condition remains true.
7TIK20 - West Hill C.I. - Spring 2009
Let's begin by looking at a simple while statement:
x = 0 // initialize before the loopwhile (x<10) { document.write(x + "<BR>") x++; // increment the variable inside loop}
TIK20 - West Hill C.I. - Spring 2009 8
Common errors
9TIK20 - West Hill C.I. - Spring 2009
Forget to do something to the value that is being tested. What’s wrong here?
x = 0 // initialize before the loopwhile (x<10) { document.write(x + "<BR>")}
Do while loop
The “test” goes to the bottom of the loop:
x = 12do {
document.write(x)x++
} while (x<10)
TIK20 - West Hill C.I. - Spring 2009 10
Do while loop - note
In this example the loop will execute once even though the original value of x is greater than that tested for in the while condition.
TIK20 - West Hill C.I. - Spring 2009 11
Back to Lesson 7a
Recall an IPO
Input: Input from the keyboard
Processing: Operations carried out by the computer
Output: Variables (storing data) that will be output to the screen
12TIK20 - West Hill C.I. - Spring 2009
Question from HomeworkA program accepts the price of a new car, trade-in-
allowance, and down payment, and then tells how much is owing on the car. The program continues to ask the user if he/she would like to continue entering values (with a confirm dialogue box--see script examples for an explanation of what they are and how they work) and continues to perform the calculations if the user clicks OK, but exits if the person clicks CANCEL.
13TIK20 - West Hill C.I. - Spring 2009
Translate to Javascript
response = “”// Cancel button pressed??? while(response != null){ document.write(response) response=prompt("How are you today? ","")}
TIK20 - West Hill C.I. - Spring 2009 14
Lesson 7a: Conditional Loops Homework
• Write a program segment containing a loop for a program that accepts the number of words in a classified ad as input and calculates the price of the ad based on the following criteria (using selection statements):
The output should be in the following format: The West Hill Classifieds The cost of your classified ad is $__________. The loop should continue to accept values until the user clicks CANCEL at the
confirm dialogue box prompt.
TIK20 - West Hill C.I. - Spring 2009 15
Number of Words Price
1 to 50 words $25.00
51 to 100 words $45.00
101 to 200 words $75.00