C++ How to Program (10th Edition)
C++ How to Program (10th Edition)
10th Edition
ISBN: 9780134448237
Author: Paul J. Deitel, Harvey Deitel
Publisher: PEARSON
bartleby

Concept explainers

bartleby

Videos

Textbook Question
Book Icon
Chapter 4, Problem 4.23E

(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.

Blurred answer
Students have asked these similar questions
C++ Programming point* go_far(point* other) Return the point bearing the x- and the y- coordinates of whichever x- and y-coordinates is further from the origin. Identify which of the two points have the furthest x- and y-coordinates independently. Example: this point has (-5, 1) and the other point has (4, 9). In the x-coordinate of the two points, -5 is further from the origin than 4. In the y-coordinate of the two points, 9 is further from the origin than 1. Hence, we return a new point bearing the coordinates (-5, 9).
C++ PROGRAMProblem Statement:                 A 4th grader is having trouble with Permutation and Combination problem in Mathematics, so a friend offered help to create a program they can use. The program contains 3 options, 1st option is for Permutation, 2nd option is Combination and 3rd is for terminating the system. For option 1, Permutation, they uses the following formula: nPr =  ; where r must not be greater than n. It should not proceed if this condition is not met. For 2nd option, Combination, they uses the formula : nCr =  ; where r must not be greater than n as well. For the 3rd option, the program owner’s information shall be displayed, like name, subject code and account number, before the program terminates.   Requirements: Develop the required system and use cpp for the file name Remember that both n and r are variables, their values shall only be entered at run time. The program requires the use of any looping The program requires the use of any conditional Introduce at…
Note: C++ Programming Answer Required The annual snake festival is upon us and all the snakes of the kingdom have gathered to participate in the parade. The chef was tasked with reporting on the parade, so he decided to watch all the snakes first. When he sees the snake first, it will be his head, so he marks the "H". Snakes are long, and when they see the snake finally scurry away, they mark its tail with a "T". In the interim, when the snake is moving around it, or in the time between one snake and the next snake, it marks a '.'. Since the snakes come in a procession and one by one, a valid message would be something like "..H..T...HTH....T.", or "...", or "HT", while "T ...H..H.T", "H..T..H", "H..H..T..T" would be invalid messages (see explanation below). Formally, a snake is represented by an 'H' followed by some (possibly null) '.' and then 'T'. A valid message is one that begins with the (probably zero-length) string '.' and then some (possibly zero) snakes, with some '.'s in…

Chapter 4 Solutions

C++ How to Program (10th Edition)

Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Np Ms Office 365/Excel 2016 I Ntermed
Computer Science
ISBN:9781337508841
Author:Carey
Publisher:Cengage
Literals in Java Programming; Author: Sudhakar Atchala;https://www.youtube.com/watch?v=PuEU4S4B7JQ;License: Standard YouTube License, CC-BY
Type of literals in Python | Python Tutorial -6; Author: Lovejot Bhardwaj;https://www.youtube.com/watch?v=bwer3E9hj8Q;License: Standard Youtube License