C++ HANLY Ch. 3 Control Structures

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

A compound statement or block

is a sequence of statements enclosed in braces. example: { statement 1 statement 2 ... statement n }

Paraphrase the following condition used to decide if a solution is mildly acidic 5.5 <= pH && pH <= 6.5

pH is between 5.5 and 6.5 (inclusive): solution is mildly acidic.

when comparing two lowercase letters, "less than" represents...

represents "earlier alphabetically."

multiple alternative if

when the nested part is only the else. we can use: if else else .... else

When applying this theorem, use the following steps to ensure the correct order of evaluation. :...

1- If the expression uses both the operator && and the operator ||, parenthesize the subexpressions that use &&. Replace each && by ||, each|| by && and negate each subexpression either by using or removing the operator ! or by replacing each relational or quality operator by its complement. e.g., > by <=, != by == etc.

3 errors using switch

1- Using a string or a floating point value(use only char or int) 2-Omission of 'break" at the end of one alternative(execution "falls through" into next alternative) 3-Forgeting the closing brace of the switch(statements following the switch become part of the default case)

In the comparison of two digit characters, the condition '0' < '1' is ....?..1... because...?..2..

1- the condition '0' < '1' is true. 2- because the lower digit has the lower character code,

Paraphrase 1- original expression and 2-its complement. Original: veteran || age >= 60 && salary < 10000 Complement: !veterans && (age < 60 || salary >= 10000)

1-The original condition is true either tor a veteran or for anyone aged 60 or more whose salary is under 10000. 2- complement is true for non-veterans who are under 60 years old or earn at least 10000.

The negation operator ! takes one operand. if the operand is false, negating it gives a...1.... result; if the operand is true, negating it gves a...2...result

1-true result 2- false result

Paraphrase the following condition used to decide if improvements should be done in a building. !buildingCodeOK || yearConstructed < 1980

There are violations to the building code or construction predates 1980.

We have seen that the ! operator can be used to complement or negate a logical expression. We can also complement simple comparisons by ...

We can also complement simple comparisons by using a different operator.

With operand ||, all comparisons are true ecxept

When both operands are false Example: operand1 false operand2 false operand1 || operand2 (read operand 1 or operand 2) is false

With operand &&, all comparisons are false except....

When both operands are true Example: operand1 true operand2 true operand1 && operand2 (read operand 1 and operand 2) is true

control structure

language construct that determines the order in which statements are executed.

pseudocode

language of structured English statements used in designing a step-by- step approach to solving a problem.

Operator < means

less than

operator <= means

less than or equal to

The value of the controlling expression of a switch may be.....and may not be......

may be a character or of an integer type but not of a floating point type (such as float or double) or of type string.

In a if: a decision that falls within a true task must be written as a ...

must be written as a nested if

!

not

The switch statement is especially useful when the selection is based on...

of a single variable or of a simple expression(the controlling expression)

||

or

The parallel if temptation

overlooking the applicability of a multiple-alternative structure that uses if-else or else-if-else-if- ... else, and choose instead a sequence of single if statements, a structure we'll call the "parallel-if." (RESIST THE TEMPTATION)

nested

placement of a control structure inside another control structure.

The 3 basic control structures are.....

sequential execution, selection, and repetition

we saw that each character is represented by an integer.code, so a comparison of two characters is a comparison of their...

so a comparison of two characters ls a comparison of their character codes.

The operator && requires two operands. A condition using && is true only if both operands are...

true

A nested if can be replaced by a multiple-alternative if when...

when new decisions fall within false tasks (on else branches). A decision that falls within a

Paraphrase the following condition used to determine whether a city would allow wood burning on a certain day: pollutionIndex < GOOD_CUTOFF && windSpeed > LIGHT_BREEZE

when pollution index is within "good" air quality range and there is some wind.

repetition statements

while statement, for statement, do_while statement:

&&

and

A condition using the operand || is false only if

both operands are false.

Precedence of Operations

----Operation Precedence 1-) function calls explicit type conversions- highest (evaluated 1st) 2-) ---- ! ----- unary+ ----- unary - 3-) binary * --- / ------- % 4-) < ---- > ------- <= ------- >= 5-) == --------- != 6-) && 7-) || 8-) (-) lowest operand (evaluated last)

What are the two cases when braces are needed in the body of an if statement?

1. When the body consists of a compound statement. 2. When you have an "if" in the body of an "if-else" statement.

GIGO

Garbage In, Garbage Out

operator > means

Greater than

Why you cannot write the expression to check whether the value of n is between 0 and 100 as o <= n <= 100?

If you write the expression as you would in math the expression will always have a value of true; because: 1- The first part of the expression, 0 <= n, evaluates either to false or true 2- (b/c of the C heritage of C++, type bool values can legally be converted to type int) SO, false is converted to 0 and true to 1.( Both values are less than 100.) 3- The same happens with the second expression n<=100 4- As a consequence of 1 and 2, both expressions will always have a value of true, which will make the total expression: 0<=n<=100 always true

while statement

