Chapter (4)
Chapter (4)
Objectives
In this chapter, you will:
Learn about control structures Examine relational and logical operators Explore how to form and evaluate logical (Boolean) expressions Discover how to use the selection control structures if, if...else, and switch in a program
Objectives (contd.)
Learn how to avoid bugs by avoiding partially understood concepts Learn to use the assert function to terminate a program
Control Structures
A computer can proceed:
In sequence Selectively (branch): making a choice Repetitively (iteratively): looping By calling a function
Relational Operators
Conditional statements: only executed if certain conditions are met Condition: represented by a logical (Boolean) expression that evaluates to a logical (Boolean) value of true or false Relational operators:
Allow comparisons Require two operands (binary) Evaluate to true or false
Comparing Characters
Expression of char values with relational operators
Result depends on machines collating sequence ASCII character set
10
11
12
13
14
15
16
17
18
Order of Precedence
Relational and logical operators are evaluated from left to right
The associativity is left to right
19
20
21
22
23
Can use the int data type to manipulate logical (Boolean) expressions
24
25
26
One-Way Selection
One-way selection syntax:
Statement is executed if the value of the expression is true Statement is bypassed if the value is false; program goes to the next statement Expression is called a decision maker
27
28
Two-Way Selection
Two-way selection syntax:
29
30
32
33
34
35
36
Short-Circuit Evaluation
Short-circuit evaluation: evaluation of a logical expression stops as soon as the value of the expression is known Example:
(age >= 21) || ( x == 5) (grade == 'A') && (x >= 7) //Line 1 //Line 2
37
38
39
num = 20
40
41
Can use if statements to check status of input stream If stream enters the fail state, include instructions that stop program execution
42
43
44
Insert a blank line between statements that are naturally separate Two commonly used styles for placing braces
On a line by themselves Or left brace is placed after the expression, and the right brace is on a line by itself
45
46
switch Structures
switch structure: alternate to if-else switch (integral) expression is evaluated first Value of the expression determines which corresponding action is taken Expression is sometimes called the selector
47
48
50
51
assert function: useful in stopping program execution when certain elusive errors occur
52
If expression evaluates to true, the next statement executes If expression evaluates to false, the program terminates and indicates where in the program the error occurred To use assert, include cassert header file
53
54
Summary
Control structures alter normal control flow Most common control structures are selection and repetition Relational operators: ==, <, <=, >, >=, != Logical expressions evaluate to 1 (true) or 0 (false) Logical operators: ! (not), && (and), || (or)
55
Summary (contd.)
Two selection structures: one-way selection and twoway selection The expression in an if or if...else structure is usually a logical expression No stand-alone else statement in C++
Every else has a related if
A sequence of statements enclosed between braces, { and }, is called a compound statement or block of statements
56
Summary (contd.)
Using assignment in place of the equality operator creates a semantic error switch structure handles multiway selection break statement ends switch statement Use assert to terminate a program if certain conditions are not met
57