1. Trang chủ
  2. » Công Nghệ Thông Tin

Lecture Computing for management - Chapter 26

48 52 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 48
Dung lượng 709,62 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

After studying this chapter you will be able to understand: Learn about control structures; examine relational and logical operators; explore how to form and evaluate logical (boolean) expressions; discover how to use the selection control structures if, if...else, and switch in a program; learn to use the assert function to terminate a program.

Trang 1

Lecture 26

Trang 2

Summary of previous lecture

 In the previous lecture, we have been learnt,

Trang 4

 A special “type” with only two values:

 false and true

 Used to implement conditions

for selection and looping in an algorithm

Trang 5

Boolean Algebra - History

 George Boole, 1815-1864

 Initially self-guided studies of languages and philosophy

 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

Trang 6

Type int as Boolean

 In C, integers are used as Booleans

 Integer value 0 is false

 Any non-zero integer value is true

Trang 7

#include <stdio.h>

/* Test some Booleans */

int main() {

int whatever = 0;

if (whatever) {

printf(“Is that true, John?\n”); }

else {

printf(“No, Jane.\n”);

} return 0;

}

Example: What is the output?

Trang 8

#include <stdio.h>

/* Test some Booleans */

int main() {

int whatever = 1;

if (whatever) {

printf(“Is that true, John?\n”); }

else {

printf(“No, Jane.\n”);

} return 0;

}

Example: What is the output?

Trang 9

#include <stdio.h>

/* Test some Booleans */

int main() {

int whatever = -100;

if (whatever) {

printf(“Is that true, John?\n”); }

else {

printf(“No, Jane.\n”);

} return 0;

}

Example: What is the output?

Trang 10

#include <stdio.h>

/* Test some Booleans */

int main() {

int whatever = ’A’;

if (whatever) {

printf(“Is that true, John?\n”); }

else {

printf(“No, Jane.\n”);

} return 0;

}

Example: What is the output?

Trang 11

#include <stdio.h>

/* Test some Booleans */

int main() {

int whatever = 0.003;

if (whatever) {

printf(“Is that true, John?\n”); }

else {

printf(“No, Jane.\n”);

} return 0;

}

Example: What is the output?

Trang 13

Boolean Expressions

 are either strictly true or strictly false

 can be evaluated to determine their true

Trang 21

0 1

NOT  A

NOT Gate

Reminder: Gates

Trang 22

Examples:

Trang 24

Examples:

Trang 25

Examples:

Trang 27

operators

Trang 28

Boolean Algebra

 NOT false = true

 NOT true = false

 true AND true = true

 true AND false = false

 true OR true = true

 true OR false = true

 false AND false = false

 false OR false = false

if (0 || 0) printf("Does false OR false = true?");

if (0 && 0) printf("Does false AND false = true?");

Trang 29

result = age >= 18 && (haveMoney || haveCard)

&& thirst > 0.3 && ! afterHours;

printf("%d\n", result);

return 0;

Trang 30

Common Mistakes

 Using = instead of ==

Trang 34

Common Mistakes

 Using = instead of ==

 Multiple comparisons

Trang 39

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

Trang 40

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?

Trang 41

Two out of three…

 True if and only if exactly two of A,B,C are true

 Define!

Is there a better way?

Trang 42

Simplifying & Checking Boolean

Expressions

 Use Truth Tables

 Transform Expressions using Boolean Algebra

Trang 43

Axioms of Boolean Algebra

Trang 44

Axioms of Boolean Algebra (cont)

Trang 47

More Examples

ch1 = 'a';

ch2 = 'a';

printf("ch1 OR ch2 = %d\n", ch1 || ch2); // 1 printf("ch1 AND ch2 = %d\n", ch1 && ch2); // 1

ch1 = 'd';

ch2 = 'f';

printf("ch1 OR ch2 = %d\n", ch1 || ch2); // 1 printf("ch1 AND ch2 = %d\n", ch1 && ch2); // 1

ch1 = 'a';

ch2 = '\0';

printf("ch1 OR ch2 = %d\n", ch1 || ch2); // 1 printf("ch1 AND ch2 = %d\n", ch1 && ch2); // 0

Ngày đăng: 30/01/2020, 15:06