CS 240 – Lecture 7 Boolean Operations, Increment and Decrement Operators, Constant Types, enum Types, Precedence.

Slides:



Advertisements
Similar presentations
Copyright © 2003 Pearson Education, Inc. Slide 1.
Advertisements

Control Structures Any mechanism that departs from straight-line execution: –Selection: if-statements –Multiway-selection: case statements –Unbounded iteration:
CS 117 Spring 2002 Review for Exam 2 March 6, 2002 open book, 1 page of notes.
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations Imperative Programming, B. Hirsbrunner,
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
JavaScript, Third Edition
1 Chapter 10 Various Topics User defined Types Enumerated Types Type Casting Syntactic Sugar Type Coercion.
Homework –Continue Reading K&R Chapter 2 –We’ll go over HW2 –HW3 is posted Questions?
2440: 211 Interactive Web Programming Expressions & Operators.
Chapter 3: Data Types and Operators JavaScript - Introductory.
COMPUTER PROGRAMMING. variable What is variable? a portion of memory to store a determined value. Each variable needs an identifier that distinguishes.
Operators Precedence - Operators with the highest precedence will be executed first. Page 54 of the book and Appendix B list C's operator precedence. Parenthesis.
Boolean Data Lesson CS1313 Fall Boolean Data Outline 1.Boolean Data Outline 2.Data Types 3.C Boolean Data Type: char or int 4.C Built-In Boolean.
1 Homework –Continue Reading K&R Chapter 2 –We’ll go over HW2 at end of class today –Continue working on HW3 Questions?
Controlling Program Flow with Decision Structures.
0 Chap.2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations 2.5Arithmetic Operators 2.6Relational.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103 material developed.
What do I need to Know For My Assignment?. C Pointer Review To declare a pointer, we use the * operator. This is similar to but different from using *
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.
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Prof. Jeremy.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. 4 Simple Flow of Control.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU Status 6/10/2016 Initial content copied verbatim from ECE 103 material developed.
CompSci 230 S Programming Techniques
Control Structures I Chapter 3
LESSON 06.
Java Language Basics.
UMBC CMSC 104 – Section 01, Fall 2016
Chapter 3 Control Statements
Lecture 3 Java Operators.
Chap. 2. Types, Operators, and Expressions
Boolean Data Lesson #1 Outline
More important details More fun Part 3
JavaScript Syntax and Semantics
Expressions and Control Flow in JavaScript
Instructor: Ioannis A. Vetsikas
Boolean Data Lesson #1 Outline


C Structures, Unions, Bit Manipulations and Enumerations
Data Types, Identifiers, and Expressions
Expressions Chapter 4 Copyright © 2008 W. W. Norton & Company.
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
Enumerations CS Fall 1999 Enumerations.
Module 2: Understanding C# Language Fundamentals
2017 Jan Sun Mon Tue Wed Thu Fri Sat

Boolean Logic Boolean Logic is considered to be the basic of digital electronics. We know that a computer’s most basic operation is based on digital electronics.
FY 2019 Close Schedule Bi-Weekly Payroll governs close schedule
Associativity and Prescedence
Jan Sun Mon Tue Wed Thu Fri Sat
Relational & Logical Operators, if and switch Statements
CISC/CMPE320 - Prof. McLeod
SE1H421 Procedural Programming LECTURE 4 Operators & Conditionals (1)
QUIZ.
SSEA Computer Science: Track A
Herbert G. Mayer, PSU CS Status 7/19/2015
CISC/CMPE320 - Prof. McLeod
OPERATORS AND EXPRESSIONS IN C++

Chap 7. Advanced Control Statements in Java
Review of Previous Lesson
OPERATORS in C Programming
Operator King Saud University
Homework Homework Questions? Continue Reading K&R Chapter 2
Controlling Program Flow
TIMELINE NAME OF PROJECT Today 2016 Jan Feb Mar Apr May Jun
Reviewing Abbreviations
OPERATORS in C Programming
Introduction to Pointers
Presentation transcript:

CS 240 – Lecture 7 Boolean Operations, Increment and Decrement Operators, Constant Types, enum Types, Precedence

Booleans – Review Booleans are values corresponding to the truth of a logical statement. True and False In many programming languages, they're treated explicitly as unique values TRUE and FALSE or similarly but written differently. However, in C, Booleans are treated numerically. 0 is false Everything else is true Any numeric-typed value can be treated as a Boolean value.

Booleans – Review Comparison Operators These should be covered from previous classes in other languages. The only thing that is different should be the way they're written. The comparison operators are as follows: x > y x greater than y x >= y x greater than or equal to y x == y x equal to y x <= y x less than or equal y x < y x less than y x != y x not equal to y

