Presentation is loading. Please wait.

Presentation is loading. Please wait.

Beginning C Lecture 3 Lecturer: Dr. Zhao Qinpei

Similar presentations


Presentation on theme: "Beginning C Lecture 3 Lecturer: Dr. Zhao Qinpei"— Presentation transcript:

1 Beginning C Lecture 3 Lecturer: Dr. Zhao Qinpei

2 Making decisions How to make decisions based on arithmetic comparisons
What logical operators are and how you can use them Reading data from the keyboard A calculator Beiginning C / Qinpei Zhao 2019/4/10

3 Decisions Play a game? Beiginning C / Qinpei Zhao 2019/4/10

4 If statement If it is a political lesson, I’ll stay in bed. If it is a programming class, I’ll go to class. If today is a public holiday, there is no class. Else, everything continues. Multiple conditions: If it is Monday, I’ll have class A; If it is Tuesday, I’ll have class B; … If it is Sunday, I’ll stay at home. Beginning C / Qinpei Zhao 2019/4/10

5 Arithmetic comparisons and expression
< is less than; < = is less than or equal to = = is equal to; ! = is not equal to > is greater than; > = is greater than or equal to expressions involving relational operators: logical expression / Boolean expressions _Bool result = 1 < 5; (true / nonzero) _Bool result = 2 = = 3; (false / 0) bool result = 5 >= (1+5); (false / 0) (#include <stdbool.h>) ‘Z’ >= ‘A’ ? 逻辑表达式、布尔运算 Beginning C / Qinpei Zhao 2019/4/10

6 The basic if statement if(expression) statement1; next_statement; if(expression) statement1; Beginning C / Qinpei Zhao 2019/4/10

7 The basic if statement (cont.)
Guessing numbers Check if age is a non-zero value Beginning C / Qinpei Zhao 2019/4/10

8 Extending the if statement: if-else
if(expression) statement1; else statement2; next_statement; Beginning C / Qinpei Zhao 2019/4/10

9 Using blocks of code in if statements
Enclose a block of statements between braces { } If the weather is sunny, I will walk to the park, eat a picnic, and walk home. Else I will stay in, watch football, and drink beer. Beginning C / Qinpei Zhao 2019/4/10

10 Nested if statements Nested ifs: have ifs within ifs.
If the weather is good, I will go out in the yard. And if it’s cool enough, I will sit in the sun. Else I will sit in the shade. I will stay indoors. I will then drink some lemon tea. Beginning C / Qinpei Zhao 2019/4/10

11 Logical operators (逻辑运算符) - AND &&
The AND operator && - combines two logical expressions Binary operator 二目/元运算符 Beginning C / Qinpei Zhao 2019/4/10

12 Logical operators (逻辑运算符) - OR ||
The OR operator || - check for any of two or more conditions being true Beginning C / Qinpei Zhao 2019/4/10

13 Logical operators (逻辑运算符) - NOT !
Unary operator 一目/元运算符 The NOT operator ! – reverse the value of a logical expression: true becomes false, and false becomes true Beginning C / Qinpei Zhao 2019/4/10

14 Verify uppercase letters
Beginning C / Qinpei Zhao 2019/4/10

15 The conditional operator
Ternary operator 三目/元运算符 The conditional operator – evaluates one of two expressions depending on whether a logical expression evaluates true or false Beginning C / Qinpei Zhao 2019/4/10

16 Operator precedence (运算符优先级)
Suppose you are to process job applications and you want to only accept applicants who are 25 or older and have graduated from Tongji or Fudan. Age >= 25 && Tongji || Fudan (Age >= 25 && Tongji ) || Fudan Age >= 25 && (Tongji || Fudan) Beginning C / Qinpei Zhao 2019/4/10

17 Operator precedence (运算符优先级)
All the comparison operators are below the binary arithmetic operators. The binary logical operators are below the comparison operators. Assignments come last in the list. The conditional operator squeezes in just above the assignment operators. The ! Operator is highest within the set of logical operators. arithmetic > comparisons > logical combinations > conditional operator > assignments See Table 3-2 Beginning C / Qinpei Zhao 2019/4/10

18 Using logical operators without confusion
Suppose you are to process job applications and you want to only accept applicants who meet certain educational specifications. An applicant who meets any of the following criteria should be accepted for an interview: Graduates over 25 who studied computer science and who didn’t graduate from Fudan Graduates from Fudan who studied computer science Graduates from Tongji who studied economics and aren’t older than 28 Graduates from Fudan who are over 25 and who didn’t study computer science Beginning C / Qinpei Zhao 2019/4/10

19 Multiple choices (else-if)
Beginning C / Qinpei Zhao 2019/4/10

20 Multiple choices (switch-case)
Beginning C / Qinpei Zhao 2019/4/10

21 The goto statement Confusing: difficult to follow
Avoid the goto statement as far as possible Beginning C / Qinpei Zhao 2019/4/10

22 Bitwise operators Operate on the bits in integer values
Bitwise operators Operate on the bits in integer values ~ unary operator (applies to one operand) Others are binary operator Beginning C / Qinpei Zhao 2019/4/10

23 Bitwise operator – AND &
both bits are 1, the resulting bit is 1; otherwise, the resulting bit is 0. int x = 13; int y = 6; int z = x & y; the bitwise operator v.s. the logical operator x & y v.s. x && y Beginning C / Qinpei Zhao 2019/4/10

24 Other bitwise operators
results in 1 if either or both of the corresponding bits are 1; otherwise, the resulting bit is 0. Exclusive OR ^ Produces a 1 if both bits are different 0 if they’re the same Unary operator ~ Flips the bits of its operand, so 1 becomes 0, and 0 becomes 1. Beginning C / Qinpei Zhao 2019/4/10

25 Designing a program Calculator
The problem is to write a simple calculator that can add, substract, multiply, divide, and find the remainder when one number is divided by another. The program must allow the calculation that is to be performed to be keyed in a natural way, such as 5.6*27 or Calculator Get the user’s input for the calculation that the user wants the computer to perform Check that input to make sure that it’s understandable Perform the calculation Display the result Beginning C / Qinpei Zhao 2019/4/10


Download ppt "Beginning C Lecture 3 Lecturer: Dr. Zhao Qinpei"

Similar presentations


Ads by Google