1 / 15

TIK-20 Computer and Information Science

TIK-20 Computer and Information Science. April 3, 2009. Lesson 7a: Conditional Loops Homework.

ashanti
Download Presentation

TIK-20 Computer and Information Science

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. TIK-20 Computer and Information Science April 3, 2009 TIK20 - West Hill C.I. - Spring 2009

  2. Lesson 7a: Conditional Loops 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. TIK20 - 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 2009

  4. Today’s Agenda • >,<,>=,<=,==,!= • for, while • IPO and Lesson 7a Homework • DO (1) and (2) at your workstations • Show Mr. Robinson or Ms. Jansen that your programs are working before you leave. TIK20 - 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 TIK20 - West Hill C.I. - Spring 2009

  6. For loops Recall for loops from last class… for (i = 0; i < 5; i++){ document.write(i + “<BR>”) } TIK20 - 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. TIK20 - 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 + "<BR>") x++; // increment the variable inside loop } TIK20 - West Hill C.I. - Spring 2009

  9. Common errors 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 + "<BR>") } TIK20 - West Hill C.I. - Spring 2009

  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 2009

  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 2009

  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 TIK20 - 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. TIK20 - 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 2009

  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 2009

More Related