Boolean – Disjunction and Conjunction Up to this point in the course, no assignment you've needed to do required more than one Boolean statement. You could always nest if-statements if needed. However, it's often the case that you can not fully determine a condition with just one Boolean statement. What if a condition involves more than two values that need to be compared? A disjunction is a combination of two or more logical statements which is true only if at least one of it's logical statements are true. A conjunction is a combination of two or more logical statements which is true only if all of it's logical statement are true.

Booleans – && and || operators In C, there are Boolean operators for combining other Boolean statements together. The "logical and" (&&) operator indicates a conjunction. The "logical or" (||) operator indicates a disjunction. x && y x || y x && y > z Again, in C, Booleans are numeric in their value, so these operations return 1 and 0 for TRUE and FALSE respectively. You can make longer conjunctions or disjunctions with these operators, as well. w && x && y && z w || x || y || z

Booleans – !, the "not" operator Sometimes, an existing logical statement is the exact opposite of what we want for a Boolean condition. Instead of rewriting it, we can invert it's value to the opposite truth value. This is called the "negation" of the statement. The unary not (!) operator, inverts the Booleans value of its operand. !1 evaluates to 0 !0 evaluates to 1 However, since non-zero numbers are considered true, !x evaluates to 0, for non-zero x

Booleans – De Morgan's Laws De Morgan's Laws are a relationship between conjunctions and disjunctions which allows you to switch from one to the other. !(x || y) is equal to (!x && !y) !(x && y) is equal to (!x || !y) The laws in English: the negation of a disjunction is the conjunction of the negations; the negation of a conjunction is the disjunction of the negations; Oftentimes, this produces better-readable code. This also works for longer conjunctions.

Booleans – Short-circuiting Property The "logical and" and "logical or" operators have an interesting property call short-circuiting. When the final result of the computation is obvious from the first operand, the second operand is not evaluated. x && y x || y For the "logical and", if x is false, we don't need to evaluate y. For the "logical or", if x is true, we don't need to evaluate y. For example, the calls below to doSomething() never happen. 0 && doSomething() 1 || doSomething()

Operators – ++ and -- We very briefly discussed the ++ operator when we introduced for- loops. These operators are designed to make changes to memory before or after the evaluation of an expression. ++x pre-increment operator; adds 1 to x before expression is eval x++ post-increment operator; adds 1 to x after expression is eval --x pre-decrement operator; minus 1 from x before expression is eval x-- post-decrement operator; minus 1 from x after expression is eval These only work on expressions which describe a location in memory! Variable names (x), array elements (a[i]), value-at addresses (*ptr)

Variables – const type Often times, you want to allocate memory for a value that will not change once it's calculated the first time. Symbolic constants are not ideal because they are simply in-text replacements. const int var = 7; By using the keyword const, we can indicate that the variable should be stored in read-only memory by the compiler. Where and how exactly is implementation specific on a per-compiler basis. Constant variables can only be assigned a value once and it must be during declaration. Otherwise, it will only ever contain junk.

Types – enum types Enumerations are ordered listings of things of the same kind. Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday January, February, March, April, May, … You get the idea. Since the elements of those lists are ordered, we can treat them as numeric values instead. enum name {value1, value2, …}; Declaring an enum type does not take up any memory. Each value in the enum declaration corresponds to its position as an integer. The value names can be used elsewhere in the code as if they were proper values.

Types – enum examples Months enum month {JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC}; Days of the Week enum dow {SUN, MON, TUE, WED, THU, FRI, SAT}; Boolean enum boolean {FALSE, TRUE};

Variables – enum variables When using enum types, we create multiple integer constants which can be used as values. We can also store those values in variables of the specific enum type. For example, with days of the week: enum dow {SUN,MON,TUE,WED,THU,FRI,SAT}; To declare a variable of type enum dow, enum dow name; enum dow today = THU; This takes up the same space in memory as an integer. enum variables always default to the first value when declared.

Precedence and Associativity of Operators sign dereference address casting Associativity ( ) [ ] -> . Left to right ! ~ ++ - - + - * & (type) sizeof right to left * / % left to right + - left to right << >> left to right < <= > >= left to right = = != left to right & left to right ^ left to right | left to right && left to right || left to right ?: right to left = += -= *= /= %= &= ^= |= <<= >>= right to left , left to right Precedence

Precedence and Associativity Operators in the same row have the same precedence Operators are in order of decreasing precedence Associativity Determine the order if the operators have the same precedence