1. Boolean Data Type & Expressions Outline. Basic Data Types. Numeric int float Non-numeric char



Similar documents
Boolean Data Outline

Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void

Selection Statements

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

C / C++ and Unix Programming. Materials adapted from Dan Hood and Dianna Xu

Fundamentals of Programming

Informatica e Sistemi in Tempo Reale

Outline. Conditional Statements. Logical Data in C. Logical Expressions. Relational Examples. Relational Operators

As previously noted, a byte can contain a numeric value in the range Computers don't understand Latin, Cyrillic, Hindi, Arabic character sets!

VB.NET Programming Fundamentals

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

Chapter 5. Selection 5-1

Sources: On the Web: Slides will be available on:

Chapter 14: Boolean Expressions Bradley Kjell (Revised 10/08/08)

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

Lecture 2 Notes: Flow of Control

Comp151. Definitions & Declarations

Statements and Control Flow

UIL Computer Science for Dummies by Jake Warren and works from Mr. Fleming

Conditions & Boolean Expressions

6.S096 Lecture 1 Introduction to C

Example of a Java program

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

Variables, Constants, and Data Types

1.00/ Session 2 Fall Basic Java Data Types, Control Structures. Java Data Types. 8 primitive or built-in data types

Sample CSE8A midterm Multiple Choice (circle one)

Conditional Statements Summer 2010 Margaret Reid-Miller

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

C++FA 5.1 PRACTICE MID-TERM EXAM

Chapter 3 Operators and Control Flow

Lecture 9 verifying temporal logic

J a v a Quiz (Unit 3, Test 0 Practice)

5 Arrays and Pointers

C Programming Tutorial

Java Basics: Data Types, Variables, and Loops

Introduction to Data Structures

CS1020 Data Structures and Algorithms I Lecture Note #1. Introduction to Java

JAVA - QUICK GUIDE. Java SE is freely available from the link Download Java. So you download a version based on your operating system.

Introduction to Java

Object Oriented Software Design

Boolean Design of Patterns

Part I. Multiple Choice Questions (2 points each):

Introduction to SQL for Data Scientists

Memory management. Announcements. Safe user input. Function pointers. Uses of function pointers. Function pointer example

CS 111 Classes I 1. Software Organization View to this point:

Introduction to Java. CS 3: Computer Programming in Java

The programming language C. sws1 1

java.util.scanner Here are some of the many features of Scanner objects. Some Features of java.util.scanner

This Unit: Floating Point Arithmetic. CIS 371 Computer Organization and Design. Readings. Floating Point (FP) Numbers

1 Abstract Data Types Information Hiding

Lecture 22: C Programming 4 Embedded Systems

Number Representation

Keil C51 Cross Compiler

Introduction to Visual C++.NET Programming. Using.NET Environment

Boolean Expressions 1. In C++, the number 0 (zero) is considered to be false, all other numbers are true.

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

Operator Overloading. Lecture 8. Operator Overloading. Running Example: Complex Numbers. Syntax. What can be overloaded. Syntax -- First Example

Simple C++ Programs. Engineering Problem Solving with C++, Etter/Ingber. Dev-C++ Dev-C++ Windows Friendly Exit. The C++ Programming Language

arrays C Programming Language - Arrays

A Grammar for the C- Programming Language (Version S16) March 12, 2016

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

if and if-else: Part 1

Member Functions of the istream Class

Logic in Computer Science: Logic Gates

Moving from CS 61A Scheme to CS 61B Java

Pemrograman Dasar. Basic Elements Of Java

Phys4051: C Lecture 2 & 3. Comment Statements. C Data Types. Functions (Review) Comment Statements Variables & Operators Branching Instructions

An Introduction to Assembly Programming with the ARM 32-bit Processor Family

CHAPTER 2. Logic. 1. Logic Definitions. Notation: Variables are used to represent propositions. The most common variables used are p, q, and r.

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

