Revision of IO Streams printf scanf.

Slides:



Advertisements
Similar presentations
1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
Advertisements

Lecture 2 Dr Richard Reilly Dept. of Electronic & Electrical Engineering Room 153, Engineering Building To insert your company logo on this slide From.
1 CSE1301 Computer Programming Lecture 7 Booleans Linda M c Iver.
FIT Objectives By the end of this lecture, students should: understand selection statements understand nested selection understand tradeoff.
BOOLEAN LOGIC CSC 171 FALL 2004 LECTURE 7. ASSIGNMENT Review Quiz # 2 Start reading Chapter 5.
More about Numerical Computation CS-2301, B-Term More about Numerical Computation CS-2301, System Programming for Non-Majors (Slides include materials.
Chapter 4 Making Decisions
1 Revision of IO Streams printf scanf. 2 CSE1301 Computer Programming Lecture 8 Booleans.
Digital Logic Circuits – Chapter 1 Section 1-3, 1-2.
Boolean Algebra – I. Outline  Introduction  Digital circuits  Boolean Algebra  Two-Valued Boolean Algebra  Boolean Algebra Postulates  Precedence.
1 Fundamentals of Computer Science Propositional Logic (Boolean Algebra)
Lecture 7 Topics –Boolean Algebra 1. Logic and Bits Operation Computers represent information by bit A bit has two possible values, namely zero and one.
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /05/2013 Lecture 4: Basics of Logic Design Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER.
Introduction to Computing Lecture 05: Selection (continued) Introduction to Computing Lecture 05: Selection (continued) Assist.Prof.Dr. Nükhet ÖZBEK Ege.
15-Nov-15 All the Operators. operators.ppt 2 Precedence An operator with higher precedence is done earlier (precedes) one with lower precedence A higher.
Advanced Logic Copyright © 1999 Patrick McDermott College of Alameda
Propositional Calculus CS 270: Mathematical Foundations of Computer Science Jeremy Johnson.
Conditional Expressions
Laws of Boolean Algebra Commutative Law Associative Law Distributive Law Identity Law De Morgan's Theorem.
Booleans Lecture 26. Summary of previous lecture In the previous lecture, we have been learnt,  The if statement  The else statement  Cascaded if 
Department of Electronic & Electrical Engineering Expressions operators operands precedence associativity types.
ECE DIGITAL LOGIC LECTURE 6: BOOLEAN ALGEBRA Assistant Prof. Fareena Saqib Florida Institute of Technology Fall 2016, 02/01/2016.
COMPUTER ARCHITECTURE & OPERATIONS I Instructor: Yaohang Li.
Department of Electronic & Electrical Engineering Lecture 3 IO reading and writing variables scanf printf format strings "%d %c %f" Expressions operators.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
Copyright © Curt Hill The C++ IF Statement More important details More fun Part 3.
Dr. ClincyLecture Slide 1 CS6020- Chapter 3 (3A and ) Dr. Clincy Professor of CS First Exam - Tuesday, September 6th Coverage: All subjects up to.
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.
The Ohio State University
Computer Architecture & Operations I
Computer Architecture & Operations I
Boolean Algebra & Logic Gates
CPS120 Introduction to Computer Science
Morgan Kaufmann Publishers
CSE 220 – C Programming Expressions.
CSE1301 Sem Selection Lecture 25 Lecture 9: Selection.
CS Chapter 3 (3A and ) Part 1 of 8
Lecture 3 Java Operators.
C# Operator Overloading and Type Conversions
Boolean Algebra & De Morgan's Theorems
2.0 FUNDAMENTALS OF JAVA PROGRAMMING LANGUAGE
More important details More fun Part 3
Overview Part 1 – Gate Circuits and Boolean Equations
Boolean Algebra.
CS Chapter 3 (3A and ) Part 1 of 8
Boolean Algebra.
CSE 311 Foundations of Computing I
Propositional Calculus: Boolean Algebra and Simplification
Winter 2018 CISC101 11/22/2018 CISC101 Reminders
All the Operators 22-Nov-18.
Relational Operators Operator Meaning < Less than > Greater than
Bits and Bytes Boolean algebra Expressing in C
All the Operators 4-Dec-18.
C Operators, Operands, Expressions & Statements
Chapter-3 Operators.
January 19 W’05 Yutao He 4532B Boelter Hall CSM51A/EEM16-Sec.1 W’05
Boolean Algebra.
Introduction to Programming – 4 Operators
SE1H421 Procedural Programming LECTURE 4 Operators & Conditionals (1)
All the Operators 6-Apr-19.
All the Operators 13-Apr-19.
Herbert G. Mayer, PSU CS Status 7/19/2015
Introduction to Computing Lecture 04: Booleans & Selection
C++ Programming Language Lecture 4 C++ Basics – Part II
ENERGY 211 / CME 211 Lecture 5 October 1, 2008.
Lecture 2: Bits, Bytes, Ints
Controlling Program Flow
Presentation transcript:

Revision of IO Streams printf scanf

CSI 121 Structure Programming Language Lecture 8 Booleans CSE1301 Sem 2, 2003 CSI 121 Structure Programming Language Lecture 8 Booleans Lecture 8: Booleans

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

Boolean A special “type” with only two values: false and true. Used to implement conditions for selection and looping in an algorithm Boolean expressions represent statements which are either strictly true or strictly false

Boolean Algebra --- History George Boole, 1815-1864 Initially self-guided studies of languages and philosphy With 16 assistant teacher at private school With 20 opened a school and taught himself mathematics Published in “Cambridge Mathematical Journal” with 24 The Mathematical Analysis of Logic was published in 1847 Casts logical reasoning in the form of algebra + is OR, * is AND 0 is FALSE, 1 is TRUE An Investigation of the Laws of Thought (1854) extends the algebra

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

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

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

Boolean Expressions ...are either strictly true or strictly false. ... can be evaluated to determine their true or falsehood. A Boolean expression which evaluates to true has integer value 1; otherwise, 0. Remember: any non-zero value is taken interpreted as true

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

healthy && wealthy && wise And True only if both Boolean arguments are true. Examples: 1 && 2 1 && 0 && -1 healthy && wealthy && wise

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

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

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

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

Reminder: Gates A OR B OR Gate AND Gate A B A AND B add mult 1 1 1 1

Reminder: Gates NOT Gate A NOT A 1

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

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

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

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

Short-circuiting A complex Boolean expression is only evaluated as far as necessary Examples: 1 || 2 0 || 1 || 2 1 && 0 && -1

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.

Example: bool.c #include <stdio.h> 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

Common Mistakes Using = instead of ==

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

Example: boolerr1.c #include <stdio.h> /* 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

Common Mistakes Using = instead of == Multiple comparisons

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

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

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

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

True and False as Constants ‘C’ does not provide constants for TRUE/FALSE (other than 0 and 1) You can make use of the pre-processor Use #define TRUE 1 #define FALSE 0 whenever you need such constants

Exclusive OR True if “at most one” of two alternatives is true False if neither is true False if both are true! Define! A xor B : Do you need brackets?

Two out of three… True if and only if exactly two of A,B,C are true CSE1301 Sem 2, 2003 Two out of three… True if and only if exactly two of A,B,C are true Define! Is there a better way? A && B && !C || A && !B && C || !A && B && C Vs A+B+C==2 Vs !A+!B+!C==1 Lecture 8: Booleans

Simplifying & Checking Boolean Expressions A && !B || B && !A Use Truth Tables Transform Expressions using Boolean Algebra A B A xor B A B 1 1

Axioms of Boolean Algebra Commutative (A && B)  (B && A) (A || B)  (B || A) Associative A && (B && C)  (A && B) && C A || (B || C)  (A || B) || C

Axioms of Boolean Algebra (cont) CSE1301 Sem 2, 2003 Axioms of Boolean Algebra (cont) Distributive A && (B || C)  (A && B) || (A && C) A || (B && C)  (A || B) && (A || C) … plus some “technicalities” Closure, Complement Element (a && !a == 0, a || !a ==1) exists, and identity element exists (a || 0 == a, a && 1 == a) Lecture 8: Booleans

De Morgan’s Law From the axioms of Boolean algebra it follows that: CSE1301 Sem 2, 2003 De Morgan’s Law From the axioms of Boolean algebra it follows that: ! ( A || B )  !A && !B ! (A && B)  !A || !B You can use all rules above to simplify / verify expressions. Exercise: simplify ! ( A || !B ) && A De Morgan, double negation B, commutative, associative, complement … B && False ==> False Lecture 8: Booleans

Conditional Expressions CSE1301 Sem 2, 2003 Conditional Expressions We can write an expression such that its value depends on a TRUTH value Condition ? Expr2 : Expr3 A ternary operator For example: int z,a,b; … z = (a>b) ? a : b ; Note that this is just an expression and is different from the conditional statement If (a>b) then z=a else z=b; Lecture 8: Booleans

Summary Boolean Type int as Boolean Boolean expressions Boolean Operators Precedence Common mistakes Some Boolean algebra

Readings This Lecture: King: Section 5.1 D&D: Sections 2.6, 3.4 – 3.6, 4.10 Kernighan & Ritchie: 2.6, 2.12