340 likes | 355 Views
Learn about selection structures, conditions, logical operators, operator precedence, DeMorgan’s Theorem, if statements, examples, nested if statements, road sign decision process, switch statements, and more in C programming.
E N D
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
Lecture 4 Selection Structures: if and switch Statements 2020/1/5
Objectives Control Structure Conditions TheifStatement The switch Statement 3 2020/1/5
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
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
Relational and Equality Operators 6 2020/1/5
Sample Conditions 7 2020/1/5
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
&& (and), || (or), ! (not) 9 2020/1/5
Operator Precedence An operator’s precedence determines its order of evaluation. - x – y * z x + y < min + max 10 2020/1/5
Evaluation Tree & Step-by-Step Evaluation 11 2020/1/5
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
Writing English Conditions in C 13 2020/1/5
Comparing Characters 14 2020/1/5
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
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
The if statement with 1 or 2 alternatives :: Flowchart 17 2020/1/5
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
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
If statement with compound statements if (condition) { true task } else { false task } 20 2020/1/5
Example Write a C program that determines the maximum value in a list of three numbers entered by the user. 21 2020/1/5
Nested if Statements An if statement with another if statement as its true task or its false task 22 2020/1/5
Nested if Example: Road Sign Decision Process 23 2020/1/5
Road Sign Decision Process 24 2020/1/5
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
Multiple-Alternative Decision 26 2020/1/5
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
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
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
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
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
Example of a switch Statement 32 2020/1/5
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
Summary Control Structure Conditions The if Statements The switch Statements 34 2020/1/5