computer science engineering 150a
play

Computer Science & Engineering 150A Introduction Problem - PDF document

CSCE150A CSCE150A Computer Science & Engineering 150A Introduction Problem Solving Using Computers Introduction Relational Relational Operators Operators Lecture 04 - Conditionals Control Structure Logical Logical Operators


  1. CSCE150A CSCE150A Computer Science & Engineering 150A Introduction Problem Solving Using Computers Introduction Relational Relational Operators Operators Lecture 04 - Conditionals Control Structure Logical Logical Operators Operators Conditions Comparing Comparing Characters Characters if statements if Statement Stephen Scott if Statement Nested if (Adapted from Christopher M. Bourke) Nested if Statements Statements switch switch Statement Statement Review Review Fall 2009 1 / 56 2 / 56 Control Structure Sequential Flow Compound statement : CSCE150A CSCE150A Written as a group of statements Bracketed by { and } Introduction Control structures : Introduction Relational Relational Used to specify sequential flow Control the flow of execution in a program or function. Statement01; Operators Operators Enable you to combine individual instructions into a single logical unit All statements are unconditionally Logical Logical with one entry point (i.e. int main(void) { ) and one exit point Operators Operators executed ( return 0; } ). Comparing Comparing Characters Characters Order is important Statement02; Three kinds of structures to control execution flow: if Statement if Statement Sequence Nested if Nested if Statements Selection Statements switch Repetition switch Statement03; Statement Statement Review Review . . . 3 / 56 4 / 56 Selection Flow Selection Flow – Conditions CSCE150A CSCE150A Definition Selection control structure : A condition is an expression that is either true or false . Evaluates criteria to determine Introduction Introduction which alternative “path” to Relational Relational A program chooses alternative paths of computation by testing one or Operators follow. Operators Selection more conditions. Logical Control Logical A control structure determines Operators Operators Structure which statement(s) to execute Comparing Comparing (ConditionEval == 1 ) → true , Characters Characters Statements are mutually (ConditionEval == 0 ) → false . if Statement if Statement Statement01; Statement02; exclusive Nested if Nested if The resting heart rate is a good indicator of health Statements Statements if (resting heart rate < 75) then you are in good health. switch switch Statement Statement . . if resting heart rate is 80, ConditionEval is false . . Review Review if resting heart rate is 50, ConditionEval is true . if resting heart rate is 75, what is ConditionEval? 5 / 56 6 / 56

  2. Relational and Equality Operators Relational and Equality Operators CSCE150A CSCE150A Conditions come in four forms: Operator Meaning Type variable relational-operator variable Introduction Introduction Relational < less than relational Relational Example: if(numberOfStudents > numberOfSeats) Operators Operators > greater than relational Logical Logical variable relational-operator CONSTANT Operators Operators <= less than or equal to relational Example: if(numberOfStudents < 5) Comparing greater than or equal to relational Comparing >= Characters Characters equal to equality variable equality-operator variable == if Statement if Statement not equal to equality != Example: if(numberOfStudents == numberOfSeats) Nested if Nested if Statements Statements variable equality-operator CONSTANT Table: Relational and Equality Operators in C switch switch Statement Statement Example: if(averageGrade == 75.0) Review Review What about more than one condition? (Example: 0 ≤ x ≤ 10 ) 7 / 56 8 / 56 Logical Operators Logical Operators CSCE150A CSCE150A Logical Expressions - expressions that involve conditional statement(s) Logical Operators : Operators that can combine conditions to make Introduction Introduction and logical operator(s). more complicated selection statements. Relational Relational Operators Operators Examples: Logical Logical C Syntax Meaning True When Operators Operators (x >= 0 && x <=10) && logical And Both are true Comparing Comparing Characters Characters (temperature > 90.0 && humidity > 0.90) || logical Or Either is true if Statement if Statement ! logical Not ( negation ) False !(x >= 0 && x <=10) Nested if Nested if Statements Statements Table: Logical Operators in C What about the following: Are we going to go or not? switch switch Statement Statement (go || !go) Review Review 9 / 56 10 / 56 Tautologies & Contradictions Distributivity CSCE150A CSCE150A The logical And can be distributed over a logical expression just as multiplication can be over an algebraic expression. Introduction Introduction A tautology is a logical expression that is always true a ( b + c ) = ab + ac Relational Relational a && (b || c) is same as (a && b) || (a && c) Operators Any non-zero constant ( 1, 1.5, 8 , etc.) Operators An expression that, when simplified, always ends up being true (Here, a , b , and c are relations like x < 5 ) Logical Logical Operators Operators (go || !go) is always true When distributing the logical Not , And and Or are reversed! Comparing Comparing A contradiction is a logical expression that is always false Characters Characters Example: if Statement The zero constant ( 0 ) if Statement !(x >= 0 && x <=10) Nested if An expression that, when simplified, always ends up being false Nested if (!(x >= 0) || !(x <=10)) Statements Statements (go && !go) is always false ((x < 0) || (x > 10)) switch switch Statement Statement Review Review Best to simplify logical expressions as much as possible, but more important to keep code readable. 11 / 56 12 / 56

  3. True and False Operator Tables C Convention Logical And CSCE150A CSCE150A The result of taking a logical And with two operands is true if and only if Introduction Introduction For convenience when writing we identify zero with false and one both operands are true . Otherwise it is false . Relational Relational with true Operators Operators Logical Logical C does not recognize the words true, false Operators Operators Operand A Operand B Result C has no built-in Boolean type! Comparing Comparing 0 0 0 Characters Characters Instead, zero is identified with false 0 1 0 if Statement if Statement 1 0 0 Any non-zero value is identified with true Nested if Nested if Statements Statements 1 1 1 Example: -1, 0.01, 386 are all true switch switch Statement Statement Review Review 13 / 56 14 / 56 Operator Tables Operator Tables Logical And Logical And CSCE150A CSCE150A The result of taking a logical Or with two operands is true if and only if Introduction Introduction You can only apply a logical Not to a single operand. The result is that at least one of the operands is true . Otherwise it is false . Relational Relational Operators Operators true gets flipped to false and vice versa. Logical Logical Operators Operand A Operand B Result Operators Comparing Comparing Operand Result 0 0 0 Characters Characters 0 1 0 1 1 if Statement if Statement 1 0 1 0 1 Nested if Nested if Statements Statements 1 1 1 switch switch Statement Statement Review Review 15 / 56 16 / 56 Operator Precedence Short-Circuiting Order of precedence for operators CSCE150A CSCE150A Precedence Operator If the first operand of a logical Or is true, the whole expression is Introduction Introduction High Function calls true regardless of the second operand. Relational Relational Operators Operators ! + - & (unary) Similarly, if the first operand of a logical And is false, the whole Logical Logical * / % expression is false regardless of the second operand. Operators Operators + - (binary) Comparing Comparing (true || anything) is true Characters Characters < <= >= > (false && anything) is false if Statement if Statement == != By convention, in either case C does not bother to evaluate the Nested if Nested if Statements && Statements second operand. switch || switch This is known as short-circuiting Statement Statement Low = Review Review Table: Order of Precedence for Operators 17 / 56 18 / 56

Recommend


More recommend