chapter 9 complex selections and repetitions. 9.1 introduction then we introduce the switch...

26
Chapter 9 Complex Selections and Repetitions

Post on 20-Dec-2015

216 views

Category:

Documents


0 download

TRANSCRIPT

Chapter 9

Complex Selections and Repetitions

• 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.

• 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)

• 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.

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

• 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

• 9.3 MULTIWAY SELECTION USING THE switch AND break STATEMENTS

switch (ControllingExpression){CaseClause-1

CaseClause-2

.

.

CaseClause-n

DefaultClause

} /* end switch */

• 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.

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 */

• 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.

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 */

• 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.

• 9.5 THE PRETEST REPETITION STRUCTURE

• The for Statement for Pretest Repetition

for (InitializationExpression; LoopControExpression;

UpdateExpression)

Loopbady

/* end for */

• 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.

int number, sum = 0 , counter = 1;

…………………….

…………………….

While (counter <= number) {

sum = sum + counter ;

counter = counter + 1;

} /* end while */

int number , sum , counter ;

……………………..

……………………..

Sum = 0;

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

sum = sum + counter;

/* end for */

• 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

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 */

• 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.

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 */

• 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.

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

• 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 */

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

• 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

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