Docsity

Conditional Statements and Loops in C++, Exercises of Nursing

Toggle sidebar
bg1
Programming with C++ Exam 2 Review
2025
Relational operators allow you to ________ numbers. - compare
All of the relational operators are binary.
True or False? - True
The ________ statement causes other program statements to execute only under
certain conditions. - if
To check if a variable has a particular value, use the = relational operator, as in the
statement
if (s = 3)
cout << "S has the value 3";
True or False? - False
A pair of characters or a pair of string objects can be compared with any of the
relational operators.
True or False? - True
In C++ when a relational expression is false, it has the value - 0.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Conditional Statements and Loops in C++ and more Exercises Nursing in PDF only on Docsity!

Relational operators allow you to ________ numbers. - compare All of the relational operators are binary. True or False? - True The ________ statement causes other program statements to execute only under certain conditions. - if To check if a variable has a particular value, use the = relational operator, as in the statement if (s = 3) cout << "S has the value 3"; True or False? - False A pair of characters or a pair of string objects can be compared with any of the relational operators. True or False? - True In C++ when a relational expression is false, it has the value - 0.

A(n) ________ is a variable, usually a bool, that signals when a condition exists. - flag The ________ statement executes one block of statements if a test condition is true, and another block if the condition is false. - if/else A trailing else placed at the end of an if/else if statement provides a default action when ________ of the if conditions is/are true. - none When an if statement is placed within the conditionally-executed code of another if statement, this is known as a(n) - nested if The rule for matching an else with an if is that an else goes with the last if statement before it that doesn't have its own else. True or False? - True The C++ ________ operator represents logical AND. - &&

The statement pass = (score >= 7)? true : false; does exactly the same thing as the if/else statement below: if (score >= 7) pass = true; else pass = false; True or False? - True If a switch statement has no ________ statements, the program "falls through" all of the statements below the one with the matching case expression. - break Which of the following correctly declares an enumerated data type named student? - enum student { Bill, Tom, Mary }; The expression x < y is called a(n) ________ expression. - relational

The ________ operator is used in C++ to test for equality. - == Relational expressions and logical expressions are both Boolean, which means they evaluate to true or false. True or False? - True Relational operators connect two or more relational expressions into one, or reverse the logic of an expression. True or False? - False The ________ statement executes one statement, or block of statements, if a condition is true and skips it, doing nothing, if the condition is false. - if The ________ statement acts like a chain of if statements. Each performs its test, one after the other, until one of them is found to be true or until the construct is exited without any test ever evaluating to true. - if/else if An expression in a C++ if statement that evaluates to 5, -5, or for that matter anything other than 0, is considered true.

s1 = "cat" and s2 = "cat ". None of these because in each case one or more characters in the strings have different ASCII codes. - None of these because in each case one or more characters in the strings have different ASCII codes. The ________ operator is known as the logical OR operator. - || The following C++ test checks if the variable child is in the range 3 - 12. if (child >= 3 && <= 12) True or False? - False If the sub-expression on the left side of an && operator is true, the expression on the right side will not be checked. True or False? - False What will the following expression evaluate to? !( 6 > 7 || 3 == 4) True or False? - true

Assuming moreData is a Boolean variable, the following two tests are logically equivalent. if (moreData == true) if (moreData) True or False? - True A switch statement branches to a particular block of code depending on the value of a numeric (i.e. integer or floating-point) variable or constant. True or False? - False The default section of a switch statement performs a similar task as the ________ portion of an if/else if statement. - trailing else The scope of a variable is the program it is defined in. True or False? - False What will the following statement do if x equals 17 and answer = 20? answer = x > 100? 0 : 1; - Assign 1 to answer.

The while loop has two important parts: a condition that is tested and a statement or block of statements that is - repeated as long as the condition is true. The block of code in the body of a while statement can contain an unlimited number of statements, provided they are enclosed in a set of braces. True or False? - True A while loop may have a semicolon after the test expression and before the body of the loop, but it is not required. True or False? - False A while loop is somewhat limited, because the counter can only count up, not down. True or False? - False The while loop is a(n) ________ loop, whereas the do-while loop is a(n) ________ loop. - pretest, post test The statements in the body of a do-while loop are executed - at least once.

A sentinel is a special value that - marks the end of a list of values. A for statement contains three expressions: initialization, test, and - update. In a for statement, the ________ expression is executed only once. - initialization An initialization expression may be omitted from the for loop if no initialization is required. True or False? - True You may define a(n) ________ in the initialization expression of a for loop. - variable When a loop is nested inside another loop, the outer loop goes through all its iterations for each iteration of the inner loop. True or False? - False A variable that keeps a running total of data values is called a(n) - accumulator

What will the following code print? num = 5; cout << num++ << " "; cout << num-- << " "; cout << --num; - 5 6 4 The while loop has two important parts: a condition that is tested and a statement or block of statements that is executed - as long as the condition is true. If nothing within a while loop ever causes the condition to become false, a(n) ________ may occur. - infinite loop A while loop is somewhat limited because the counter can only be incremented or decremented by one each time through the loop. True or False? - False A(n) ________ is a variable that controls the number of times a loop iterates. - loop control variable

A(n) ________ is a variable that is regularly incremented or decremented each time a loop iterates. - counter When a loop is nested inside another loop, the inner loop goes through all its iterations for each iteration of the outer loop. True or False? - True You can nest a for loop inside another for loop, but cannot nest a while loop inside another while loop or a do-while loop inside another do-while loop. True or False? - False Before beginning to add value to it, an accumulator should be initialized to 1. True or False? - False The ________ statement causes a loop to terminate early. - break If a while loop has no braces around the body of the loop - the loop body contains just one statement.

A(n) _________ is a sum of numbers that accumulates with each iteration of a loop. - running total