1 / 34

CSC 1401 S1 Computer Programming I

CSC 1401 S1 Computer Programming I. Hamid Harroud School of Science and Engineering, Akhawayn University h.harroud@aui.ma http://www.aui.ma/~H.Harroud/CSC1401 Spring 2009. Lecture 4. Selection Structures: if and switch Statements. 2020/1/5. Objectives. Control Structure Conditions

jeanettec
Download Presentation

CSC 1401 S1 Computer Programming I

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. CSC 1401 S1Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University h.harroud@aui.ma http://www.aui.ma/~H.Harroud/CSC1401 Spring 2009

  2. Lecture 4 Selection Structures: if and switch Statements 2020/1/5

  3. Objectives Control Structure Conditions TheifStatement The switch Statement 3 2020/1/5

  4. Control Structures Control Structures control the flow of execution Three kinds of execution flow: Sequence The execution of the program is sequential Selection A control structure which chooses alternative to execute Repetition A control structure which repeats a group of statements. We will focus on the selection control structure in this lecture. 4 2020/1/5

  5. Conditions A program may choose among alternative statements by testing the value of key variables. if (grade > 60) printf(“Passed!”) Condition is an expression that is either false (represented by 0) or true (represented by 1). grade > 60 Conditions may contain relational or equalityoperators. 5 2020/1/5

  6. Relational and Equality Operators 6 2020/1/5

  7. Sample Conditions 7 2020/1/5

  8. Logical Operators To form more complicated conditions by the three logical operators &&(and) || (or) ! (not) Logical Expressions is an expression which uses one or more logical operators, e.g., salary < MIN_SALARY || dependents > 5 temperature > 90.0 && humidity > 0.9 !(0 <= n && n<= 100) 8 2020/1/5

  9. && (and), || (or), ! (not) 9 2020/1/5

  10. Operator Precedence An operator’s precedence determines its order of evaluation. - x – y * z x + y < min + max 10 2020/1/5

  11. Evaluation Tree & Step-by-Step Evaluation 11 2020/1/5

  12. Short Circuit Evaluation Stopping evaluation of a logical expression as soon as its value can be determined. e.g. a && b must be false if a is 0. 12 2020/1/5

  13. Writing English Conditions in C 13 2020/1/5

  14. Comparing Characters 14 2020/1/5

  15. Complementing a condition Complement a logical expression with the symbol ! just change its operator item == SENT !(item == SENT) item != SENT !(a <= b) a > b 15 2020/1/5

  16. DeMorgan’s Theorem A way to simplify the logical expression If comp_1 is the complement of expr_1 The complement of expr_1 && expr_2 is comp_1 || comp_2 The complement of expr_1 || expr_2is comp_1 && comp_2. e.g. the complement of age > 25 && (status == ‘S’|| status == ‘D’) age <= 25 ||(status != ‘S’&& status!= ‘D’) 16 2020/1/5

  17. The if statement with 1 or 2 alternatives :: Flowchart 17 2020/1/5

  18. The if statement with 1 or 2 alternatives Syntax if (condition) statement ; else statement ; Ex-1. if (rest_heart_rate > 56 ) printf(“keep up your exercise program!\n”); else printf(“Your heart rate is in excellent health\n”); Ex-2. if (x != 0.0) product = product * x; 18 2020/1/5

  19. Example Write a C program that reads in a number N from the user and then displays its parity (odd or even) 19 2020/1/5

  20. If statement with compound statements if (condition) { true task } else { false task } 20 2020/1/5

  21. Example Write a C program that determines the maximum value in a list of three numbers entered by the user. 21 2020/1/5

  22. Nested if Statements An if statement with another if statement as its true task or its false task 22 2020/1/5

  23. Nested if Example: Road Sign Decision Process 23 2020/1/5

  24. Road Sign Decision Process 24 2020/1/5

  25. Dangling Else Which if is associated with the else? if (x > 0) if (y > 0) a = a + 1; else b = b + 1; 25 2020/1/5

  26. Multiple-Alternative Decision 26 2020/1/5

  27. Order of Conditions in a Multiple-Alternative Decision When more than one condition in a multiple-alternative decision is true, only the first task following the first true condition executes. Textbook examples 27 2020/1/5

  28. Example Write a C program that reads in a grade on a scale of 100 and displays the equivalent letter grade (A, B, C, D or F) 28 2020/1/5

  29. Example Write a C program that asks a user about his or her gender and height and then decides if the user is Short, Medium or Tall based on the decision tree shown below: 29 2020/1/5

  30. Example Write a C Program that reads in three integer numbers; Num1, Num2 and Num3 and displays the list of entered numbers in ascending order. For example, if the user enters the following list of integers: 84 3 55 Your program should display the following: The ordered list is: 3 55 84 30 2020/1/5

  31. The switch statement When the selection is based on the value of a single variable or of a simple expression (called the controlling expression). 31 2020/1/5

  32. Example of a switch Statement 32 2020/1/5

  33. Example Write a program using the switch statement to simulate a calculator that supports the following arithmetic operations: + - * /. You program should start by asking the user to select an arithmetic operation and the two double operands, calculates and then displays the result. 33 2020/1/5

  34. Summary Control Structure Conditions The if Statements The switch Statements 34 2020/1/5

More Related