Download presentation
Presentation is loading. Please wait.
Published byElvin Todd Modified over 9 years ago
1
TIK-20 Computer and Information Science April 3, 2009 1TIK20 - West Hill C.I. - Spring 2009
2
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
3
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 20093 Number of WordsPrice 1 to 50 words$25.00 51 to 100 words$45.00 101 to 200 words$75.00
4
Today’s Agenda 1.>, =,<=,==,!= 2.for, while 3.IPO and Lesson 7a Homework 4.DO (1) and (2) at your workstations 5.Show Mr. Robinson or Ms. Jansen that your programs are working before you leave. 4TIK20 - West Hill C.I. - Spring 2009
5
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
6
For loops Recall for loops from last class… for (i = 0; i < 5; i++){ document.write(i + “ ”) } 6TIK20 - West Hill C.I. - Spring 2009
7
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
8
Let's begin by looking at a simple while statement: x = 0 // initialize before the loop while (x<10) { document.write(x + " ") x++; // increment the variable inside loop } TIK20 - West Hill C.I. - Spring 20098
9
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 loop while (x<10) { document.write(x + " ") }
10
Do while loop The “test” goes to the bottom of the loop: x = 12 do { document.write(x) x++ } while (x<10) TIK20 - West Hill C.I. - Spring 200910
11
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 200911
12
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
13
Question from Homework 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. 13TIK20 - West Hill C.I. - Spring 2009
14
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 200914
15
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 200915 Number of WordsPrice 1 to 50 words$25.00 51 to 100 words$45.00 101 to 200 words$75.00
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.