1 CSE1301 Computer Programming Lecture 7 Booleans Linda M c Iver.

Slides:



Advertisements
Similar presentations
Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
Advertisements

AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
1 CSE1301 Computer Programming Lecture 8 Selection.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
1 9/24/07CS150 Introduction to Computer Science 1 Relational Operators and the If Statement.
1 CSE1301 Computer Programming Lecture 9 Selection.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
CS 1400 Chapter 4, sections Relational operators Less than< Greater than> Less than or equal= Equal== Not equal!=
FIT Objectives By the end of this lecture, students should: understand selection statements understand nested selection understand tradeoff.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Conditional Statements Control Structures.
1 Lecture 7:Control Structures I (Selection) Introduction to Computer Science Spring 2006.
CS 3850 Lecture 5 Operators. 5.1 Binary Arithmetic Operators Binary arithmetic operators operate on two operands. Register and net (wire) operands are.
1 CS150 Introduction to Computer Science 1 Relational Operators and the If Statement 9/22/08.
CS 1400 Jan 2007 Chapter 4, sections Relational operators Less than< Greater than> Less than or equal= Equal== Not equal!=
Visual C++ Programming: Concepts and Projects
1 Revision of IO Streams printf scanf. 2 CSE1301 Computer Programming Lecture 8 Booleans.
1 Chapter Four Boolean Expressions and Control Statements.
Chapter 4: Basic C Operators
Ryan Chu. Arithmetic Expressions Arithmetic expressions consist of operators, operands, parentheses, and function calls. The purpose is to specify an.
CSci 125 Lecture 10 Martin van Bommel. Simple Statements Expression followed by semicolon Assignments total = n1 + n2; Function calls printf(”Hello.\n”);
Control Structures – Selection Chapter 4 2 Chapter Topics  Control Structures  Relational Operators  Logical (Boolean) Operators  Logical Expressions.
CMSC 104, Version 8/061L11Relational&LogicalOps.ppt Relational and Logical Operators Topics Relational Operators and Expressions The if Statement The if-else.
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1) Linda M c Iver.
Flow of Control Part 1: Selection
April 6, 1998CS102-02Lecture 2-1 Java Operators CS Lecture 2-1 Being a Smooth Operator.
CHAPTER 4: Selection Control Structure. Objectives  Use the relational comparison operators  Learn about AND logic  Learn about OR logic  Make selections.
Introduction to Computing Lecture 05: Selection (continued) Introduction to Computing Lecture 05: Selection (continued) Assist.Prof.Dr. Nükhet ÖZBEK Ege.
1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
CSE 1301 Lecture 8 Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Conditional Expressions
Relational and Boolean Operators CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Principles of Programming Chapter 4: Basic C Operators  In this chapter, you will learn about:  Arithmetic operators  Unary operators  Binary operators.
Booleans Lecture 26. Summary of previous lecture In the previous lecture, we have been learnt,  The if statement  The else statement  Cascaded if 
If Statements Programming. COMP104 Lecture 7 / Slide 2 Review: Rules for Division l C++ treats integers different than doubles. 100 is an int. l 100.0,
CSCI 171 Presentation 3. Operators Instructs C to perform some operation Assignment = Mathematical Relational Logical.
1 Lecture03: Control Flow 9/24/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
CSE202: Lecture 5The Ohio State University1 Selection Structures.
ICS102 Lecture 8 : Boolean Expressions King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science.
Random Functions Selection Structure Comparison Operators Logical Operator
Rational Expressions relational operators logical operators order of precedence.
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Prof. Jeremy.
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Based on slides by Shawn.
CMSC201 Computer Science I for Majors Lecture 03 – Operators
Revision of IO Streams printf scanf.
CSE 220 – C Programming Expressions.
Lecture 3 Java Operators.
Operators And Expressions
Operators and Expressions
Making Choices with if Statements
Boolean Data Lesson #1 Outline
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Factoring if/else code
Flow of Control.
Control Structures – Selection
Boolean Data Lesson #1 Outline
Flow of Control.
And now for something completely different . . .
Computers & Programming Languages
Relational Operators Operator Meaning < Less than > Greater than
C Operators, Operands, Expressions & Statements
Flow of Control.
Introduction to Programming – 4 Operators
SE1H421 Procedural Programming LECTURE 4 Operators & Conditionals (1)
Introduction to Computing Lecture 04: Booleans & Selection
ENERGY 211 / CME 211 Lecture 5 October 1, 2008.
Controlling Program Flow
Presentation transcript:

1 CSE1301 Computer Programming Lecture 7 Booleans Linda M c Iver

2 Topics Boolean Type int as Boolean Boolean expressions Boolean Operators Precedence Common mistakes

3 Boolean A special “type” with only two values: –false and true. Used for conditions –for selection and looping

4 Type int as Boolean In C, integers are used as Booleans Integer value 0 is false. Any non-zero integer value is true.

5 #include /* Test some Booleans. */ int main() { int whatever = 0; if (whatever) { printf(“Is that true, Homer?\n”); } else { printf(“No, Marge.\n”); } return 0; } Example: What is the output?

