Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Computing Lecture 04: Booleans & Selection

Similar presentations


Presentation on theme: "Introduction to Computing Lecture 04: Booleans & Selection"— Presentation transcript:

1 Introduction to Computing Lecture 04: Booleans & Selection
Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical & Electronics Engineering

2 Topics Booleans if and else statements

3 Boolean A special “type” with only two values: false and true
Used to implement the conditions for selection and looping in an algorithm

4 Boolean Expressions ...are expressions that can be evaluated to either strictly true or strictly false A Boolean expression which evaluates to true has integer value 1; otherwise, 0

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

6 Not True only if the single Boolean argument is false Examples: ! 1
! 0 ! happy

7 Equality True only if both arguments have identical values Examples:
1 == 2 1 == 0 42 == 42 truth == beauty

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

9 Inequality True only if arguments have different values Examples:
1 != 2 1 != 0 42 != 42 num != 54

10 Comparison True only if the specified relationship holds Examples:
1 < 2 0 > 1 42 <= 42 age >= 18

11 Comparison True only if the specified relationship holds Examples:
1 < 2 0 > 1 42 <= 42 age >= 18 Careful!

12 And True only if both Boolean arguments are true Examples: 1 && 2
0 <= n && n <= 100 1 && 0 && -1

13 not to be confused with “bitwise AND” (&)
True only if both Boolean arguments are true. Examples: 1 && 2 0 <= n && n <= 100 1 && 0 && -1 not to be confused with “bitwise AND” (&)

14 Or True only if either Boolean argument is true (or both are true)
Examples: 1 || 2 11 || 0 || 1 good || bad || ugly

15 not to be confused with “bitwise 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

16 Truth Table P Q !P P && Q P || Q false true

17 Precedence Highest to lowest: Brackets Not (!)
Comparison (<, >, <=, >=) Equality (==) Inequality (!=) And (&&) Or (||) Note: The assignment operator (=) is lower in order of precedence than Boolean / Logical operators

18 Example: bool.c #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

19 Example: bool.c #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

20 The if statement Determines whether a block is executed
Implements the selection instructions within an algorithm Decides what to do by evaluating a Boolean expression If the expression is true (non-zero), the block is executed

21 Example: oddnum.c Read in a number, and print it if it is odd.
output “Enter an integer” input number if (number is odd) then { output the number }

22 Example: oddnum.c #include <stdio.h>
/* Read in a number, and echo it if it is odd. */ int main() { return 0; } Read in a number, and print it if it is odd. output “Enter an integer” input number if (number is odd) then { output the number }

23 Example: oddnum.c #include <stdio.h>
/* Read in a number, and echo it if it is odd. */ int main() { int number; printf("Enter an integer: "); scanf("%d", &number); return 0; } Read in a number, and print it if it is odd. output “Enter an integer” input number if (number is odd) then { output the number }

24 Example: oddnum.c #include <stdio.h>
/* Read in a number, and echo it if it is odd. */ int main() { int number; printf("Enter an integer: "); scanf("%d", &number); if (number % 2 != 0) printf("%d\n", number); } return 0; Read in a number, and print it if it is odd. output “Enter an integer” input number if (number is odd) then { output the number }

25 Example: oddnum.c Do not put “then” here! #include <stdio.h>
/* Read in a number, and echo it if it is odd. */ int main() { int number; printf("Enter an integer: "); scanf("%d", &number); if (number % 2 != 0) printf("%d\n", number); } return 0; Read in a number, and print it if it is odd. output “Enter an integer” input number if (number is odd) then { output the number } Do not put “then” here!

26 Do not put semicolon here!
Example: oddnum.c #include <stdio.h> /* Read in a number, and echo it if it is odd. */ int main() { int number; printf("Enter an integer: "); scanf("%d", &number); if (number % 2 != 0) printf("%d\n", number); } return 0; Read in a number, and print it if it is odd. output “Enter an integer” input number if (number is odd) then { output the number } Do not put semicolon here!

27 Example: oddnum.c #include <stdio.h>
/* Read in a number, and echo it if it is odd. */ int main() { int number; printf("Enter an integer: "); scanf("%d", &number); if (number % 2 != 0) printf("%d\n", number); } return 0; Read in a number, and print it if it is odd. output “Enter an integer” input number if (number is odd) then { output the number }

28 Syntax for if statement
if (condition) statement; OR { statement1; statementn; }