Binary Division. Decimal Division. Hardware for Binary Division. Simple 16-bit Divider Circuit

Boolean Expressions & the if Statement

We will learn the Python programming language. Why? Because it is easy to learn and many people write programs in Python so we can share.

Lecture Notes on Linear Search

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

Lecture 12: More on Registers, Multiplexers, Decoders, Comparators and Wot- Nots

What is a Loop? Pretest Loops in C++ Types of Loop Testing. Count-controlled loops. Loops can be...

Characters & Strings Lesson 1 Outline

Chapter 2: Elements of Java

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

CISC 181 Project 3 Designing Classes for Bank Accounts

Java Interview Questions and Answers

Numeral Systems. The number twenty-five can be represented in many ways: Decimal system (base 10): 25 Roman numerals:

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

The if Statement and Practice Problems

Chapter One Introduction to Programming

Semantic Analysis: Types and Type Checking

Binary Adders: Half Adders and Full Adders

CS11 Advanced C++ Spring Lecture 9 (!!!)

C PROGRAMMING FOR MATHEMATICAL COMPUTING

Topics. Parts of a Java Program. Topics (2) CS 146. Introduction To Computers And Java Chapter Objectives To understand:

13 Classes & Objects with Constructors/Destructors

CS 241 Data Organization Coding Standards

C++ Language Tutorial

So far we have considered only numeric processing, i.e. processing of numeric data represented

Negative Integral Exponents. If x is nonzero, the reciprocal of x is written as 1 x. For example, the reciprocal of 23 is written as 2

Outline Basic concepts of Python language

Shell Scripts (1) For example: #!/bin/sh If they do not, the user's current shell will be used. Any Unix command can go in a shell script

Transcription:

Boolean Data Type & Expressions Outline. Boolean Data Type & Expressions Outline 2. Basic Data Types 3. C Boolean Data Types: char or int/ Boolean Declaration 4. Boolean or Character? 5. Boolean Literal Constants 6. Boolean Expressions/ Boolean Operations 7. Example: Boolean Literal Constants & Expressions 8. Example: Boolean Variables 9. Relational Operations 0. Example: Relational Expressions. Structure of Boolean Expressions 2. Precedence Order of Boolean Operations 3. Another Boolean Precedence Example 4. Example: Boolean Precedence Order 5. Example: More Relational Expressions 6. Example: More Relational Expressions (continued) 7. Short Circuiting 8. Exercise: Writing a Program Numeric int float Non-numeric char Basic Data Types float standard_deviation, relative_humidity; int count, number_of_silly_people; char middle_initial, hometown[30]; 2

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, although 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, since the smallest collection of bits that they can address is a byte (or, in a few cases, a word). Boolean Declaration char CS33 lectures are fascinating; This declaration tells the compiler to grab a group of bytes, name them CS33 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 ( byte): CS33 lectures are fascinating :???????? 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. % cat shortcircuit.c const int maximum_short_height_in_cm = 70; int my_height_in_cm = 60; char I_am_Henry = ; 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); % gcc -o shortcircuit shortcircuit.c % shortcircuit I_am_Henry = my_height_in_cm = 60 I_am_tall = 0 my_middle_initial = J Whether a char is a Boolean or a character depends entirely on how you use it in the program. 3 4

