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.
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 ratings0% 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.
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.
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.
• 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.
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.
used to form more complex conditions by combining simple conditions. • The logical operators are && (logical AND), | | (logical OR) and ! (logical NOT).
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.
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.
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.
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.
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);
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.