29 Notes on if Which of the following code fragments are equivalent? A if (number % 2 != 0) { printf("%d", number); } printf(” is odd\n"); B if (number % 2 != 0) printf("%d", number); printf(” is odd\n"); C if (number % 2 != 0) { printf("%d", number); printf(” is odd\n"); }

30 Notes on if Common mistakes if (number % 2 != 0); {
printf("%d is an odd ", number); } printf("number\n");

31 Notes on if Common mistakes if (number % 2 != 0); {
printf("%d is an odd ", number); } printf("number\n"); No semi-colon here!

32 Notes on if Common mistakes if (number = 0) { printf("%d\n", number);
}

33 Notes on if Common mistakes if (number = 0) { printf("%d\n", number);
} Should be ==

34 The else statement Can only occur after an if statement
Is only executed when the if’s block does not execute

35 Example: oddeven.c #include <stdio.h>
/* Determine whether an input number is odd or even. */ main() { int number; printf("Enter an integer: "); scanf("%d", &number); if (number % 2 != 0) printf("%d is an odd number\n", number); } Read in a number, and determine if it’s odd or even. output “Enter an integer” input number if (number is odd) then { output: number “ is an odd number” } else output: number “ is an even number”

36 Example: oddeven.c #include <stdio.h>
/* Determine whether an input number is odd or even. */ main() { int number; printf("Enter an integer: "); scanf("%d", &number); if (number % 2 != 0) printf("%d is an odd number\n", number); } else printf("%d is an even number\n", Read in a number, and determine if it’s odd or even. output “Enter an integer” input number if (number is odd) then { output: number “ is an odd number” } else output: number “ is an even number”

37 Example: oddeven.c No semicolons here! No semicolons here!
#include <stdio.h> /* Determine whether an input number is odd or even. */ main() { int number; printf("Enter an integer: "); scanf("%d", &number); if (number % 2 != 0) printf("%d is an odd number\n", number); } else printf("%d is an even number\n", Read in a number, and determine if it’s odd or even. output “Enter an integer” input number if (number is odd) then { output: number “ is an odd number” } else output: number “ is an even number” No semicolons here! No semicolons here!

38 Example: oddeven.c #include <stdio.h>
/* Determine whether an input number is odd or even. */ main() { int number; printf("Enter an integer: "); scanf("%d", &number); if (number % 2 != 0) printf("%d is an odd number\n", number); } else printf("%d is an even number\n", Read in a number, and determine if it’s odd or even. output “Enter an integer” input number if (number is odd) then { output: number “ is an odd number” } else output: number “ is an even number”

39 Syntax for if & else statement
if (condition) statementT; else statementF; OR { statementT1; statementTn; } statementF1; statementFn;

40 Cascaded if statement Multiple alternative blocks each with a Boolean expression First expression which evaluates to true causes execution of the associated block Only at most one block will be executed

41 Example: months.c Determine the number of days in a given month:
30 days hath September, April, June and November. All the rest hath 31, Excepting February alone, Which hath 28 days clear, And 29 in each leap year. output “Enter an integer” input month if (month is September, or April, or June, or November) then { output “30 days” } else if (month is February) output “28 or 29 days” else output “31 days”

42 Example: months.c int main() { return 0; } #include <stdio.h>
/*************************\ Determine the number of days in a given month: 30 days hath September, April, June and November; All the rest have 31, Excepting February alone, And that has 28 days clear And 29 in each leap year. \*************************/ const int September = 9; const int April = 4; const int June = 6; const int November = 11; const int February = 2;

43 Example: months.c int main() { int month;
printf("Enter number of month: "); scanf("%d", &month); return 0; } #include <stdio.h> /*************************\ Determine the number of days in a given month: 30 days hath September, April, June and November; All the rest have 31, Excepting February alone, And that has 28 days clear And 29 in each leap year. \*************************/ const int September = 9; const int April = 4; const int June = 6; const int November = 11; const int February = 2;

44 Example: months.c int main() { int month;
printf("Enter number of month: "); scanf("%d", &month); if (month==September || month==April || month==June || month==November ) printf("30 days\n"); } return 0; #include <stdio.h> /*************************\ Determine the number of days in a given month: 30 days hath September, April, June and November; All the rest have 31, Excepting February alone, And that has 28 days clear And 29 in each leap year. \*************************/ const int September = 9; const int April = 4; const int June = 6; const int November = 11; const int February = 2;

45 if (month==September || April || June || November )
Example: months.c int main() { int month; printf("Enter number of month: "); scanf("%d", &month); if (month==September || month==April || month==June || month==November ) printf("30 days\n"); } return 0; #include <stdio.h> /*************************\ Determine the number of days in a given month: 30 days hath September, April, June and November; All the rest have 31, Excepting February alone, And that has 28 days clear And 29 in each leap year. \*************************/ const int September = 9; const int April = 4; const int June = 6; const int November = 11; const int February = 2; Common mistake: if (month==September || April || June || November )

46 Example: months.c int main() { int month;
printf("Enter number of month: "); scanf("%d", &month); if (month==September || month==April || month==June || month==November ) printf("30 days\n"); } else if (month==February) printf("28 or 29 days\n"); return 0; #include <stdio.h> /*************************\ Determine the number of days in a given month: 30 days hath September, April, June and November; All the rest have 31, Excepting February alone, And that has 28 days clear And 29 in each leap year. \*************************/ const int September = 9; const int April = 4; const int June = 6; const int November = 11; const int February = 2;

47 Example: months.c int main() { int month;
printf("Enter number of month: "); scanf("%d", &month); if (month==September || month==April || month==June || month==November ) printf("30 days\n"); } else if (month==February) printf("28 or 29 days\n"); else printf("31 days\n"); return 0; #include <stdio.h> /*************************\ Determine the number of days in a given month: 30 days hath September, April, June and November; All the rest have 31, Excepting February alone, And that has 28 days clear And 29 in each leap year. \*************************/ const int September = 9; const int April = 4; const int June = 6; const int November = 11; const int February = 2;

48 “Default” block. Example: months.c int main() { int month;
printf("Enter number of month: "); scanf("%d", &month); if (month==September || month==April || month==June || month==November ) printf("30 days\n"); } else if (month==February) printf("28 or 29 days\n"); else printf("31 days\n"); return 0; #include <stdio.h> /*************************\ Determine the number of days in a given month: 30 days hath September, April, June and November; All the rest have 31, Excepting February alone, And that has 28 days clear And 29 in each leap year. \*************************/ const int September = 9; const int April = 4; const int June = 6; const int November = 11; const int February = 2; “Default” block.

49 Example: months.c int main() { int month;
printf("Enter number of month: "); scanf("%d", &month); if (month==September || month==April || month==June || month==November ) printf("30 days\n"); } else if (month==February) printf("28 or 29 days\n"); else printf("31 days\n"); return 0; #include <stdio.h> /*************************\ Determine the number of days in a given month: 30 days hath September, April, June and November; All the rest have 31, Excepting February alone, And that has 28 days clear And 29 in each leap year. \*************************/ const int September = 9; const int April = 4; const int June = 6; const int November = 11; const int February = 2;

50 Syntax for cascaded if statements
if (condition1) statement1; else if (condition2) statement2; . else if (conditionn) statementn; else statemente;

51 Q: Notes on Cascaded if What is the output if: number is equal to 5
{ printf(“S1\n”); } else if (number <= 40) printf(“S2\n”); else if (number >= 10) printf(“S3\n”); else if (number <= 20) printf(“S4\n”); Q: What is the output if: number is equal to 5 number is equal to 20 number is equal to 30 number is equal to 35

52 Common Mistakes Using = instead of ==

53 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;

54 Usual error message (if any): “LValue required...”
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; Usual error message (if any): “LValue required...”

55 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;

56 Common Mistakes Using = instead of == Multiple comparisons

57 Example: #include <stdio.h> /* Another common C error. */
int main() { int score; scanf("%d", &score); if ( 0 < score < 48 ) printf("Fail\n"); } return 0;

58 0 or 1 Example: #include <stdio.h> /* Another common C error. */
int main() { int score; scanf("%d", &score); if ( 0 < score < 48 ) printf("Fail\n"); } return 0; 0 or 1

59 always 1 0 or 1 Example: #include <stdio.h>
/* Another common C error. */ int main() { int score; scanf("%d", &score); if ( 0 < score < 48 ) printf("Fail\n"); } return 0; always 1 0 or 1

60 Example: #include <stdio.h> /* Another common C error. */
int main() { int score; scanf("%d", &score); if ( 0 < score && score < 60 ) printf("Fail\n"); } return 0;

61 Summary Booleans Boolean expressions Boolean Operators Precedence
if and else statements Common mistakes


Download ppt "Introduction to Computing Lecture 04: Booleans & Selection"

Similar presentations


Ads by Google