Selection Statements (2)

24
Selection Statements (2) Program Design (I) 2021 Fall Fu-Yin Cherng Dept. CSIE, National Chung Cheng University 1

Transcript of Selection Statements (2)

Selection Statements (2)Program Design (I)

2021 Fall Fu-Yin Cherng

Dept. CSIE, National Chung Cheng University

1

Outline

● Conditional Expression● Boolean Value● The switch statement

2

Conditional Expressions

● C’s conditional operator allows an expression to produce one of two values depending on the value of a condition.

● The conditional operator consists of two symbols (? and :), which must be used together

● A bit hard to understand, but a shorter and useful expression○ can achieve same result by using if statement○ still be used in some modern languages, for example, R and Python

3

expr1 ? expr2 : expr3

Conditional Expressions

● “if expr1 then expr2 else expr3”● The expression is evaluated in stages● expr1 is evaluated first; ● if its value isn’t zero (true), then expr2 is evaluated, and its value (expr2) is

the value of the entire conditional expression.● If the value of expr1 is zero (false), then the value of expr3 is the value of

the conditional expression

4

expr1 ? expr2 : expr3

nonzero (true)

zero (false)

Conditional Expressions

5

int i = 1, j = 2, k;

k = i > j ? i : j; /* k is now __(1)__ */

k = (i >= 0 ? i : 0) + j; /* k is now __(2)__*/compare the value of i and j, if i > j is true, then return value of i. If i is less than j, then return j. k is used to store the returned value

What is the value of (1) ?

Conditional Expressions

7

int i = 1, j = 2, k;

k = i > j ? i : j; /* k is now 2 */

k = i >= 0 ? i : 0; + /* k is now __(2)__*/

What is the meaning of this condition expression ?

What is the value of (2) ?

Conditional Expressions

9

int i = 1, j = 2, k;

k = i > j ? i : j; /* k is now 2 */

k = (i >= 0 ? i : 0) + j; /* k is now 3 */

parentheses are necessary; precedence of conditional operators is less than other operators (except the assignment operator; store the value after finishing all computation)

Conditional Expressions

● Conditional expressions are often used in return statements○ return i > j ? i : j;

● Calls of printf can sometimes benefit from condition expressions

10

if (i > j)

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

else

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

printf("%d\n", i > j ? i : j); //shorter way

Boolean Values

● True (1), False (0) => Boolean Values● You can actually declare variable which type is Boolean● This type of variable can be used to represent the true or false● What’s the meaning of Boolean?

○ name after George Boole○ mathematician, philosopher, and logician (1815 - 1864)○ creator of Boolean algebra○ fundations for the modern information and computer sicence

11https://en.wikipedia.org/wiki/George_Boole

Boolean Values

● There are two ways to declare variable with Boolean type● _Bool: is an integer type, so a _Bool variable is really just an integer

variable in disguise.● however, a _Bool variable can only be assigned 0 or 1

12

_Bool flag;

if (flag) statement;

// tests whether flag is 1 (true)

Boolean Values

● The second way is to use <stdbool.h> header which makes it easier to work with Boolean values.

● supplies macros named true and false, which stand for 1 and 0,

13

_Bool flag;

if (flag) statement;

// tests whether flag is 1 (true)

#include <stdio.h>

#include <stdbool.h>

...

bool flag;

flag = false; //0

flag = true; //1

How to print Boolean values?

14https://stackoverflow.com/questions/17307275/what-is-the-printf-format-specifier-for-bool

The switch Statement

● When there is a series of values/conditions you need to match/compared, you might used casecaded if statment

● C provides an alternative which is easier to read and faster

15

if (grade == 4)

printf("Excellent");

else if (grade == 3)

printf("Good");

else if (grade == 2)

printf("Average");

else if (grade == 1)

printf("Poor");

else if (grade == 0)

printf("Failing");

else

printf("Illegal grade");

The switch Statement

● The switch statement ● switch statement will test the

value of grade variable ● If match 4, executed the

statements in case 4○ print “Excellent”○ break statement makes the

execution flow jump out of the switch statement

16

switch (grade) {

case 4: printf("Excellent");

break;

case 3: printf("Good");

break;

case 2: printf("Average");

break;

case 1: printf("Poor");

break;

case 0: printf("Failing");

break;

default: printf("Illegal grade");

break;

}

...

if grade == 4

The switch Statement

● The switch statement ● switch statement will test the

value of grade variable ● If match 4, executed the

statements in case 4○ print “Excellent”○ break statement makes the

execution flow jump out of the switch statement

● If don’t match any number from 0 to 4, go to default

17

switch (grade) {

case 4: printf("Excellent");

break;

case 3: printf("Good");

break;

case 2: printf("Average");

break;

case 1: printf("Poor");

break;

case 0: printf("Failing");

break;

default: printf("Illegal grade");

break;

}

...

if grade is out of the range from 0 to 4

The switch Statement

● Common form of the switch statement

● switch○ must be followed by an controlling

expression in parentheses● controlling expression

○ integer expression (e.g., grade)○ Can’t use floating-point numbers

(float grade; ) and strings (“hello”)

18

switch ( controlling expression ) {

case constant-expression : statements

case constant-expression : statements

default : statements}

The switch Statement

● Case labels○ Each case begins with a label

(constant-expression)● Constant-expression

○ ordinary expression except that it can’t contain variables or function calls.

○ expressions only contain constant● For example

○ 5, 5 + 1○ n + 10 (not constant expression)

19

switch ( expression ) {

case constant-expression : statements

case constant-expression : statements

default : statements}

The switch Statement

● Statements○ After each case label comes any

number of statements○ No braces ({ }) are required around

the multiple statements (rare case in C)

○ The last statement in each group is normally break.

20

switch ( expression ) {

case constant-expression : statements

case constant-expression : statements

default : statements}

The switch Statement

● Duplicate case labels aren’t allowed.○ can’t have two case 4 in a switch

● The order of the cases doesn’t matter, and the default case doesn’t need to come last.

● Several case labels may precede a group of statements

○ when grade is 1, 2, 3, or 4, will always print Passing

● If default is missing, just continue run the next statement after switch

21

switch (grade) { case 4: case 3: case 2: case 1: printf("Passing"); break; case 0: printf("Failing"); break; default:

printf("IllegalGrade"); break;

}

The Role of the break Statement

22

switch (grade) { //if grade is 3

case 4: printf("Excellent");

case 3: printf("Good");

case 2: printf("Average");

case 1: printf("Poor");

case 0: printf("Failing");

default: printf("Illegal grade");

}

// if grade is 3, printed message is:

// GoodAveragePoorFailingIllegal grade

● break helps the program to “break” out of the switch statement after finish the statements in the matched case

○ Continue to execute next statement after the switch

● What happen without break?○ continue run the statements of next

case!

The Role of the break Statement

23

switch (grade) { //if grade is 3

case 4: printf("Excellent");

case 3: printf("Good");

case 2: printf("Average");

case 1: printf("Poor");

case 0: printf("Failing");

default: printf("Illegal grade");

}

// if grade is 3, printed message is:

// GoodAveragePoorFailingIllegal grade

● Forgetting to use break is a common error

● Always remember to put one after each case (including default) in switch statement

Summary

● Conditional Expression○ expr1 ? expr2 : expr3

● Boolean Value○ _Bool○ <stdbool.h>, boll

● The switch statement○ general form○ introduction of each component○ break statement

24