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++ MPL
Additional Engineering Textbook Solutions
Starting Out with Python (4th Edition)
Electric Circuits. (11th Edition)
Starting Out with C++ from Control Structures to Objects (9th Edition)
Management Information Systems: Managing The Digital Firm (16th Edition)
Starting Out with Java: From Control Structures through Objects (7th Edition) (What's New in Computer Science)
Java How to Program, Early Objects (11th Edition) (Deitel: How to Program)
- Making choices with the conditional expression LISP provides a control structure called the conditional expression that works like an if statement in C++. It tests a condition and runs either a consequent expression or an alternative expression, depending on the truth value of the condition. The entire conditional expression then returns the value of the consequent or the alternative expression. Type the following conditional expression at the interpreter's prompt: |(if (> 4 3) 4 3) What value is returned? Now we'll define a function that solves a general problem, finding the maximum of two numbers, using the following conditional expression: |(define (maximum x y) (if (> x y) y)) Test this function with several pairs of numbers.arrow_forwardBranching Statements on Solving Function My Soluti Write a MATLAB program to evaluate the function y(x) = Inx for any user-specified value of x, where x is a number. 1.0 (note that In is the natural logarithm, the logarithm to the base e). Use an if structure to verify that the value passed to the program is legal. If the value of x is legal, calculate y(x). If not, write a suitable error message and quit. Script e A Save C Reset E MATLAB Documenta 1 % Define variables: 2 % x -- First independent variable 3 % fun -- Resulting function 4 % Simulate: Prompt the user for the value x 5 disp('Enter x: '); 6 rndNum = randi([1,3]); 7 sampleNum = [-1 0 2]; 8 x = sampleNum(1,rndNum) 9 % Calculate the function use disp in showing your answer 10 11arrow_forwardc++ programming if…else and switch case are types of conditional selection statements used in many programming languages such as C++. Write your own codes for question (a), (b) and (c) to demonstrate how the selection statement works. (a) Using if….else selection statement, allow user to key in a number then check whether the number is positive or negative number. (b) Using if….else...if selection statement, allow user to key in a mark for one subject and display the grade for the subject. A for >80, B for >60, C for >50, D for >40, F for <40 .. (c) Using nested if..else selection statement, ask user to key in their age. Then define whether the user should go to nursery, primary school, secondary school or university based on their input. (d) Using switch case selection statement, allow user to key in a mark for one subject and display the grade for the subject. A for >80, B for >60, C for >50, D for >40, F for <40 .arrow_forward
- 1. Write a program in C which is a Menu-Driven Program to compute the area of the various geometrical shapes. You are required to use switch... case statement. i. Circle ii. Rectangle iii. Cylinder 2. Write a C program to read electricity unit (kW) and calculate the total electricity bill (RM) according to the given condition by using if... else if statement → For first 100 units RM 0.44/unit ⇒>> For next 100 units RM 0.51/unit For unit above 200 RM 0.65/unit →>> An additional tax (SST) of 6% is added to the bill. RM must be shows in two decimal points. => The output must have the option to choose from a menu as follow: Information of tariff charge; i. ii. Calculate a power consumption (kWh) from the keyboard and display the total bill; If the total bill calculated is less than or equal to RM20, print "Your bill is free". Otherwise, print the total bill. iii. Exit the calculator and say "Thank You", Other than the options, print "Option unavailable"arrow_forward4. Dangling else issue can be syntactically resolved by the C-compiler? Select one: a. None of the mentioned b. True c. Depends on the programmer's code d. Falsearrow_forwardIn C Language 1. Can nested if statements be used as an alternative to the logical And and Or operators? (Answer Yes or No only) 2. Assuming resident is a Boolean variable, is if (resident) the same as if(resident==true)? (Answer Yes or No only)arrow_forward
- 5. What are differences between switch statement and if-else if statement control structures? By using the aid of flowchart(s), explain the differences.arrow_forward1. What is the significance of using control structures? 2. For you, which is is preferably the most convenient control structure to be used in comparisons, IF-ELSE or SWITCH? 3. Do SWITCH and IF-ELSE have differences?arrow_forwardCase Study – Finding Factors Create a solution with functions (modules) and control structures for a program that reads a positive and non-zero integer entered by an interactive user and prints out all of its factors in ascending order. For example, when the user enters 50, the program should print 2 5 5 because 2 * 5 * 5 = 50; when the user enters 60, the program should print 2 2 3 5 because 2 * 2 * 3 * 5 = 60 when the user enters 150, the program should print 2 3 5 5 because 2 * 3 * 5 * 5 = 150 User should be able to repeat procedure as many times as they like. Determine program output(s).Provide a brief explanation for your answer.arrow_forward
- Case Study – Finding Factors Create a solution with functions (modules) and control structures for a program that reads a positive and non-zero integer entered by an interactive user and prints out all of its factors in ascending order. For example, when the user enters 50, the program should print 2 5 5 because 2 * 5 * 5 = 50; when the user enters 60, the program should print 2 2 3 5 because 2 * 2 * 3 * 5 = 60 when the user enters 150, the program should print 2 3 5 5 because 2 * 3 * 5 * 5 = 150 User should be able to repeat procedure as many times as they like. Determine the best control structures to use in the solution for reading program input(s). You must provide explanation for your answers.arrow_forwardCase Study – Finding Factors Create a solution with functions (modules) and control structures for a program that reads a positive and non-zero integer entered by an interactive user and prints out all of its factors in ascending order. For example, when the user enters 50, the program should print 2 5 5 because 2 * 5 * 5 = 50; when the user enters 60, the program should print 2 2 3 5 because 2 * 2 * 3 * 5 = 60 when the user enters 150, the program should print 2 3 5 5 because 2 * 3 * 5 * 5 = 150 User should be able to repeat procedure as many times as they like. To represent the hierarchical structure of modules, create a structure chart with data flow and conditional and loop execution. It should be noted that the hierarchical structure of modules should be designed with appropriate and manageable sized functions.arrow_forwardCase Study – Finding Factors Create a solution with functions (modules) and control structures for a program that reads a positive and non-zero integer entered by an interactive user and prints out all of its factors in ascending order. For example, when the user enters 50, the program should print 2 5 5 because 2 * 5 * 5 = 50; when the user enters 60, the program should print 2 2 3 5 because 2 * 2 * 3 * 5 = 60 when the user enters 150, the program should print 2 3 5 5 because 2 * 3 * 5 * 5 = 150 User should be able to repeat procedure as many times as they like. Create a flowchart for the module that processes the integer and prints out all of its factors in ascending order to show the process's execution flow.arrow_forward
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage LearningProgramming Logic & Design ComprehensiveComputer ScienceISBN:9781337669405Author:FARRELLPublisher:CengageC++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology Ptr
- Microsoft Visual C#Computer ScienceISBN:9781337102100Author:Joyce, Farrell.Publisher:Cengage Learning,Np Ms Office 365/Excel 2016 I NtermedComputer ScienceISBN:9781337508841Author:CareyPublisher:Cengage