1 / 23

E0001 Computers in Engineering

E0001 Computers in Engineering. IF....THEN. Readings. Schneider - Chapter 5 Problems 5.1 and 5.2 Study book. About this lecture. IF... THEN statements relational and logical operators. Problem.

Download Presentation

E0001 Computers in Engineering

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. E0001 Computers in Engineering IF....THEN

  2. Readings • Schneider - Chapter 5 • Problems 5.1 and 5.2 • Study book

  3. About this lecture • IF... THEN statements • relational and logical operators

  4. Problem • A computer store sells discs for $1 each for small orders or at 70 cents for orders of 25 or more. Request the number of discs ordered, calculate and display the total cost.

  5. Flowchart

  6. Program INPUT “enter the number of discs ”; num IF num < 25 THEN cost = num * 1 ELSE cost = num * 0.7 ENDIF PRINT “cost of order is”; cost

  7. IF ... THEN • used to choose between TWO alternative paths • has two general forms • single line • block

  8. Single Line form IF condition is true THEN do one action If the condition is not true control moves to the following line in the program Condition is an expression using relational and/or logical operators

  9. Block form • IF condition true THEN action action END IF • if conditions false control goes to line following END IF

  10. IF THEN ELSESINGLE LINE IF IF cond true THEN action ELSE action BLOCK IF IF conditon true THEN action action ELSE action action END IF

  11. Expression using <, less than >, greater than <=, less than or equal to >=, greater than or equal to =, equal to <>, does not equal Comparison can include NUMBERS STRINGS, based on ASCII order EVALUATES to TRUE or FALSE Relational Operators

  12. Examples IF 3> 5 THEN ..... IF 9.5 >= x THEN.... IF x = y +5 THEN ... IF “aardvark” < “APPLE” THEN ..... IF name$ = “Smith” THEN ....

  13. ASCII • American Standard Code for Information Interchange • most widely used coding system to represent data • unique binary number for each character • 8 bit code • look in qbasic help for ASCII code • see also ASC and CHR$ commands

  14. Logical Operators • allow combinations of conditions • AND, OR, NOT • IF j = 7 AND e > 5 OR x <>0 THEN ...

  15. AND • condition1 AND condition2 • true - if both condition1 AND condition2 are true • false - if either condition1 OR condition2 are false • for example if a = 6, b = 10, c = 2 • IF a > 5 AND b > a THEN ..... • true AND true • TRUE • IF a > 5 AND b < a THEN ..... • true AND false • FALSE

  16. OR • condition1 OR condition2 • true if either condition1 or condition2 are true • false if both condition1 or condition2 are false • for example if a = 6, b = 10, c = 2 • write 2 statements using OR, one evaluating to true, one evaluating to false

  17. NOT • NOT condition1 • true if condition1 is false • false if condition1 is true • OPPOSITE

  18. Operator Hierarchy • arithmetic operations • relational evaluations • logical operators in order: • NOT • AND • OR • brackets can be used for • readability and to change evaluation order

  19. example • a = 5; c$ = “x”; f = 10 IF a = 3 AND c$ = “x” THEN LET f = f + 1 false true false f = 10

  20. d = 3; p = 5; r = 3; t = 5; s = 6; y = 3 IF s>y OR t = d AND p<5 OR NOT y = r THEN determine the output - is the condition true or false? What is done first? IF true OR false AND false OR NOT true THEN IF true OR false AND false OR false THEN IF true OR false OR false THEN IF true OR false THEN IF true THEN

  21. d = 3; p = 5; r = 3; t = 5; s = 6; y = 3 { IF s>y OR t = d AND p<5 OR NOT y = r THEN 1.true 2.false 3. false 4. true and 5. false or 6. false 7. true or 8. true { { { {

  22. OR true OR true = true true OR false = true NOT NOT true = false NOT false = true AND true AND true = true true AND false = false TIP think true = 1; false = 0 OR => + (addition) AND => * (multiply) NOT =>opposite LOGICAL relators

  23. Thats all folks!

More Related