Concept explainers
Sometimes either a switch statement or an if /else if statement can be used to implement logic that requires branching to different blocks of program code. But the two arc not interchangeable.
A) Under what circumstances would an if/else if statement be a more appropriate choice than a switch statement?
B) Under what circumstances would a switch statement be a more appropriate choice than an if/else If statement?
C) Under what circumstances would a set of nested if/else statements he more appropriate than either of the other two structures?
Try to come up with at least one example case for each of the three, where it is the best way to implement the desired branching logic.
Learn your wayIncludes step-by-step video
Chapter 4 Solutions
Starting Out With C++, Early Objects - With Access Package
Additional Engineering Textbook Solutions
Starting Out with Python (3rd Edition)
Introduction To Programming Using Visual Basic (11th Edition)
Problem Solving with C++ (10th Edition)
Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)
Starting Out with Java: From Control Structures through Data Structures (3rd Edition)
Concepts Of Programming Languages
- PROGRAM C According to the decisions taken during the pandemic process, the curfews are as follows:People over the age of 60 cannot go out on the weekend.People under the age of 20 can go out after 15:00 on the weekend.People between the ages of 20-60 cannot go out after 21:00 on weekdays.Using the if-else structure, it will print the appropriate warning on the screen when age information is entered from the keyboard, and it will also give you special permission when 00 age information is entered on the keyboard. Write a C program to print "You can go out whenever you want!"arrow_forward(Data processing) Years that are evenly divisible by 400 or are evenly divisible by 4 but not by 100 are leap years. For example, because 1600 is evenly divisible by 400, 1600 was a leap year. Similarly, because 1988 is evenly divisible by 4 but not by 100, it was also a leap year. Using this information, write a C++ program that accepts the year as user input, determines whether the year is a leap year, and displays a message telling the user whether the entered year is or is not a leap year.arrow_forwardQ: Compare if else and switch case. Explain these statements with its general syntaxes and flow charts.arrow_forward
- 1. Write an if statement that assigns 0 to p if q is equal to 30. In C++ programming language.arrow_forwardWrite multiple if statements: If car_year is before 1967, print "Probably has few safety features." (without quotes). If after 1971, print "Probably has seat belts.". If after 1991, print "Probably has electronic stability control.". If after 2002, print "Probably has tire-pressure monitor.". End each phrase with period and newline. Remember that print() automatically adds a newline. Ex: car_year = 1995 prints:arrow_forward7. The rules of writing 'if-then-else', 'case-switch', 'while-until' and 'for' control flow statements are called _____ . a. Comments b. Functions c. Line length and wrapping d. Control Structurearrow_forward
- 3. Write an if statement that assigns 0.4 to commission if sales is greater than or equal to 10000. In C++ language.arrow_forwardConvert each if-else statement to a single assignment statement using a conditional expression, using parentheses around the condition. Enter "Not possible" if appropriate. 1) if x < 100: y = 0 else: y = xarrow_forward1. An if selection statement executes if and only if: A. the Boolean condition evaluates to false. B. the Boolean condition evaluates to true. C. the Boolean condition is short-circuited. D. none of the above. Answer: 2.A compound statement is enclosed between: A. [ ] B. { } C. ( ) D. < > Answer: 4. The controlling expression for a switch statement includes all of the following types except: A. char B. int C. byte D. double Answer:arrow_forward
- In c program Write an interactive program that contains an if statement that may be used to compute the area of a square ( area=side2) or a circle ( area=p*radius2) after prompting the user to type the first character of the figure name ( S or C).arrow_forwardObjective: Learn about repetition control structures Use the while, do-while, and for loop statements Apply break and continue Construct and use a counter-controlled, sentinel-controlled and flag-controlled repetition structures Differentiate the various iteration statements from each other and Implement nested control structures. Problem: Write a c Program that will print the number series vertically on screen: 10 1 9 2 8 3 7 4 6 5arrow_forwardLanguage: C++ (Apartment problem) A real estate office handles , say, 50 apartment units. When the rent is, say, $600 per month, all the units are occupied. However, for each, say $40 increase in rent, one unit becomes vacant. Moreover, each occupied unit requires an average of $27 per month for maintenance. How many units should be rented to maximize the profit? Write a program that prompts the user to enter: The total number of units. The rent to occupy all the units. The increase in rent that results in a vacant unit. Amount to maintain rented unit. The program then outputs the number of units to be rented to maximize the profit. Note: Please include the pseudocodearrow_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