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 Plus Mylab Programming With Pearson Etext -- Access Card Package (10th Edition)
- (Practice) Write C++ statements for the following: a.b=sinxcosxb.b=sin2xcos2xc.area=( cbsina)/2d.c= a 2 + b 2 e.p= | mn|f.sum= a( r n 1) r1arrow_forwardState whether each of the following is true or false. If false, explain why e) All variables must be given a type when they’re defined.arrow_forwardUse Type Switches and Asertions sparingly function to write go code. Question is right , please donot reject it Output 70< nil>arrow_forward
- State which of the following are true and which are false. If false, explain your answer.c) The statement printf("a = 5;"); is a typical example of an assignment statementarrow_forwardWrite C++ statements to accomplish each of the followingarrow_forwardState whether each of the following is true or false. If false, explain why c) The escape sequence \n when used in a printf format control string causes the cursor to position to the beginning of the next line on the screen.arrow_forward
- -Program C 2. Not my Favorites by CodeChum Admin We've already done looping through a series of numbers and printing out its squares, so how about we level it up and go with cubes this time? I have a condition though; I don't want to be associated with numbers that are divisible by 3 or 5 so if an integer is divisible by either of the two, don't make their cube values appear on the screen, please. Care to fulfill my request? Instructions: Print out the cube values of the numbers ranging from 1 to 1000 (inclusive). However, if the number is divisible by either 3 or 5, do not include them in printing and proceed with the next numbers. Tip: When skipping through special values, make use of the keyword continue and put them inside a conditional statement. Output Multiple lines containing an integer. 1 8 64 343 512 . . .arrow_forward4-write functions to write a C++ program for solving the following computational problem: a)-Find the smallest Fibonacci number greater than 1,000,000 and greater than 1,000,000,000.arrow_forwardCompute the weakest precondition for each of the following assignment statements and postconditions. x = 2 * y -5; y = x + 2; {y < 2}arrow_forward
- Finish the 2 "TODO" in the ASM Language! A palindrome is a word that is spelled the same way forwards and backwards. For example, "radar", "racecar", "civic", "kayak", and "madam" are all palindromes. The definition can be extended to phrases and sentences when ignore case and punctuation, but for this exercise we will stick to a single word. The starter code provided uses the C library functions printf and scanf to prompt for an input a word. The word that is entered from the keyboard is a null-terminated string placed in the byte array at address buf. There are also two output strings provided at addresses str_is_palindrome and str_is_not_palindrome. The starter code provided simply outputs the string at str_is_not_palindrome. The code contains two TODO comments... At the first TODO comment, the byte array at buf is filled with input from the keyboard. This input is a null-terminated character string (i.e. the array contains the characters entered on the keyboard, followed by ASCII…arrow_forwardAnswer the following sentences true or false 1. The loop execution is terminated on the basis of the test conditions. 2. A do..while loop is also called an exit-controlled loop. 3. A block of loop control statements in C are executed for number of times until the condition becomes false.arrow_forwardMake use of if-else and or switch casearrow_forward
- C++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology PtrC++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning