Top Banner
Chapter 9 Complex Selections and Repetitions
26

Chapter 9 Complex Selections and Repetitions. 9.1 INTRODUCTION Then we introduce the switch statement, which can also be used for multiway selection.

Dec 20, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Chapter 9 Complex Selections and Repetitions. 9.1 INTRODUCTION Then we introduce the switch statement, which can also be used for multiway selection.

Chapter 9

Complex Selections and Repetitions

Page 2: Chapter 9 Complex Selections and Repetitions. 9.1 INTRODUCTION Then we introduce the switch statement, which can also be used for multiway selection.

• 9.1 INTRODUCTION

• Then we introduce the switch statement, which can also be used for multiway selection.

• For statement, which is another pretest repetition structure.

• Do-while statement. The do-while statement is the C implementation of the posttest repetition structure.

Page 3: Chapter 9 Complex Selections and Repetitions. 9.1 INTRODUCTION Then we introduce the switch statement, which can also be used for multiway selection.

• 9.2 COMPLEX PREDICATES

• Logical Expressions and Logical Operatiors– Logical and (conjunction)– Logical or (disjunction)– Logical not (negation)

• The C Logical Operators– Logical and is represented by &&.– The symbol || stands for logical or.– The C logical not operator is !.

• Example 9.1 (semester_average >= 80 ) && (semester_average <= 89)

Page 4: Chapter 9 Complex Selections and Repetitions. 9.1 INTRODUCTION Then we introduce the switch statement, which can also be used for multiway selection.

• Example 9.3

! (student_status == ‘u’)

• Simplifying Complex Predicates

in some selection problems there are several forms for expressing the same condition.

We should try to get rid of the negation and find the simplest form in order to enhance the readability of the code.

Page 5: Chapter 9 Complex Selections and Repetitions. 9.1 INTRODUCTION Then we introduce the switch statement, which can also be used for multiway selection.

Predicate Equivalent Simple Form

! (a == b) a != b

! (a < b) a >= b

! (a > b) a <= b! (Expression_1 && Expression_2) (! Expression_1) || (Expression_2)

Figure 9.1 Equivalent forms for predicates involving negation

Page 6: Chapter 9 Complex Selections and Repetitions. 9.1 INTRODUCTION Then we introduce the switch statement, which can also be used for multiway selection.

• Precedence of Logical OperatorsLogical not , unary arithmetic operators

Binary arithmetic operators

Relational operators

Logical and

Logical or

• Example 9.6x+y >= 13 && ! (x-y) || x*y -16 == 4

4 6 8 2 1 9 3 5 7

Page 7: Chapter 9 Complex Selections and Repetitions. 9.1 INTRODUCTION Then we introduce the switch statement, which can also be used for multiway selection.

• 9.3 MULTIWAY SELECTION USING THE switch AND break STATEMENTS

switch (ControllingExpression){CaseClause-1

CaseClause-2

.

.

CaseClause-n

DefaultClause

} /* end switch */

Page 8: Chapter 9 Complex Selections and Repetitions. 9.1 INTRODUCTION Then we introduce the switch statement, which can also be used for multiway selection.

• In the switch statement the controlling expression is evaluated first. The controlling expression must compute to an integral value and must be of type int or char;

• If the value of the controlling expression does not match any of the constant values in the case clauses, the content of the default clause is executed.

Page 9: Chapter 9 Complex Selections and Repetitions. 9.1 INTRODUCTION Then we introduce the switch statement, which can also be used for multiway selection.

