C++ How To Program Plus Mylab Programming With Pearson Etext -- Access Card Package (10th Edition)
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
-Convert SES variable to a factor and assign the value labels “low”, ”middle” and “high” to the 3 levels of thevariable. Then -Define the variables complic, comorb, depressi, diabetes as factors. For all these factors, a zero means “no” and a 1 means “yes”. Assign these value labels through the function factor() and check the change using str()
State the order of evaluation of the operators in each of the following C statements andshow the value of x after each statement is performed.a) x = 7 + 3 * 6 / 2 - 1;b) x = 2 % 2 + 2 * 2 - 2 / 2;c) x = ( 3 * 9 * ( 3 + ( 9 * 3 / ( 3 ) ) ) );
State whether each of the following is true or false. If false, explain why g) Definitions can appear anywhere in the body of a function.

Chapter 4 Solutions

C++ How To Program Plus Mylab Programming With Pearson Etext -- Access Card Package (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
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
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