Boolean Data Lesson #1 Outline

Slides:



Advertisements
Similar presentations
Arithmetic Expressions Lesson #1 CS1313 Spring Arithmetic Expressions Lesson #1 Outline 1.Arithmetic Expressions Lesson #1 Outline 2.A Less Simple.
Advertisements

Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
Introduction to Programming with Java, for Beginners Primitive Types Expressions Statements Variables Strings.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
CIS 234: Using Data in Java Thanks to Dr. Ralph D. Westfall.
CS180 Recitation 3. Lecture: Overflow byte b; b = 127; b += 1; System.out.println("b is" + b); b is -128 byte b; b = 128; //will not compile! b went out.
CS1061 C Programming Lecture 4: Indentifiers and Integers A.O’Riordan, 2004.
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.
Primitive Types CSE 115 Spring 2006 April 3 &
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
Admin Office hours 2:45-3:15 today due to department meeting if you change addresses during the semester, please unsubscribe the old one from the.
1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral.
Array Lesson 1 CS1313 Spring Array Lesson 1 Outline 1.Array Lesson 1 Outline 2.Mean of a List of Numbers 3.mean: Declarations 4.mean: Greeting,
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Conditional Statement
Numeric Data Types Lesson CS1313 Spring Numeric Data Types Outline 1.Numeric Data Types Outline 2.Data Types 3.Integers in Mathematics 4.Integers.
EXPRESSIONS AND ASSIGNMENT CITS1001. Scope of this lecture Assignment statements Expressions 2.
CPS120: Introduction to Computer Science
Selection Boolean What is Boolean ? Boolean is a set with only two values : –true –false true and false are standard identifiers in Pascal, called Boolean.
C Operators. CONTENTS C OPERATORS TYPES OF OPERATOR UNARY BINARY TERNARY ARITHMATIC RELATIONAL LOGICAL.
Computer Science 1620 boolean. Types so far: Integer char, short, int, long Floating Point float, double, long double String sequence of chars.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
Primitive Variables.
Java Language Basics By Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.
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.
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
Chapter 7 C supports two fundamentally different kinds of numeric types: (a) integer types - whole numbers (1) signed (2) unsigned (b) floating types –
CS1313: Arithmetic Expressions Lesson #2 CS1313 Spring Arithmetic Expressions Lesson #2 Outline 1.Arithmetic Expressions Lesson #2 Outline 2.Named.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
Windows Programming Lecture 06. Data Types Classification Data types are classified in two categories that is, – those data types which stores decimal.
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.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Rational Expressions relational operators logical operators order of precedence.
Functional Programming Lecture 1 - Introduction Professor Muffy Calder.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Based on slides by Shawn.
Week 3 - Friday CS222.
Rational Expressions. relational operators. logical operators
Boolean Data Lesson #1 Outline
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
EGR 2261 Unit 4 Control Structures I: Selection
Variables and Primative Types
Type Conversion, Constants, and the String Object
Numeric Data Types Outline
Basic Notions Review what is a variable? value? address? memory location? what is an identifier? variable name? keyword? what is legal identifier? what.
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,
Boolean Data Lesson #2 Outline
Winter 2018 CISC101 11/22/2018 CISC101 Reminders
Relational Operators Operator Meaning < Less than > Greater than
Introduction to Java, and DrJava part 1
C Operators, Operands, Expressions & Statements
Introduction to Primitive Data types
C++ Data Types Data Type
Program Breakdown, Variables, Types, Control Flow, and Input/Output
Introduction to Java, and DrJava
SE1H421 Procedural Programming LECTURE 4 Operators & Conditionals (1)
CS 240 – Lecture 7 Boolean Operations, Increment and Decrement Operators, Constant Types, enum Types, Precedence.
Boolean Data Outline Boolean Data Lesson Boolean Data Outline
Variables Lesson Outline
Primitive Types and Expressions
Introduction to Java, and DrJava part 1
DATA TYPES There are four basic data types associated with variables:
Arithmetic Expressions Lesson #1 Outline
Variables and Constants
Introduction to Primitive Data types
Presentation transcript:

Boolean Data Lesson #1 Outline Boolean Data Outline Data Types C Boolean Data Type: char or int C Built-In Boolean Data Type: bool bool Data Type: Not Used in CS1313 Boolean Declaration Boolean or Character? Boolean or Character Example #1 Boolean or Character Example #2 Boolean, Character or Integer? #1 Boolean Literal Constants Using Boolean Literal Constants #1 Using Boolean Literal Constants #2 What is a Boolean Expression? #1 What is a Boolean Expression? #2 What is a Boolean Expression? #3 What is a Boolean Expression? #4 What is a Boolean Expression? #5 Boolean Expressions Boolean Operations C Boolean Expression Evaluation Values Boolean Expression Example #1 Boolean Expression Example #2 Boolean Variables Example #1 Boolean Variables Example #2 Boolean Data Lesson #1 CS1313 Fall 2018