Boolean Literal Constants A Boolean literal constant can have either of two possible values (but not both): to represent false: 0 to represent true: anything other than 0 (usually ) We can use Boolean literal constants in several ways: In declaring and initializing a named constant: const char true = ; In declaring and initializing a variable: char getting a bad grade = 0; In an assignment: this is my first guess = ; In an expression: Henry is short && ; The first two of these uses are considered good programming practice, AND SO IS THE THIRD, which is a way that Booleans are different from numeric data; that is, it s acceptable to use a Boolean literal constant in an assignment statement. 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 Expressions Just like a numeric arithmetic expression, a Boolean expression is a combination of Boolean terms (such as variables, named constants and literal constants), Boolean operators (e.g.,!, &&,, 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 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 Name Kind Operator Usage Effect Identity Unary None x None Negation Unary!!x Inverts value of x Conjunction Binary && x && y if both x is nonzero (AND) and y is nonzero; Disjunction Binary x y if either x is nonzero (Inclusive OR) or y is nonzero, or both; Note: C Boolean expressions evaluate either to 0 (representing false) or to (representing true). 5 6

Example: Boolean Literal Constants & Expressions % cat lgcexprsimple.c const char true =, 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(" 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); % gcc -o lgcexprsimple lgcexprsimple.c % lgcexprsimple true =, false = 0!true = 0,!false = true true = true false = false true = false false = 0 % cat prog_logic.c Example: Boolean Variables int project_due_soon; int been_putting_project_off; int start_working_on_project_today; printf("is it true that you have a "); printf("programming project due soon? n"); scanf("%d", &project_due_soon); printf("is it true that you have "); printf("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); % gcc -o prog_logic prog_logic.c % prog_logic Is it true that you have a programming project due soon? Is it true that you have been putting off working on it? Is it true that you should start working on it today? ANSWER: true && true = true && false = 0 false && true = 0 false && false = 0 Notice that a Boolean expression always evaluates to either or 0. 7 8

Relational Operations A relational operation is a binary operation that compares two numeric operands and produces a Boolean result. For example: CS33 lab section == 4 cm per km!= 00 age < 2 number of students <= number of chairs credit hours > 30 electoral votes >= 270 The relational operations are: Operation Name Operand Usage Result Equal == x == y if the value of x is exactly the same as the value of y; Not Equal!= x!= y if the value of x is different from the value of y; Less Than < x < y if the value of x is less than the value of y; Less Than Or <= x <= y if the value of x is less Equal To than or equal to the value of y; Greater Than > x > y if the value of x is greater than the value of y; Greater Than Or >= x >= y if the value of x is greater Equal To than or equal to the value of y; Example: Relational Expressions % cat relational.c int CS33_size, METR243_size; printf("how many students are in CS33? n"); scanf("%d", &CS33_size); printf("how many students are in METR243? n"); scanf("%d", &METR243_size); printf("%d == %d: %d n", CS33_size, METR243_size, CS33_size == METR243_size); printf("%d!= %d: %d n", CS33_size, METR243_size, CS33_size!= METR243_size); printf("%d < %d: %d n", CS33_size, METR243_size, CS33_size < METR243_size); printf("%d <= %d: %d n", CS33_size, METR243_size, CS33_size <= METR243_size); printf("%d > %d: %d n", CS33_size, METR243_size, CS33_size > METR243_size); printf("%d >= %d: %d n", CS33_size, METR243_size, CS33_size >= METR243_size); % gcc -o relational relational.c % relational How many students are in CS33? 64 How many students are in METR243? 57 64 == 57: 0 64!= 57: 64 < 57: 0 64 <= 57: 0 64 > 57: 64 >= 57: 9 0

Structure of Boolean Expressions A Boolean expression can be long and complicated. For example: a b c && d &&!e Terms and operators can be mixed together in almost limitless variety, but they must follow the rule that a unary operator has a term immediately to its right and a binary operator has terms on both its left and its right. Parentheses can be placed around any unary or binary expression: (a b) (c && (d && (!e))) Putting a term in parentheses may change the value of the expression, because a term inside parentheses will be calculated first. For example: a b && c is evaluated as b AND c, OR a, but is evaluated as a OR b, AND c. (a b) && c So, Boolean expressions look and behave a lot like arithmetic expressions. Precedence Order of Boolean Operations In the absence of parentheses to explicitly state the order of operations, the order of precedence is:. relational operations, left to right 2.!, left to right 3. &&, left to right 4., left to right After taking into account the above rules, the expression as a whole is evaluated left to right. For example: 0 && && 0 && 0 but 0 && ( ) && 0 && && 0 && 0 2

Another Boolean Precedence Example! 0 but! (0 )! 0 Rule of Thumb: if you can t remember the priority order of the operators, use lots of parentheses. Example: Boolean Precedence Order % cat lgcexpr.c printf("0 && && = %d n", 0 && && ); printf("0 && ( ) && = %d n", 0 && ( ) && ); printf("! 0 = %d n",! 0 ); printf("!(0 ) = %d n",!(0 )); % gcc -o lgcexpr lgcexpr.c % lgcexpr 0 && && = 0 && ( ) && = 0! 0 =!(0 ) = 0 3 4

Example: More Relational Expressions % cat comparisons.c int a, b, c; char b_equals_a, b_equals_c; char b_between_a_and_c, b_between_c_and_a; char b_outside_a_and_c; char a_lt_b_lt_c, c_lt_b_lt_a; printf(" n"); scanf("%d %d %d", &a, &b, &c); printf(" n"); printf("a = %d, b = %d, c = %d n", a, b, c); b_equals_a = (b == a); b_equals_c = (b == c); b_between_a_and_c = ((a < b) && (b < c)); b_between_c_and_a = ((c < b) && (b < a)); b_outside_a_and_c =!(b_between_a_and_c b_between_c_and_a); a_lt_b_lt_c = a < b < c; c_lt_b_lt_a = c < b < a; printf("b == a: %d n", b_equals_a); printf("b == c: %d n", b_equals_c); printf("a < b && b < c: %d n", b_between_a_and_c); printf("c < b && b < a: %d n", b_between_c_and_a); printf("a < b < c: %d n", a_lt_b_lt_c); printf("c < b < a: %d n", c_lt_b_lt_a); printf("b outside a and c: %d n", b_outside_a_and_c); % gcc -o comparisons comparisons.c 4 4 5 a = 4, b = 4, c = 5 b == a: b == c: 0 a < b && b < c: 0 c < b && b < a: 0 a < b < c: c < b < a: b outside a and c: 4 5 5 a = 4, b = 5, c = 5 b == a: 0 b == c: a < b && b < c: 0 c < b && b < a: 0 5 a < b < c: c < b < a: b outside a and c: 4 5 6 a = 4, b = 5, c = 6 b == a: 0 b == c: 0 a < b && b < c: c < b && b < a: 0 a < b < c: c < b < a: b outside a and c: 0 6 5 4 a = 6, b = 5, c = 4 b == a: 0 b == c: 0 a < b && b < c: 0 c < b && b < a: a < b < c: c < b < a: b outside a and c: 0 4 3 5 a = 4, b = 3, c = 5 b == a: 0 b == c: 0 a < b && b < c: 0 c < b && b < a: 0 a < b < c: c < b < a: b outside a and c: 6

Short Circuiting When a C program evaluates a Boolean expression, it may happen that, after evaluating some of the terms, the result can no longer change: % cat shortcircuit.c const int maximum_short_height_in_cm = 70; int my_height_in_cm = 60; char I_am_Henry = ; 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); Exercise: Writing a Program Let k be a distance in kilometers. Let m be a distance in millimeters. Then: Write a C program that inputs an int distance in kilometers and an int distance in millimeters, and determines whether they are the same distance. The body of the program must not have any numeric literal constants; all constants must be declared using appropriate variable names. Don t worry about comments. % gcc -o shortcircuit shortcircuit.c % shortcircuit I_am_Henry = my_height_in_cm = 60 I_am_tall = 0 my_middle_initial = J In such a case, the Boolean expression short circuits: the rest of the expression is not evaluated, because evaluating it takes time, but will not change the result. In the example above, the relational expression never gets evaluated, because the first operand in the OR operation ( ) evaluates to, and therefore the entire OR operation must evaluate to. 7 8