6 #include /* Test some Booleans. */ int main() { int whatever = 1; if (whatever) { printf(“Is that true, Homer?\n”); } else { printf(“No, Marge.\n”); } return 0; } Example: What is the output?

7 #include /* Test some Booleans. */ int main() { int whatever = -100; if (whatever) { printf(“Is that true, Homer?\n”); } else { printf(“No, Marge.\n”); } return 0; } Example: What is the output?

8 #include /* Test some Booleans. */ int main() { int whatever = ’A’; if (whatever) { printf(“Is that true, Homer?\n”); } else { printf(“No, Marge.\n”); } return 0; } Example: What is the output?

9 #include /* Test some Booleans. */ int main() { int whatever = 0.003; if (whatever) { printf(“Is that true, Homer?\n”); } else { printf(“No, Marge.\n”); } return 0; } Example: What is the output?

10 Example: What is the output? #include /* Test some Booleans. */ int main() { float whatever = 0.003; if (whatever) { printf(“Is that true, Homer?\n”); } else { printf(“No, Marge.\n”); } return 0; } Behavior is “undefined.”

11 Boolean Expressions...are either strictly true or strictly false. A Boolean expression which evaluates to true has integer value 1; otherwise, 0.

12 Boolean Operators...are used in forming Boolean expressions....are also known as “logical” operators And ( && ) Or ( || ) Not ( ! ) Equality ( == ) Inequality ( != ) Comparison (, = )

13 And True only if both Boolean arguments are true. 1 && 2 1 && 0 && -1 tall && dark && handsome Examples:

14 And True only if both Boolean arguments are true. 1 && 2 1 && 0 && -1 tall && dark && handsome Examples: not to be confused with “bitwise AND” ( & )

15 Or True only if either Boolean argument is true (or both are true). 1 || 2 11 || 0 || 1 good || bad || ugly Examples:

16 Or True only if either Boolean argument is true (or both are true). 1 || 2 11 || 0 || 1 good || bad || ugly Examples: not to be confused with “bitwise OR” ( | )

17 Not True only if the single Boolean argument is false. ! 1 ! 0 ! happy Examples:

18 Equality True only if both arguments have identical values. 1 == 2 1 == 0 42 == 42 truth == beauty Examples:

19 Equality True only if both arguments have identical values. 1 == 2 1 == 0 42 == 42 truth == beauty Examples: not to be confused with assignment ( = )

20 Inequality True only if arguments have different values. 1 != 2 1 != 0 42 != 42 truth != beauty Examples:

21 Comparison True only if the specified relationship holds 1 < 2 0 > 1 42 <= 42 age >= 18 Examples:

22 Comparison True only if the specified relationship holds 1 < 2 0 > 1 42 <= 42 age >= 18 Careful! Examples:

23 Precedence Highest to lowest: –Brackets –Not ( ! ) –Comparison (, = ) –Equality ( == ) –Inequality ( != ) –And ( && ) –Or ( || ) Note: The assignment operator (=) is lower in order of precedence than Boolean / Logical operators.

24 Example: #include int main() { int age = 18; int haveMoney = 0; int haveCard = 1; float thirst = 0.31; int afterHours = 1; int result; result = age >= 18 && (haveMoney || haveCard) && thirst > 0.3 && ! afterHours; printf("%d\n", result); return 0; } bool.c

25 Common Mistakes Using = instead of ==

26 Example: #include /* Probably the most common C error. */ int main() { int score; scanf("%d", &score); if (score = 48 || score = 49) { printf("Almost!\n"); } return 0; } boolerr1.c

27 Example: #include /* Probably the most common C error. */ int main() { int score; scanf("%d", &score); if (score = 48 || score = 49) { printf("Almost!\n"); } return 0; } boolerr1.c Usual error message (if any): “LValue required...”

28 Example: #include /* Probably the most common C error. */ int main() { int score; scanf("%d", &score); if (score == 48 || score == 49) { printf("Almost!\n"); } return 0; } boolerr1.c

29 Common Mistakes Using = instead of == Multiple comparisons

30 Example: #include /* Another common C error. */ int main() { int score; scanf("%d", &score); if ( 0 < score < 48 ) { printf("Fail\n"); } return 0; } boolerr2.c

31 #include /* Another common C error. */ int main() { int score; scanf("%d", &score); if ( 0 < score < 48 ) { printf("Fail\n"); } return 0; } Example: boolerr2.c 0 or 1

32 #include /* Another common C error. */ int main() { int score; scanf("%d", &score); if ( 0 < score < 48 ) { printf("Fail\n"); } return 0; } Example: boolerr2.c 0 or 1 always 1

33 Example: #include /* Another common C error. */ int main() { int score; scanf("%d", &score); if ( 0 < score && score < 48 ) { printf("Fail\n"); } return 0; } boolerr2.c

34 Summary Boolean Type int as Boolean Boolean expressions Boolean Operators Precedence Common mistakes

35 Readings This Lecture: D&D 2/e: Sections 2.12 to 2.13, 3.2 and 4.13 to 4.15 D&D 3/e: Sections 2.6, 3.5, 4.10 and 4.11 Next Lecture: Selection D&D 2/e: Sections 3.2 to 3.3 D&D 3/e: Sections 3.4 to 3.6