switch (major_code) {

case 1 :

printf(“student major is computer science.”;

break;

case 7 :

printf(“Student major is computer engineering.”;

break;

default :

printf(“Student major is a noncomputer field.”);

} /* end switch */

Page 10: Chapter 9 Complex Selections and Repetitions. 9.1 INTRODUCTION Then we introduce the switch statement, which can also be used for multiway selection.

• If the actions of two or more consecutive cases are identical, all we have to do is list the cases with empty statements and specify the action and a break statement in the final case.

Page 11: Chapter 9 Complex Selections and Repetitions. 9.1 INTRODUCTION Then we introduce the switch statement, which can also be used for multiway selection.

switch (character) {

case ‘0’ : case ‘1’ : case ‘2’ : case ‘3’ : case ‘4’ : case ‘5’ : case ‘6’ : case ‘7’ :

case ‘8’ : case ‘9’ :

printf(“The content is a decimal integer.”);

break;

default :

printf(“The content is a nondecimal character.”);

} /* end swithc */

Page 12: Chapter 9 Complex Selections and Repetitions. 9.1 INTRODUCTION Then we introduce the switch statement, which can also be used for multiway selection.

• 9.4 STYLE CONSIDERATIONS FOR MULTIWAY SELECTION STRUCTURES– Keep the use of logical negation to a minimum in

forming complex predicates.– Remember that a nested if statement is more gene

ral than a switch statement.– Use proper indentations in forming switch statem

ents.– Whenever possible, use a default clause in your s

witch statements.

Page 13: Chapter 9 Complex Selections and Repetitions. 9.1 INTRODUCTION Then we introduce the switch statement, which can also be used for multiway selection.

• 9.5 THE PRETEST REPETITION STRUCTURE

• The for Statement for Pretest Repetition

for (InitializationExpression; LoopControExpression;

UpdateExpression)

Loopbady

/* end for */

Page 14: Chapter 9 Complex Selections and Repetitions. 9.1 INTRODUCTION Then we introduce the switch statement, which can also be used for multiway selection.

• Executes the InitializationExpression.

• Evaluates the LoopControlExpression. If it computes to zero, the loop is exited.

• If the LoopControlExpression yields a nonzero value, the LoopBody is executed and then the UpdateExpression is evaluated.

• Tests the LoopControlExpression again. Thus the LoopBody is repeated until the LoopControlExpression computes to a zero value.

Page 15: Chapter 9 Complex Selections and Repetitions. 9.1 INTRODUCTION Then we introduce the switch statement, which can also be used for multiway selection.

int number, sum = 0 , counter = 1;

…………………….

…………………….

While (counter <= number) {

sum = sum + counter ;

counter = counter + 1;

} /* end while */

Page 16: Chapter 9 Complex Selections and Repetitions. 9.1 INTRODUCTION Then we introduce the switch statement, which can also be used for multiway selection.

int number , sum , counter ;

……………………..

……………………..

Sum = 0;

for (counter = 1 ; counter <= number ; counter = counter + 1)

sum = sum + counter;

/* end for */

Page 17: Chapter 9 Complex Selections and Repetitions. 9.1 INTRODUCTION Then we introduce the switch statement, which can also be used for multiway selection.

• Equivalence of for and while Statementsthe same repetition problem can be expressed equivalently using either a while or a for statement.

• Using the for Statement for Counter-Controlled Loops

• Using the for Statement for Sentinel-Controlled Loops

Page 18: Chapter 9 Complex Selections and Repetitions. 9.1 INTRODUCTION Then we introduce the switch statement, which can also be used for multiway selection.

char answer;

printf(“Do you want to continue? (y/n):”);

for (scanf(“%c”, &answer);

answer != ‘y’ && answer != ‘Y’ && answer != ‘n’ && answer != ‘N’

;

scanf(“%c” , &answer))

printf(“Please type y or n: “);

/* end for */

Page 19: Chapter 9 Complex Selections and Repetitions. 9.1 INTRODUCTION Then we introduce the switch statement, which can also be used for multiway selection.

• Checking for Incorrect Data in a Loop and the continue Statement

we may prefer to warn the user and skip the rest of the loop body. Causes the program control to skip the rest of the loop body and execute the loop again.

Page 20: Chapter 9 Complex Selections and Repetitions. 9.1 INTRODUCTION Then we introduce the switch statement, which can also be used for multiway selection.

for (scanf(“%d”, &test_score); test_score !=0 ;

scanf(“%d”, &test_score)) {

if (test_score <0 || test_score >100) {

printf(“Incorrect test score ! Enter a correct value : “) ;

continue;

} /* end if */

sum = sum + test_score;

number_of_students = number_of students + 1;

printf(“Enter a test score. Enter 0 to stop: “);

} /* end for */

Page 21: Chapter 9 Complex Selections and Repetitions. 9.1 INTRODUCTION Then we introduce the switch statement, which can also be used for multiway selection.

• 9.6 THE POSTTEST REPETITION STRUCTURE

the loop body is executed before the loop control expression is tested

• The do-while Statement– It executes the LoopBody.– It evaluates the LoopControlExpression. If the

value of the LoopControlEcpression is 0, the computer exits the loop; otherwise, it does the LoopBody again.

Page 22: Chapter 9 Complex Selections and Repetitions. 9.1 INTRODUCTION Then we introduce the switch statement, which can also be used for multiway selection.

int number, sum = 0 , counter = 0;

…………………….

…………………….

Do {

sum = sum + counter ;

counter = counter + 1 ;

} while (counter <= number) ;

/* end do-while */

• Use of do-while for Counter- and Sentinel-Controlled Loops

Page 23: Chapter 9 Complex Selections and Repetitions. 9.1 INTRODUCTION Then we introduce the switch statement, which can also be used for multiway selection.

• 9.7 NESTED LOOPSA nested loop is a repetition structure that contains one or more loops in its body. A loop contained in another loop forms a doubly nested loop.

int control_var1, control_var2;

for (control_var1=1; control_var1<=8 ; control_var1 +=2)

for (control_var2 = control_var1 ; control_var2 <= 10 ;

control_var2 +=3 )

printf(“control_var1 = %d control_var2 = %d\n” ,

control_var1, control_var2);

/* end for */

/*end for */

Page 24: Chapter 9 Complex Selections and Repetitions. 9.1 INTRODUCTION Then we introduce the switch statement, which can also be used for multiway selection.

control_var1 = 1 control_var2 = 1

control_var1 = 1 control_var2 = 4

control_var1 = 1 control_var2 = 7

control_var1 = 1 control_var2 = 10

control_var1 = 3 control_var2 = 3

control_var1 = 3 control_var2 = 6

control_var1 = 3 control_var2 = 9

control_var1 = 5 control_var2 = 5

control_var1 = 5 control_var2 = 8

control_var1 = 7 control_var2 = 7

control_var1 = 7 control_var2 = 10

Page 25: Chapter 9 Complex Selections and Repetitions. 9.1 INTRODUCTION Then we introduce the switch statement, which can also be used for multiway selection.

• 9.8 STYLE CONSIDERATIONS FOR REPETITION STATEMENTS– Indent the loop body of for statements– Avoid the following permitted syntax elements

of the for statement : missing initialization, update, and/or loop control expressions in the header.

– Avoid using while and for statements for the implementation of posttest repetition structures.

– Indent the loop body of do-while

Page 26: Chapter 9 Complex Selections and Repetitions. 9.1 INTRODUCTION Then we introduce the switch statement, which can also be used for multiway selection.

• 9.9 EXAMPLE PROGRAM 1: A C Program that Computes Distribution of Letter Grades