C++ Chapter 4: Selection Structures

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

switch structure

Selection structure for multiple selection. Chooses among several cases based on the value of a certain character or expression

conditional operator

a ternary operator written as ?: ; the three arguments explain what the condition is, what the result will be if the condition is true, and what the result will be if the condition is false

How to disable assert statements in a program

add the preprocessor directive: #define NDEBUG

Associativity of most operators

left to right

<

less than

<=

less than or equal to

!

not

!=

not equal to

compound statement

one or more statements enclosed between curly braces. Treated as a single statement.

||

or

Associativity of assignment operators

right to left

(statement1 || statement2) is false only when...

statement1 and statement2 are BOTH false. If either is true, whole expression becomes true.

(statement1 && statement2) is true only when...

statement1 and statement2 are BOTH true. If either is false, whole expression becomes false.

loop

structure that causes a series of instructions to be repeated until a given condition is false.

syntax of switch structure

switch (expression) { case value1: ----statements1 ----break; case value2: ----statements2 ----break; . . . case valueN: ----statementsN ----break; default: ----statements

decision maker

the condition in an if statement. If it is true, the body of the if statement will execute.

decision maker

the expression in an if statement which determines whether to execute the statement that follows it

selector

the expression used in a switch statement that determines which case will be executed

associativity

the order in which operators are grouped and evaluated

pairing an else with an if

the rule stating that an else statement is associated with the most recent incomplete if statement

action statement

the statement following the expression in an if statement. Accomplishes a task or completes a process

Evaluate: !(!true)

true

Evaluate: !false

true

When converting between int and bool, all nonzero values are treated as...

true

logical (Boolean) values

true and false

Logical (Boolean) operators

used to combine logical expressions

nested

when one control statement is located within another

short-circuit evaluation

a process in which the computer evaluates a logical expression from left to right and stops as soon as the value of the expression is known

switch structure

a selection structure that does not require the evaluation of a logical expression

Order of Logical Operations

! && ||

Logical operators in C++

&& and || or ! not

How does C++ compare characters?

By comparing their values in the collating sequence

Correct logical operation to check if n is between 0 and 10 inclusive

0 <= n && n <=10

Evaluate: !(!36)

1

Order of Operations (by symbol)

1) () 2) !, +, - (unary) 3) *, /, % 4) +, - 5) <, <=, >, >= 6) ==, != 7) && 8) || 9) =, +=, -=, *=, /=

Order of Operations (by operator type)

1) Parentheses 2) Unary operators 3) Arithmetic operators 4) Relational operators 5) Logical operators 6) Assignment operators

Two most common control structures

1) Selection 2) Repetition

4 ways a computer can process a program

1) Sequentially (in order) 2) Selectively (making a choice) 3) Repetitively (executing a loop) 4) Calling a function

Two selection structures

1) if statement 2) switch structure

The simple data types in C++

1) integers (int, bool,...) 2) floating-point numbers (double, float,...) 3) characters (char)

Rules for execution of switch statement

1) selector is evaluated and its value compared to each case value. 2) if a match is found, the statements following that case and all successive cases are executed until the program finds a break statement 3) if no match is found, the default statements, if any, are executed.

2 uses of parentheses

1) to clarify the order of operations 2) to override the precedence of operators

Difference between = and ==

= is the assignment operator. It evaluates the expression on the right and assigns that value to the variable on the left. == is the equality operator. It evaluates the expressions on either side and then checks if the two expressions have the same value (are equal)

Relational operators in C++

== equal to != not equal to < less than <= less than or equal to > greater than >= greater than or equal to

conditional operator

?: Performs the same action as an if...else statement

branch

A choice made in a program

Logical expression

An expression that evaluates to "true" or "false".

pseudocode

An informal mixture of C++ and English that is used to plan the algorithm for a program before actually implementing code.

Block

Another name for a compound statement

Associativity

Direction in which an operator is evaluated (left-right or right-left)

True or false: "Hello" > "Hen"

False. First two characters the same, but then "n" > "l".

True or false: "Bill" >= "Billy"

False. Since all characters are the same, but "Billy" has an extra character, "Billy" is greater than "Bill"

Why are if-else statements more efficient than parallel if statements?

In an if-else series, the program stops checking the conditions as soon as it finds the correct value. In parallel if, all conditions must be checked every time the program is run, making it much slower.

How does C++ compare strings?

It compares strings character by character, comparing the collating sequences of each pair of characters.

Can "else" stand alone?

No. Every "else" must always be paired with a corresponding "if"

Short-circuit evaluation

Process in which the computer evaluates a logical expression from left to right and stops as soon as the final value of the expression is known.

return statement

Returns the value following it, and immediately exits the function where it appears.

Control structures

Statements in a program that alter the normal flow of control. Used to implement selection and repetition.

selector

The expression in a switch structure whose value determines which case is executed.

Where should #define NDEBUG be placed in a program?

This preprocessor directive must be placed before #insert <cassert>

True or false: "Hello" < "Hi"

True. First character is the same, but then "i" is greater than "e".

True or False: C++ allows conversion from bool to int and vice versa.

True. values of "true" are converted to a value of 1, and values of "false" are converted to a value of 0.

How to test for input failure

Use the input stream variable (usually "cin") as the condition in a while loop. If the input succeeded, then the variable evaluates to true. If not,it evaluates to false.

input failure

When a program attempts to enter an illegal value into a variable, the input stream enters a fail state, where the program disregards all subsequent input statements

How to pair else's with if's

When an "else" is written, it is always associated with the most recent incomplete if. Example: if (n > 10) ----if (n < 20) -------n += 10; ----else -------n -= 10; Here, the "else is paired with the second if, not the first one.

logical expression

an expression that has a value of either true or false

conditional expression

an expression that uses a conditional operator

pseudocode (pseudo)

an informal mixture of C++ and ordinary language used to design an outline of a logical solution to a problem

relational operator

an operator that is used to make comparisons in a program

ternary operator

an operator that takes three arguments

&&

and

block of statements

another name for a compound statement

Boolean expression

another name for a logical expression

syntax of assert function

assert (expression); if expression is true, the program continues. If expression is false, the program terminates with an appropriate error message.

Each of the relational operators is a ______ operator

binary

Syntax for conditional operator

condition ? expression1 : expression2 If condition is true, expression1 will execute. Else, expression2 will execute.

compound statement

consists of a sequence of statements enclosed in curly braces, { and }

if...else statement

control structure used to implement two-way selection

==

equal to

Evaluate: !(!false)

false

assert function

function that terminates program if a given condition is not true.

>

greater than

>=

greater than or equal to

#include <cassert>

header file required to use the assert function in C++

syntax of if...else statement

if (expression) -----statement1 else -----statement2


संबंधित स्टडी सेट्स

Psychology 312 Quizzes for chapter 8-9-10

View Set

CHEM 1311H Ultimate Final Exam Review

View Set

CNA/RN Important Conversions + other key information

View Set

Exam #4 - Med Surg 3 - Ch 8 Concepts of Emergency and Trauma Nursing

View Set