In the example, as long as condition is true, statement is executed. Example: while (condition) statement

for statement

In the example, first, init is executed; then as long as condition is true, statement followed by update is repeatedly executed. Example: for (init; condition) ; update) statement

if-else statement:

In the example, if condition is true, statement 1 is executed; otherwise statements is skipped and statement 2 is executed. Example: if (condition) statement 1 else statement 2

Switch statement

In the example, value of control is compared to each label in turn; the statements following the matching label (up to break ;) are executed and all others are skipped. The statements after default : are executed only if no label matches. Example:(NOTE: TAKE ------ AS WHITE SPACES) switch (control) { case label 11 ........................ case label 1n: statements1 --------------------break; case label 21: ......................... case label 2n: statements2 ---------------------break; case label n1: ...................... case label nm: statements n ---------------------break; default: -------- statements d }

do-while statement

In the example,first, statement is executed once. Then, as long as condition is true, statement is repeatedly executed. do statement while (condition) ;

if statement

In the example,if condition is true, statement is executed; otherwise statement is skipped. Example: if (condition) statement 1;

Complement this condition veteran || age >= 60 && salary < 10000

Step1: Add parentheses: veteran || (age >= 60 && salary < 10000) Step 2: Negate subexpressions, replacing operators: !veterans && (age < 60 || salary >= 10000)

Paraphrase the following condition used to decide if temperature is comfortable for tourists: !(temperature >= 68 && temperature <= 80)

Temperature is outside tourist comfort range.

In a comparison of two capital letters,....?... has the lower code

The letter nearer the beginning of the alphabet has the lower code; Example: The condition 'A' < 'B' is true.

C++ always associate an else with...

The most recent incomplete if.

parallel-if

a sequence of single if statements

DeMorgan's Theorem and its dual are...

! (A && B) is equivalent to !A || !B ! (A || B) is equivalent to !A && !B

Nested Selection Structure Format

(NOTE ---- are white spaces cond means condition) if (cond1) 'true" task for cond1--- if (cond2) ---------------------------------statement1 ------------------------------else ----------------------------------statement2 else "false" task for cond1--- if (cond3) -----------------------------------statement3 ------------------------------- else ------------------------------------statement4 Study book page 86

how you write in C++ an expression to check whether the value of n is between 0 and 100?

0 <= n && n <= 100

Compare the repetition and selection control structures while and if

1- Both control structures have a parenthesized condition immediately following the initial reserved word if or while. 2- in while: after the first input, if the condition is false the statement that depend on the condition will be skipped , or if the condition is true, the statement will be repeated as many times as the condition remains being true. In contrast, In if-else: If the condition after if is true, the statement is executed only once, or if the condition is false, the statements after it is skipped and the statement after else is executed once. 3- While statement: allows for repetition of only one statement, to repeat more than one statement, enclose them in braces forming a single compound statement. 4- in an if: to execute multiple statements dependent on the condition , also enclose them in braces

Use 2 different expressions to complement the following condition: data < CUTOFF

Complement 1: ! (data < CUTOFF) Complement 2: data >= CUTOFF In Complement 1: apply the negation operator ! to the original condition. In Complement 2: replace the < operator by its complement, >=.

Why the parallel-if version of a code is less efficient than the use of multiple alternative structure?

b/c all conditions are tested every time the code is run. In contrast, the multiple alternative version will test only one if the 1st condition is true and if not it will test only the necessary conditions until it finds a true.

Make a program fragment that solves this: An employee is eligible for early retirement when the sum of his age and years of service is greater than 85.

bool eligible; int age, yearsService; ... if ((age + yearsService) > 85) eligible = true; else eligible = false;

DeMorgan's Theorem presents a way to complement any expression by ...

by replacing operators.

selection

conditionally choosing for execution one of several code fragments. 0ne of several alternatives is selected based on a condition.

repetition

conditionally repeating a section of code.

The relationships between the character codes for Lowercase letters, uppercase letters, and digits depend on the character set used by a particular

depend on the character set used by a particular C++ implementation.

operator != means

does not equal

Change the following fragment of a program for a shorter equivalent expression: if ((age + yearsService) > 85) eligible = true; else eligible = false;

eligible = (age + yearsService ) > 85;

operator == means

equals

short-circuit evaluation

evaluating only as much of a logical expression as is needed to determine its value.

sequential execution

executing all statements in the order in which they are listed.

condition

expression that is true or false

controlling expression (of a switch)

expression whose value is matched to a case label to select the code fragment to execute.

operator >= means

greater than or equal to

selection statements are...

if statement, if_else statement, switch statement.


Set pelajaran terkait

AP Human Geography Unit VII Cities and Urban Land Use

View Set

Reimbursement - Final Exam Review

View Set

Assessment of Respiratory Function

View Set

Frans werkwoorden être, faire, avoir en aller.

View Set

Earth/Space Sections 23.3 and 23.4

View Set

Formation of contract: offer and acceptance

View Set

Ch. 16: Nursing Care of the Child With an Alteration in Intracranial Regulation/Neurologic Disorder - ML6

View Set