55
1 Selection

Lec4 Selection

Embed Size (px)

DESCRIPTION

computation c

Citation preview

1

Selection

2

Topics

• The if statement• The Boolean Type • The else statement• Cascaded if• Switch Statement• Nested if

3

The if statement• Determines whether a statement or 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 statement

or block is executed.if ( expression )

statement

4

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

5

Type int as Boolean

• In C, integers are used as Booleans• Integer value 0 is false.• Any non-zero integer value is true.

6

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

}

Example: What is the output?

7

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

}

Example: What is the output?

8

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

}

Example: What is the output?

9

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

}

Example: What is the output?

10

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

}

Example: What is the output?

11

Example: What is the output?

#include <stdio.h>

/* Test some Booleans. */

int main(){

float whatever = 0.003;

if (whatever){

printf(“Is that true, Homer?\n”);}else{

printf(“No, Marge.\n”);}return 0;

}

Behavior is “undefined.”

12

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

13

Boolean Operators

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

14

And

• True only if both Boolean arguments are true.

1 && 2

1 && 0 && -1

healthy && wealthy && wise

Examples:

15

And

• True only if both Boolean arguments are true.

1 && 2

1 && 0 && -1

healthy && wealthy && wise

Examples:

not to be confused with

“bitwise AND” (&)

16

Or

• True if either Boolean argument is true (or both are true).

1 || 2

11 || 0 || 1

good || bad || ugly

Examples:

17

Or

• True only if either Boolean argument is true (or both are true).

1 || 2

11 || 0 || 1

good || bad || ugly

Examples:not to be confused

with “bitwise OR” (|)

18

Not

• True only if the single Boolean argument is false.

! 1

! 0

! happy

Examples:

19

Equality

• True only if both arguments have identical values.

1 == 2

1 == 0

42 == 42

truth == beauty

Examples:

20

Equality

• True only if both arguments have identical values.

1 == 2

1 == 0

42 == 42

truth == beauty

Examples:

not to be confused with assignment (=)

21

Inequality

• True only if arguments have different values.

1 != 2

1 != 0

42 != 42

truth != beauty

Examples:

22

Comparison

• True only if the specified relationship holds

1 < 2

0 > 1

42 <= 42

age >= 18

Examples:

23

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.

24

What is a statement?• Statements are lines of instructions in our

programs ending with a semicolon (;).• A compound statement or block is a series

of statements surrounded by braces.{

number = number + 1;printf("%d\n", number);

}

• An empty statement is a single semicolon.

E.g.

25

#include <stdio.h>

/* Read in a number, and echo itif it is odd. */

int main(){

return 0;}

Example: oddnum.c

26

#include <stdio.h>

/* Read in a number, and echo itif it is odd. */

int main(){

int number;

printf("Enter an integer: ");scanf("%d", &number);

return 0;}

Example: oddnum.c

27

#include <stdio.h>

/* Read in a number, and echo itif it is odd. */

int main(){

int number;

printf("Enter an integer: ");scanf("%d", &number);

if (number % 2 != 0){

printf("%d\n", number);}

return 0;}

Example: oddnum.c

28

#include <stdio.h>

/* Read in a number, and echo itif it is odd. */

int main(){

int number;

printf("Enter an integer: ");scanf("%d", &number);

if (number % 2 != 0){

printf("%d\n", number);}

return 0;}

Example: oddnum.c

Do not put “then” here!

29

#include <stdio.h>

/* Read in a number, and echo itif it is odd. */

int main(){

int number;

printf("Enter an integer: ");scanf("%d", &number);

if (number % 2 != 0){

printf("%d\n", number);}

return 0;}

Example: oddnum.c

Do not put semicolon here!

30

#include <stdio.h>

/* Read in a number, and echo itif it is odd. */

int main(){

int number;

printf("Enter an integer: ");scanf("%d", &number);

if (number % 2 != 0){

printf("%d\n", number);}

return 0;}

Example: oddnum.c

31

Notes on if• Which of the following code fragments are equivalent?

