0% found this document useful (0 votes)
3 views23 pages

Week9 Lecturenote

Chapter 4 discusses C program control structures, focusing on the do...while iteration statement, which ensures the loop body executes at least once. It also covers the break and continue statements for altering control flow, as well as logical operators for combining conditions. Additionally, it highlights the importance of distinguishing between equality (==) and assignment (=) operators to avoid common programming errors.

Uploaded by

baranyardim3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views23 pages

Week9 Lecturenote

Chapter 4 discusses C program control structures, focusing on the do...while iteration statement, which ensures the loop body executes at least once. It also covers the break and continue statements for altering control flow, as well as logical operators for combining conditions. Additionally, it highlights the importance of distinguishing between equality (==) and assignment (=) operators to avoid common programming errors.

Uploaded by

baranyardim3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Chapter 4

C Program Control
Part II

Lecture#8

© 2016 Pearson Education, Ltd. All rights reserved. 1


© 2016 Pearson Education, Ltd. All rights reserved. 2
4.8 do…while Iteration Statement

• The do…while iteration statement is similarto


the while statement.
– In the while statement, the loop-continuation
condition is tested at the beginning of the loop
before the body of the loop is performed.
– The do…while statement tests the loop-
continuation condition after the loop bodyis
performed.
• Therefore, the loop body will be executed at
least once.

© 2016 Pearson Education, Ltd. All rights reserved. 3


4.8 do…while Iteration Statement (Cont.)

• A do…while with no braces aroundthe


single-statement body appears as
do
statement
while (condition);
• which can be confusing.
• The last line—while(condition);—may be
misinterpreted as a while statement
containing an empty statement.

© 2016 Pearson Education, Ltd. All rights reserved. 4


4.8 do…while Iteration Statement (Cont.)

• Figure 4.9 uses a do…while statementto


print the numbers from 1 to 10.
• The control variable counter is
preincremented in the loop-continuation
test.

© 2016 Pearson Education, Ltd. All rights reserved. 5


© 2016 Pearson Education, Ltd. All rights reserved. 6
4.8 do…while Iteration Statement (Cont.)

do…while Statement Flowchart


• Figure 4.10 shows the do…while statement
flowchart, which makes it clear that the
loop-continuation condition does not
execute until after the action is performed
at least once.

© 2016 Pearson Education, Ltd. All rights reserved. 7


© 2016 Pearson Education, Ltd. All rights reserved. 8
4.9 break and continue Statements
• The break and continue statements are used
to alter the flow of control.
break Statement
• The break statement, when executed in a
while, for , do…while or switch statement,
causes an immediate exit from that statement.
• Program execution continues with the next
statement.
• Common uses of the break statement areto
escape early from a loop or to skip the
remainder of a switch statement.
© 2016 Pearson Education, Ltd. All rights reserved. 9
4.9 break and continue Statements
(Cont.)

• Figure 4.11 demonstrates the break


statement in a f o r iteration statement.
• When the i f statement detects that x has
become 5, break is executed.
• This terminates the f o r statement, and the
program continues with the p r i n t f after
the f o r .
• The loop fully executes only four times.

© 2016 Pearson Education, Ltd. All rights reserved. 1


0
© 2016 Pearson Education, Ltd. All rights reserved. 11
4.9 break and continue Statements
(Cont.)
continue Statement
• The continue statement, when executed in a
while, f o r or do…while statement, skips the
remaining statements in the body of that
control statement and performs the next
iteration of the loop.
• Figure 4.12 uses the continue statementin a
f o r statement to skip the p r i n t f statement
and begin the next iteration of the loop.

© 2016 Pearson Education, Ltd. All rights reserved. 12


© 2016 Pearson Education, Ltd. All rights reserved. 13
4.10 Logical Operators

• C provides logical operators that may be


used to form more complex conditions by
combining simple conditions.
• The logical operators are && (logical AND),
| | (logical OR) and ! (logical NOT).

© 2016 Pearson Education, Ltd. All rights reserved. 1


4
4.10 Logical Operators (Cont.)
Logical AND (&&) Operator
• Suppose we wish to ensure that two conditions are both
true before we choose a certain path of execution.
• In this case, we can use the logical operator && as
follows:
i f (gender == 1 && age >= 65)
++seniorFemales;
• This i f statement contains two simple conditions.
– The condition gender == 1might be evaluated, for example, to
determine if a person is a female.
– The condition age >= 65 is evaluated to determine whethera
person is a senior citizen.

© 2016 Pearson Education, Ltd. All rights reserved. 15


4.10 Logical Operators (Cont.)

• The i f statement then considers the


combined condition
gender == 1 && age >= 65
Which is true if and only if both of thesimple
conditions are true.
• Finally, if this combined condition is true,
then the count of seniorFemales is
incremented by 1.

© 2016 Pearson Education, Ltd. All rights reserved. 1


6
4.10 Logical Operators (Cont.)
Logical OR ( | | ) Operator
• Now let’s consider the | | (logical OR) operator.
• Suppose we wish to ensure at some point in a program
that either or both of two conditions are true before we
choose a certain path of execution.
• In this case, we use the | | operator as in the following
program segment
i f ( semeste rAverage >= 90 | | f i nalExam >= 90)
printf("Student grade i s A");
• The condition semesterAverage >= 90 is evaluated to
determine whether the student deserves an “A”in the
course because of a solid performance throughout the
semester.

© 2016 Pearson Education, Ltd. All rights reserved. 17


4.10 Logical Operators (Cont.)
Logical Negation ( ! ) Operator
• C provides ! (logical negation) to enable you to “reverse” the
meaning of a condition.
• The logical negation operator has only a single condition as an
operand (and is therefore a unary operator).
• Placed before a condition when we’re interested in choosing a path
of execution if the original condition (without the logical negation
operator) is false, such as in the following program segment:
i f (!(grade == sentinelValue))
printf("The next grade i s %f\n", grade);
• The parentheses around the condition grade ==sentinelValue
are needed because the logical negation operator has a higher
precedence than the equality operator.

© 2016 Pearson Education, Ltd. All rights reserved. 1


8
4.10 Logical Operators (Cont.)

• In most cases, you can avoid using logical


negation by expressing the condition
differently with an appropriate relational
operator.
• For example, the preceding statement may
also be written as follows:
i f (grade != sentinelValue)
printf("The next grade i s %f\n", grade);

© 2016 Pearson Education, Ltd. All rights reserved. 1


9
4.11 Confusing Equality (==) and
Assignment (=) Operators (Cont.)
• For example, suppose we intend to write
i f (payCode == 4 )
printf(“%s“, "You get a bonus!");
but we accidentally write
i f (payCode = 4 )
printf(“%s“, "You get a bonus!");
• The first i f statement properly awards a bonus
to the person whose paycode is equal to 4.
• The second i f statement—the one with the
error—evaluates the assignment expression in
the i f condition.

© 2016 Pearson Education, Ltd. All rights reserved. 20


4.12 Structured Programming Summary

• Figure 4.17 summarizes the control statements


discussed in Chapters 3 and 4.
• Small circles are used in the figure to indicate the single
entry point and the single exit point of each statement.

© 2016 Pearson Education, Ltd. All rights reserved. 21


© 2016 Pearson Education, Ltd. All rights reserved. 22
© 2016 Pearson Education, Ltd. All rights reserved. 23

You might also like