Concept explainers
Describe the difference between the if /else if statement and a series of if statements.
“if/else”if statement:
- “if/else” ifis a conditional statement that controls the condition branching.
- It executes the statements after evaluating the conditional expression.
- The “if” statement evaluates the test expression and when it is evaluated as true, the statement inside the body of “if” statement is executed.
- If the test expression is evaluated as false, it moves to the next if/else statement and the process continues till it reaches the finalif/else statement.
- If the test expression is evaluated as false, the statements in the “else” is executed.
Syntax:
//if conditional statement
if(condition)
{
Statement1
}
//if condition statement2
else if(condition)
{
Statement2
}
//else part
else
{
Statement3
}
Series of “if” statement:
A condition statement that controls the condition branching. The “if” statement evaluates the test expression and when it is evaluatedas true, the statement inside the body of “if” statement gets executed.
Syntax:
// if conditional statement1
if(condition)
{
Statement1
}
// if conditional statement2
if(condition)
{
Statement2
}
Explanation of Solution
Difference between if/else if statement and a series of if statement:
if/else if statement | if statement |
The condition gets tested until one conditional block is found to be true. | The conditions get validated for each of the condition cases given in a series. |
The conditions are being evaluated to true to execute the statement. On evaluating true, the statement gets execute and exits the controls. | Each series of condition needs to be evaluated for the execution of series of statement present. The control gets validated for each condition and based the valuation of condition; the control exits the condition. |
Want to see more full solutions like this?
Chapter 4 Solutions
Starting Out with C++ from Control Structures to Objects Plus MyLab Programming with Pearson eText -- Access Card Package (9th Edition)
Additional Engineering Textbook Solutions
Using MIS (10th Edition)
Introduction To Programming Using Visual Basic (11th Edition)
Starting Out With Visual Basic (7th Edition)
Problem Solving with C++ (10th Edition)
Modern Database Management
Java How To Program (Early Objects)
- Write an if-else statement that assigns 0 to the variable b if the variable a is less than 10. Otherwise, it should assign 99 to the variable b.arrow_forwardTrue or False? Every if statement must have a corresponding else.arrow_forward0 When you write an If-Then-Else statement, under what circumstances do the statementsthat appear between Else and End If execute?arrow_forward
- Multi-way selection is possible with nested if-else statements and switch statements. When is it inappropriate to use a switch statement?arrow_forward2. Write an if statement that assigns 0.3 to commission if sales is greater than or equal to 10000. C++ programming language.arrow_forward1. Write an if statement that assigns 0 to p if q is equal to 30. In C++ programming language.arrow_forward
- Write an if-else statement that assigns 20 to the variable y if the variable x is greater than100. Otherwise, it should assign 0 to the variable y.arrow_forward1. Write an if statement that assigns 0 to x if y is equal to 20. 2. Write an if statement that assigns 0.2 to commission if sales is greater than or equal to 10000. In Python Programming, please.arrow_forwardWrite an if statement that assigns 0 to the variable b and assigns 1 to the variable c if the variable a is less than 10.arrow_forward
- Using C++ programming language: 01. Write a simple if statement that multiplies the variable num1 by 2 only if the variable multBy2 is equal to true. 02. Assume you have two string variables str1 and str2. Write just the if statement that would print which string is greater. Assume they are not equal.arrow_forwardHow would these ternary operators be converted into else/if statements? Please post the code belowarrow_forwardYou must use switch statements instead of if-else statements. If-else statements will not be accepted for this problem. You want to learn to play an instrument, but you need to know how much it's going to cost to buy it. The music store has the following table on their website: Write a menu-driven program that asks the user to input an instrument category and then an instrument. The program should give the user the price. The user should input an integer in the range of the choices you give them (for example, a user cannot input 3 if you only have 2 choices) and in response should be prompted to for the next set of choices. Once they make the final selection, the total should be printed to them as an integer with proper formatting as shown in the sample run. Sample run: Select a category: (1)Brass (2)Woodwind (3)Percussion 2 Select an instrument: (1)Flute (2)Saxophone 2 Your instrument will be $425. Ensure you are doing input validation, output Please enter a valid input. if the user…arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education