if (number % 2 != 0){

printf("%d", number);}printf(” is odd\n");

if (number % 2 != 0)printf("%d", number); printf(” is odd\n");

if (number % 2 != 0){

printf("%d", number);printf(” is odd\n");

}

A

B

C

32

Notes on if• Which of the following code fragments are equivalent?

if (number % 2 != 0){

printf("%d", number);}printf(” is odd\n");

if (number % 2 != 0)printf("%d", number);printf(” is odd\n");

if (number % 2 != 0){

printf("%d", number);printf(” is odd\n");

}

A

B

C

33

Notes on if• Which of the following code fragments are equivalent?

if (number % 2 != 0){

printf("%d", number);}printf(” is odd\n");

if (number % 2 != 0)printf("%d", number);printf(” is odd\n");

if (number % 2 != 0){

printf("%d", number);printf(” is odd\n");

}

A

B

CA Statement

A Compound Statement

34

Notes on if• Common mistake

if (number % 2 != 0);{

printf("%d is an odd ", number);}printf("number\n");

35

Notes on if• Common mistake

if (number % 2 != 0);{

printf("%d is an odd ", number);}printf("number\n");

No semi-colon here!

The semicolon is an empty statement.

36

Notes on if• Common mistake

if (number = 0){

printf("%d\n", number);}printf("%d\n", number);

37

Notes on if• Common mistake

if (number = 0){

printf("%d\n", number);}printf("%d\n", number);

Should be ==

38

The else statement

• Can only occur after an if statement• Is only executed when the if block does

not execute

if ( expression )statement1

elsestatement2

39

#include <stdio.h>

/* Determine whether an input numberis 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);

}

}

Example: oddeven.c

40

#include <stdio.h>

/* Determine whether an input numberis 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", number);

}}

Example: oddeven.c

41

#include <stdio.h>

/* Determine whether an input numberis 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", number);

}}

Example: oddeven.c

No semicolons here!

42

#include <stdio.h>

/* Determine whether an input numberis 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", number);

}}

Example: oddeven.c

43

Cascaded if statement

• Multiple alternative blocks each with a Boolean expression.

• First expression which evaluates to true causes execution of the associated block.

• At most only one block will be executed.

44

Example: months.c

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”}

45

int main(){

return 0;}

Example: months.c

#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 clearAnd 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;

46

int main(){int month;printf("Enter number of month: ");scanf("%d", &month);

return 0;}

Example: months.c

#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 clearAnd 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

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

Example: months.c

#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 clearAnd 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

#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 clearAnd 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;

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

Example: months.c

Common mistake:

if (month==September || April || June || November )

49

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

Example: months.c

#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 clearAnd 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

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;

}

Example: months.c

#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 clearAnd 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;

51

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;

}

Example: months.c

#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 clearAnd 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.

52

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;

}

Example: months.c

#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 clearAnd 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;

53

Notes on Cascaded if

if (letter >= ’a’){

printf(“S1\n”);}else if (letter <= ’z’){

printf(“S2\n”);}else if (letter >= ’A’){

printf(“S3\n”);}else if (letter <= ’Z’){

printf(“S4\n”);}

What is the output if:

• letter is equal to ‘b’

• letter is equal to ‘z’

• letter is equal to ‘A’

• letter is equal to ‘X’

Q:

54

More Examples

if (ch >= ’a’ && ch <= ’z’){

printf(“%c is in lower case.\n”, ch);}else if (ch >= ’A’ && ch <= ’Z’){

printf(“%c is in upper case.\n”. ch);}else if (ch >= ’0’ && ch <= ’9’){

printf(“%c is a digit with value %d.\n”, ch, ch - ’0’);}

55

if (ch >= ’a’ && ch <= ’z’){

printf(“%c is in lower case.\n”, ch);}else if (ch >= ’A’ && ch <= ’Z’){

printf(“%c is in upper case.\n”. ch);}else if (ch >= ’0’ && ch <= ’9’){

printf(“%c is a digit with value %d.\n”, ch, ch - ’0’);}

More Examples