C Programming Lecture 11. The switch Statement b The switch statement is a multiway conditional...

9
C Programming C Programming Lecture 11 Lecture 11

Transcript of C Programming Lecture 11. The switch Statement b The switch statement is a multiway conditional...

Page 1: C Programming Lecture 11. The switch Statement b The switch statement is a multiway conditional statement generalizing the if-else statement.

C ProgrammingC Programming

Lecture 11Lecture 11

Page 2: C Programming Lecture 11. The switch Statement b The switch statement is a multiway conditional statement generalizing the if-else statement.

The The switchswitch Statement Statement

The The switchswitch statement is a statement is a multiway conditional multiway conditional statement statement generalizinggeneralizing the the if-elseif-else statement. statement.

Page 3: C Programming Lecture 11. The switch Statement b The switch statement is a multiway conditional statement generalizing the if-else statement.

switchswitch switch (expression){ case constant1: statement

. . .statement

case constant_2: statement. . .statement

case constant_n: statement. . .statement

default : statement. . .statement

} /* end switch */

Page 4: C Programming Lecture 11. The switch Statement b The switch statement is a multiway conditional statement generalizing the if-else statement.

The The breakbreak Statement Statement

The The breakbreak statement causes a statement causes a jump out of a switch jump out of a switch statement.statement.• Execution continues after the Execution continues after the closing brace of the switch closing brace of the switch statement.statement.

Page 5: C Programming Lecture 11. The switch Statement b The switch statement is a multiway conditional statement generalizing the if-else statement.

Example Using the Example Using the switchswitch StatementStatement

switch (val) { case 1: ++a_cnt; break; case 2: case 3: ++b_cnt; break; default: ++other_cnt;}

The val expression mustbe of an integer type.

The constant integralexpressions following thecase labels must all be unique.

Page 6: C Programming Lecture 11. The switch Statement b The switch statement is a multiway conditional statement generalizing the if-else statement.

The Effect of a The Effect of a switch switch StatementStatement

EvaluateEvaluate the switch the switch expressionexpression.. Go to the Go to the case labelcase label having a having a constant constant valuevalue that matches the value of the that matches the value of the expression found in step 1.expression found in step 1.• If a match is not found, go to the If a match is not found, go to the default labeldefault label..

• If there is no default label, If there is no default label, terminate the switch.terminate the switch.

Terminate the switch when a Terminate the switch when a breakbreak statement is encountered, statement is encountered, oror by by “falling off the end”.“falling off the end”.

Page 7: C Programming Lecture 11. The switch Statement b The switch statement is a multiway conditional statement generalizing the if-else statement.

Bell Labs Industrial Bell Labs Industrial Programming StyleProgramming Style

Follow normal rules of English, where Follow normal rules of English, where possible.possible.• e.g., put a space after a commae.g., put a space after a comma

Put one space on either side of a Put one space on either side of a binary operator.binary operator.

Indent code in a consistent fashion to Indent code in a consistent fashion to indicate flow of control.indicate flow of control.

Put braces as indicated in the Put braces as indicated in the following example:following example: for (i = 0; i < n; ++i) for (i = 0; i < n; ++i) {{ . . . . . . /* indent 3 spaces *//* indent 3 spaces */ }}

Page 8: C Programming Lecture 11. The switch Statement b The switch statement is a multiway conditional statement generalizing the if-else statement.

Student StyleStudent Style

Place Place beginningbeginning and ending and ending braces in the same column.braces in the same column. while (i < 10)while (i < 10)

{{

. . .. . .

}} Either style for braces is Either style for braces is acceptable in our class.acceptable in our class.

Page 9: C Programming Lecture 11. The switch Statement b The switch statement is a multiway conditional statement generalizing the if-else statement.

Misuse of Relational Misuse of Relational OperatorsOperators

int k = 8;int k = 8;if (if (2 < k < 72 < k < 7)) printf(“true”); printf(“true”); /*true is printed*//*true is printed*/elseelse printf(“false”);printf(“false”);

int k = 8;int k = 8;if (if (2 < k && k <2 < k && k < 7) 7) printf(“true);printf(“true);elseelse printf(“false”); printf(“false”); /*false is printed*//*false is printed*/