1 / 22

Ifs

Ifs. Page that asks user for background color and changes fore color from black if user selects black as background color. First part of code. Second part of code. The if structure. //change fore color if user has selected black background if(back == "black") { fore="yellow"; }

ghouse
Download Presentation

Ifs

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

  2. Page that asks user for background color and changes fore color from black if user selects black as background color.

  3. First part of code

  4. Second part of code

  5. The if structure //change fore color if user has selected black background if(back == "black") { fore="yellow"; } • After the keyword “if” comes a condition (a.k.a. a proposition) – something that is either true or false, followed by a statement or possibly a block of statements that are to be executed should the condition be true and not executed otherwise. • (Page 66 Beginning JavaScript, Paul Wilton)

  6. Are you asking me or are you telling me? • The code back = “black” is an assignment – it tells the variable back to take on the value “black” • The code back == “black” is a Boolean expression – it asks whether the variable back is equal to “black” which may be true or false.

  7. Accidentally (on purpose) replacing == with = What happens if we have an assignment where we want a condition?

  8. The variable back is assigned the value “black.” Then the expression evaluates as true meaning the it’s true that the variable back was successfully assigned the value “black.” So then the statements inside the if block are executed, and the fore color is changed to yellow. This is how we ended up with a black back color and yellow fore color even though the user seems to have requested an orange back color.

  9. A problem: Since “Black” is not equal to “black” the fore color is not changed. Hence the back color and fore color end up to be the same and the message cannot be read.

  10. Modified code produces same result whether user enters “black” or “Black”

  11. The Boolean Operator OR if(back == "black" || back=="Black") { fore="yellow"; } • The two vertical lines (above the backslash on the keyboard) denote the Boolean OR operator. The if block is executed if either back equals “black” is true or if back equals “Black” • (Page 74 Beginning JavaScript, Paul Wilton)

  12. The Boolean OR Truth Table

  13. A better way to handle this particular problem: the toUpperCase function

  14. A capital idea if(back.toUpperCase() == "BLACK") { fore="yellow"; } • The pre-defined method toUpperCase() belongs to the String class and returns a string with any lowercase letters turned into the corresponding uppercase letters. • Note that it does not change to variable back to all capitals letters but makes a temporary copy with all capital letters in this case which we use for purposes of comparison. • (Page 122 Beginning JavaScript, Paul Wilton)

  15. Allowing user to respond in another format

  16. Return to OR if(back.toUpperCase() == "BLACK" || back == "#000000") { fore="yellow"; } • Using the OR (||) operator again allows the user to enter the color in the hexadecimal format or in name format. • Note that on each side of the OR is a complete condition and not a set of possible values. Do not use the following if(back.toUpperCase() == "BLACK" || "#000000") //WRONG!!!!

  17. Incorrect ORing (the condition is always true)

  18. References • Beginning JavaScript, Paul Wilton

More Related