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
please solve it, C++
Please answer item number 3. (pyhthon programming)
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