Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University

Similar presentations


Presentation on theme: "1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University"— Presentation transcript:

1 1 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

2 2015/10/16 Lecture 4 Selection Structures: if and switch Statements

3 2015/10/16 3 Objectives Control Structure Conditions The if Statement The switch Statement

4 2015/10/16 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.

5 2015/10/16 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 equality operators.

6 2015/10/16 6 Relational and Equality Operators

7 2015/10/16 7 Sample Conditions

8 2015/10/16 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 5 temperature > 90.0 && humidity > 0.9 !(0 <= n && n<= 100)

9 2015/10/16 9 && (and), || (or), ! (not)

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

11 2015/10/16 11 Evaluation Tree & Step-by-Step Evaluation

12 2015/10/16 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.

13 2015/10/16 13 Writing English Conditions in C

14 2015/10/16 14 Comparing Characters

15 2015/10/16 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

16 2015/10/16 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_2 is comp_1 && comp_2. e.g. the complement of age > 25 && (status == ‘S’|| status == ‘D’) age <= 25 ||(status != ‘S’&& status!= ‘D’)

17 2015/10/16 17 The if statement with 1 or 2 alternatives :: Flowchart

18 2015/10/16 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;

19 2015/10/16 19 Example Write a C program that reads in a number N from the user and then displays its parity (odd or even)

20 2015/10/16 20 If statement with compound statements if (condition) { true task } else { false task }

21 2015/10/16 21 Example Write a C program that determines the maximum value in a list of three numbers entered by the user.

22 2015/10/16 22 Nested if Statements An if statement with another if statement as its true task or its false task

23 2015/10/16 23 Nested if Example: Road Sign Decision Process

24 2015/10/16 24 Road Sign Decision Process

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

26 2015/10/16 26 Multiple-Alternative Decision

27 2015/10/16 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

28 2015/10/16 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)

29 2015/10/16 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:

30 2015/10/16 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

31 2015/10/16 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).

32 2015/10/16 32 Example of a switch Statement

33 2015/10/16 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.

34 2015/10/16 34 Summary Control Structure Conditions The if Statements The switch Statements


Download ppt "1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University"

Similar presentations


Ads by Google