Data Types A data type is (surprise) a type of data: Numeric int: integer float: floating point (also known as real) Non-numeric char: character #include <stdio.h> int main () { /* main */ float standard_deviation, relative_humidity; int count, number_of_silly_people; char middle_initial, hometown[30]; } /* main */ Boolean Data Lesson #1 CS1313 Fall 2018

C Boolean Data Type: char or int The C data type typically used for storing Boolean values is char, although int will also work. Like numeric data types, Booleans have particular ways of being stored in memory and of being operated on. Conceptually, a Boolean value represents a single bit in memory. But, the char and int data types aren’t implemented this way – if for no other reason than that computers can’t address a single bit, because the smallest collection of bits that they can address is a byte (or, in a few cases, a word). Boolean Data Lesson #1 CS1313 Fall 2018

C Built-In Boolean Data Type: bool C also has a built-in data type for Booleans: bool The bool data type has possible values false and true However, some C compilers don’t have the bool data type and the Boolean values true and false available by default; you have to make them available using this directive: #include <stdbool.h> (after #include <stdio.h>). Boolean Data Lesson #1 CS1313 Fall 2018

bool Data Type: Not Used in CS1313 In CS1313, we WON’T use the bool data type, nor its values true and false. Instead, we’ll use char or int. Similarly, we’ll use 0 for falsity and 1 (or any nonzero integer value) for truth. Boolean Data Lesson #1 CS1313 Fall 2018

Boolean Declaration char CS1313_lectures_are_fascinating; This declaration tells the compiler to grab a group of bytes, name them CS1313_lectures_are_fascinating, and think of them as storing a Boolean value (either true or false). How many bytes? Even though conceptually a Boolean represents a single bit, in practice char variables are usually implemented using 8 bits (1 byte): CS1313_lectures_are_fascinating : Boolean Data Lesson #1 CS1313 Fall 2018

Boolean or Character? Question: How does the C compiler know that a particular char declaration is a Boolean rather than a character? Answer: It doesn’t. Whether a char (or an int) is treated by a program as a Boolean or as a character (respectively, an integer) depends entirely on how you use it in the program. Boolean Data Lesson #1 CS1313 Fall 2018

Boolean or Character Example #1 #include <stdio.h> int main () { /* main */ const int maximum_short_height_in_cm = 170; const int program_success_code = 0; int my_height_in_cm = 160; char I_am_Henry = 1; char I_am_tall; char my_middle_initial = 'J'; I_am_tall = (!I_am_Henry) && (my_height_in_cm > maximum_short_height_in_cm); printf("I_am_Henry = %d\n", I_am_Henry); printf("my_height_in_cm = %d\n", my_height_in_cm); printf("I_am_tall = %d\n", I_am_tall); printf("my_middle_initial = %c\n", my_middle_initial); return program_success_code; } /* main */ Boolean Data Lesson #1 CS1313 Fall 2018

Boolean or Character Example #2 % gcc -o short short.c % short I_am_Henry = 1 my_height_in_cm = 160 I_am_tall = 0 my_middle_initial = J Whether a char (or an int) is treated by a program as a Boolean or a character (respectively, an integer) depends entirely on how you use it in the program. Boolean Data Lesson #1 CS1313 Fall 2018

Boolean, Character or Integer? #1 In the previous example program, we had char variables named I_am_Henry and I_am_tall. We treated them as Boolean variables in the calculation subsection, but in the output subsection we had: printf("I_am_Henry = %d\n", I_am_Henry); printf("I_am_tall = %d\n", I_am_tall); How can this be? Boolean Data Lesson #1 CS1313 Fall 2018

Boolean, Character or Integer? #1 char I_am_Henry = 1; char I_am_tall; … I_am_tall = (!I_am_Henry) && … ; printf("I_am_Henry = %d\n", I_am_Henry); printf("I_am_tall = %d\n", I_am_tall); How can it be that the same variable is simultaneously a Boolean, a character and an integer? It turns out that char not only means character, it also means an integer of 1 byte (8 bits). This is confusing, but you’ll get used to it. Boolean Data Lesson #1 CS1313 Fall 2018

Boolean Literal Constants In C, a Boolean literal constant can have either of two possible values (but not both at the same time, of course): to represent false: 0 to represent true: anything other than 0 (usually 1) Boolean Data Lesson #1 CS1313 Fall 2018

Using Boolean Literal Constants #1 We can use Boolean literal constants in several ways: In declaring and initializing a named constant: const char true = 1; In declaring and initializing a variable: char I_am_getting_a_bad_grade = 0; In an assignment: this_is_my_first_guess = 1; In an expression: Henry_is_tall && 1; Boolean Data Lesson #1 CS1313 Fall 2018

Using Boolean Literal Constants #2 The first two of these uses – in a named constant declaration and in a variable declaration – are considered good programming practice, AND SO IS THE THIRD (in an assignment), which is a way that Booleans are different from numeric data. As for using Boolean literal constants in expressions, it’s not so much that it’s considered bad programming practice, it’s just that it’s kind of pointless. Boolean Data Lesson #1 CS1313 Fall 2018

