0% found this document useful (0 votes)
27 views37 pages

Lect 6

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views37 pages

Lect 6

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 37

GLA CRASH COURSE

Booleans/Logical operators

Lecture 6

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

2
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

3
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

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

5
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;
}
6
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;
}
7
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;
}
8
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;
}
9
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;
}
10
Example: What is the output?
Behavior is
#include <stdio.h>
“undefined.”
/* Test some Booleans. */

int main()
{
float whatever = 0.003;

if (whatever)
{
printf(“Is that true, Homer?\n”);
}
else
{
printf(“No, Marge.\n”);
}
return 0;
}
11
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

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

13
And
 True only if both Boolean arguments are
true.
Examples:

1 && 2

1 && 0 && -1

healthy && wealthy && wise

14
And
 True only if both Boolean arguments are
true.
Examples:

1 && 2
not to be
confused with
1 && 0 && -1
“bitwise
AND” (&)
healthy && wealthy && wise

15
Or
 True if either Boolean argument is true (or
both are true).

Examples:

1 || 2

11 || 0 || 1

good || bad || ugly

16
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

17
Not
 True only if the single Boolean argument is
false.

Examples:

! 1

! 0

! happy

18
Reminder: Gates

AND Gate OR Gate

A B A AND B A OR B
0 0
0 1
1 0
1 1
19
Reminder: Gates

NOT Gate

A NOT A
0
1

20
Equality
 True only if both arguments have identical
values.
Examples:
1 == 2

1 == 0

42 == 42

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

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

1 != 0

42 != 42

truth != beauty
23
Comparison
 True only if the specified relationship holds

Examples:
1 < 2

0 > 1

42 <= 42

age >= 18

24
Short-circuiting
 A complex Boolean expression is only
evaluated as far as necessary

Examples:

1 || 2

0 || 1 || 2

1 && 0 && -1

25
Precedence
Highest to lowest:
 Brackets
 Not (!)
 Comparison (<, >, <=, >=)
 Equality (==)
 Inequality (!=) Note: The assignment
operator (=) is lower in
 And (&&) order of precedence
than Boolean / Logical
 Or (||)
operators.
26
Example:
#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
27
Common Mistakes
 Using = instead of ==

28
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;
}
boolerr1.c
29
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");
} Usual error message (if any):
“LValue required...”
return 0;
}
boolerr1.c
30
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;
}
boolerr1.c
31
Common Mistakes
 Using = instead of ==
 Multiple comparisons

32
Example:

#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
33
Example:

#include <stdio.h>

/* Another common C error. */

int main()
{
int score;

scanf("%d", &score);

if ( 0 < score < 48 )


{
printf("Fail\n");
} 0 or 1
return 0;
}
boolerr2.c
34
Example:

#include <stdio.h>

/* Another common C error. */

int main()
{
int score;
always 1
scanf("%d", &score);

if ( 0 < score < 48 )


{
printf("Fail\n");
} 0 or 1
return 0;
}
boolerr2.c
35
Example:

#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
36
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 ;
37

You might also like