3.1 If-else branches (general)

¡Supera tus tareas y exámenes ahora con Quizwiz!

3.1.12 If x equals -1 = Put "Disagrees" to output Else If x equals 0 = Put "Neutral" to output Else If x equals 1 = Put "Agrees" to output Else = Put "Invalid entry" to output ----------------------------------------------------------------------------- In the code above, suppose a programmer removed the "Else" part entirely. If x is 2, which is correct? 1.) The last branch, meaning the Else If x equals 1 branch, will execute. 2.) No branch will execute. 3.) The program is NOT legal.

2

3.1.12 If x equals -1 = Put "Disagrees" to output Else If x equals 0 = Put "Neutral" to output Else If x equals 1 = Put "Agrees" to output Else = Put "Invalid entry" to output In the code above, suppose a programmer, after the third branch (x equals 1), inserts a new branch: Else If x equals -1 ... When might that new branch execute? 1.) When x is -1 2.) When x is 1 3.) Never

3

3.1.12 If x equals -1 = Put "Disagrees" to output Else If x equals 0 = Put "Neutral" to output Else If x equals 1 = Put "Agrees" to output Else = Put "Invalid entry" to output If x is 1, then the output _________________.(Disagrees, Neutral, Agrees, or Invalid entry)

Agrees

A __________ is a program path taken only if an expression's value is true. Ex: A hotel may discount a price only for people over age 60. (Think of a tree)

Branch

3.1.12 If x equals -1 = Put "Disagrees" to output Else If x equals 0 = Put "Neutral" to output Else If x equals 1 = Put "Agrees" to output Else = Put "Invalid entry" to output If x is -5, then the output _________________.(Disagrees, Neutral, Agrees, Nothing is output, or Invalid entry)

Invalid entry

3.1.5 This program outputs the absolute value of the input. After getting input val, if val < 0, the branch val = -val executes. Finally, val is output. When executing, if the input val is -9, then val < 0 is true. The branch is taken, so val = -val executes, assigning val with 9. So 9 is output. If the input val were 45, will the branch be taken?

No

A restaurant host seats patrons. The host seats a party of 1 at the counter. A party of 2 is seated at a small table. Other size parties are seated at a large table. The host mentally executes the algorithm: "If party of 1, seat at counter; Else If party of 2, seat at small table; Else seat at large table." What if it was a party of 10?

Seat at large table

3.1.12 If x equals -1 = Put "Disagrees" to output Else If x equals 0 = Put "Neutral" to output Else If x equals 1 = Put "Agrees" to output Else = Put "Invalid entry" to output Could the programmer have written the three branches in the order: x equals 1, x equals 0, and x equals -1, and achieved the same results?

Yes


Conjuntos de estudio relacionados

Chapter 32: Consumer and Environmental Law

View Set

Managerial Accounting Final CH 11,13,14

View Set

Economics and Financial Literacy

View Set

Chapter 18 - Male Genital system

View Set

Chapter 15: The Respiratory System

View Set

Ch. 6 Understanding Business: Entrepreneurship and Starting a Small Business

View Set

Chapter 1 - The Principles and Practice of Economics

View Set