What is a Boolean Expression? #1 a || (b || c && !d) && e && (f || g) && h In programming, a Boolean expression is a combination of: Boolean Operands Boolean Operators Parentheses: ( ) Boolean Data Lesson #1 CS1313 Fall 2018

What is a Boolean Expression? #2 a || (b || c && !d) && e && (f || g) && h In programming, a Boolean expression is a combination of: Boolean Operands, such as: Boolean literal constants (0 for false, nonzero for true) Boolean named constants Boolean variables Boolean-valued function invocations Boolean Operators Parentheses: ( ) Boolean Data Lesson #1 CS1313 Fall 2018

What is a Boolean Expression? #3 a || (b || c && !d) && e && (f || g) && h In programming, a Boolean expression is a combination of: Boolean Operands Boolean Operators, such as: Relational Operators (which have numeric operands) Logical Operators Parentheses: ( ) Boolean Data Lesson #1 CS1313 Fall 2018

What is a Boolean Expression? #4 a || (b || c && !d) && e && (f || g) && h In programming, a Boolean expression is a combination of: Boolean Operands Boolean Operators, such as: Relational Operators (which have numeric operands) Equal: == Not Equal: != Less Than: < Less Than or Equal To: <= Greater Than: > Greater Than or Equal To: >= Logical Operators Parentheses: ( ) Boolean Data Lesson #1 CS1313 Fall 2018

What is a Boolean Expression? #5 a || (b || c && !d) && e && (f || g) && h In programming, a Boolean expression is a combination of: Boolean Operands Boolean Operators, such as: Relational Operators (which have numeric operands) Logical Operators Negation (NOT): ! Conjunction (AND): && Disjunction (OR): || Parentheses: ( ) Boolean Data Lesson #1 CS1313 Fall 2018

Boolean Expressions Just like a numeric (arithmetic) expression, a Boolean expression is a combination of Boolean terms (such as variables, named constants, literal constants and Boolean-valued function calls), Boolean operators (for example, !, &&, ||, relational comparisons) and parentheses. I_am_happy !I_am_happy it_is_raining && it_is_cold it_is_raining || it_is_cold (!it_is_raining) || (it_is_cold && I_am_happy) Boolean Data Lesson #1 CS1313 Fall 2018

Boolean Operations Like arithmetic operations, Boolean operations come in two varieties: unary and binary. A unary operation is an operation that uses only one term; a binary operation uses two terms. Boolean operations include: Operation Kind Operator Usage Effect Identity Unary None x No change to value of x Negation ! !x Inverts value of x Conjunction (AND) Binary && x && y 1 if both x is nonzero AND y is nonzero; otherwise 0 Disjunction (Inclusive OR) || x || y 1 if either x is nonzero OR y is nonzero, or both; otherwise 0 Boolean Data Lesson #1 CS1313 Fall 2018

C Boolean Expression Evaluation Values C Boolean expressions evaluate to either: 0 (representing false) 1 (representing true) Note that any nonzero value represents true, but, when C evaluates a Boolean expression, then if that expression evaluates to true, then specifically its value is 1. Note that only 0 represents false, ever. Boolean Data Lesson #1 CS1313 Fall 2018

Boolean Expression Example #1 #include <stdio.h> int main () { /* main */ const char true = 1, false = 0; printf(" true = %d, false = %d\n", true, false); printf("!true = %d, !false = %d\n", !true, !false); printf("\n"); printf("true || true = %d\n", true || true); printf("true || false = %d\n", true || false); printf("false || true = %d\n", false || true); printf("false || false = %d\n", false || false); printf("true && true = %d\n", true && true); printf("true && false = %d\n", true && false); printf("false && true = %d\n", false && true); printf("false && false = %d\n", false && false); } /* main */ Boolean Data Lesson #1 CS1313 Fall 2018

Boolean Expression Example #2 % gcc -o logic_expression_simple logic_expression_simple.c % logic_expression_simple true = 1, false = 0 !true = 0, !false = 1 true || true = 1 true || false = 1 false || true = 1 false || false = 0 true && true = 1 true && false = 0 false && true = 0 false && false = 0 Boolean Data Lesson #1 CS1313 Fall 2018

Boolean Variables Example #1 #include <stdio.h> int main () { /* main */ const int true = 1; const int false = 0; int project_due_soon; int been_putting_project_off; int start_working_on_project_today; printf("Is it true that you have a programming project due soon?\n"); printf(" (Answer %d for true, %d for false.)\n", true, false); scanf("%d", &project_due_soon); printf("Is it true that you have been putting off working on it?\n"); scanf("%d", &been_putting_project_off); start_working_on_project_today = project_due_soon && been_putting_project_off; printf("Is it true that you should start "); printf("working on it today?\n"); printf("ANSWER: %d\n", start_working_on_project_today); } /* main */ Boolean Data Lesson #1 CS1313 Fall 2018

Boolean Variables Example #2 % gcc -o pp_logic pp_logic.c % pp_logic Is it true that you have a programming project due soon? (Answer 1 for true, 0 for false.) 1 Is it true that you have been putting off working on it? Is it true that you should start working on it today? ANSWER: 1 Boolean Data Lesson #1 CS1313 Fall 2018