Concept explainers
(Dangling-else Problem)C++ compliers always associate an else with the immediately preceding if unless told to do otherwise by the placement of braces ({and}). This behavior can lead to what is referred to as the dangling-else problem. The indentation of the nested statement
if (x > 5)
if (y > 5)
cout << “x and y are > 5”;
else
cout << “x is <=5”;
appears to indicate that if x is greater than 5, the nested if statement determines whether y is also greater than 5. If so, the statement outputs the sting “x and y are >5”. Otherwise, it appears that if x is not greater than 5, the else part of the if… else outputs the string “x is <=5”. Beware! This nested if… else statement does not execute as it appears. The complier actually interprets the statement as
if (x > 5)
if (y > 5)
cout << “x and y are >5”;
else
cout << “x is <=5”;
in which the body of the first if is a nested if… else. The outer if statement tests whether x is greater than 5. If so, execution continues by testing whether y is also greater than 5. If the second condition is true, the proper string - “x and y are > 5” - is displayed. However, if the second condition is false, the string, “x is <= 5” is displayed, even though we know that x is greater than 5. Equally bad, if the outer if statement’s condition is false, the inner if… else is skipped and nothing is displayed. For this exercise, add braces to the preceding code snippet to force the nested if… else statement to execute as it was originally intended.
Want to see the full answer?
Check out a sample textbook solutionChapter 4 Solutions
C++ How to Program (10th Edition)
- C++ Programming Program Preface (Infix to Postfix) Write a program that converts an infix expression into an equivalent postfix expression. The rules to convert an infix expression into an equivalent postfix expression are as follows: Suppose infx represents the infix expression and pfx represents the postfix expression. The rules to convert infx into pfx are as follows: a. Initialize pfx to an empty expression and also initialize the stack. b. Get the next symbol, sym, from infx. b.1. If sym is an operand, append sym to pfx. b.2. If sym is (, push sym into the stack. b.3. If sym is ), pop and append all of the symbols from the stack until the most recent left parentheses. Pop and discard the left parentheses. b.4. If sym is an operator: b.4.1. Pop and append all of the operators from the stack to pfx that are above the most recent left parentheses and have precedence greater than or equal to sym. b.4.2. Push sym onto the stack. c. After…arrow_forwardC++ Nothing too advanced please. Learning the basics #include <iostream>using namespace std;int main()arrow_forwardi need solution very very quickly please #data structure in c++# #the question below# The cafeteria in a university decided to make Black Friday, each student can take at most 3items from the cafeteria, if the student has an average greater than or equal 90, he can take all his itemsfor free and if the average between 60 and 89 the student can take 1 item only as free from his items,otherwise the student should pay for the items. before the cafeteria opens the door there are many andmany students waiting at the door, the guard decide to order the students about their averages in a priorityqueue. So the heist average comes first in the queue.Notes:- The student object consists of ID, Name, average and number of buying items between (0,3) items- The number of the students should be created randomly and must be less than 100 students- The number of buying items and the student average are created randomly.- The cafeteria has random number of items less than 200- The price of each item in…arrow_forward
- Compute the weakest precondition for each of the following assignment statements and postconditions. x = 2 * y -5; y = x + 2; {y < 2}arrow_forwardI need the answer as soon as possiblearrow_forwardOverview In this assignment, the student will write a C++ program that identifies a non-negative integer as prime, composite, or neither and provides the unique prime factorization for the composite case. When completing this assignment, the student should demonstrate mastery of the following concepts: · Decision Making (if, else) · Iteration (for, while) · Number Theory (Prime Factors) · Advanced Loop Variants Assignment Write a C++ program that prompts the user for a single integer value. After receiving the value, perform an analysis to determine if the integer is prime, composite or neither. If you are not sure what this means, you need to research the appropriate topics in number theory and consider what the algorithmic approach to determining if these properties are present would be from a programming perspective. In the event that you determine the provided integer is composite, provide a listing of the unique prime factorization for that integer as illustrated in the…arrow_forward
- - Pure Javascript in console- Use camelCase- use functions to call each case- all cases must be user inputarrow_forwardQuestion 13 In C programming, construct an expression to express the following test condition: Tests that str1 is not equal to "xyz" and the length of str1 is less than or equal to 3arrow_forwardneed unique answer onlyarrow_forward
- 58arrow_forwardanswer needed soon it's c programmingarrow_forwardEXERCISE WEEK 7 (USE C++ CODING) Based on the image attached, shows a statement of a program are in the incorrect order. Rearrange the statements so that they prompt the user to input the shape type (rectangle, circle, or cylinder) and the appropriate dimension of the shape. The program then outputs the following information about the shape: For a rectangle, it outputs the area and perimeter; for a circle, it outputs the area and circumference; and for a cylinder, it outputs the volume and surface area. After rearranging the statements, your program should be properly indented.arrow_forward
- Np Ms Office 365/Excel 2016 I NtermedComputer ScienceISBN:9781337508841Author:CareyPublisher:Cengage