Week 3 Selection Structures

# Week 3 Selection Structures - String status NumCds...

This preview shows pages 1–2. Sign up to view the full content.

Reading on Selection Str Table of Contents: Single Alternative If-Structures Double Alternative If-Structures if structure is a selection structure with only one outcome. The outcome executes only if the contains the outcome which executes when a condition evaluates t An entry point occurs where the vertical line intersects with the top of the if-structure’s diamon occurs where the vertical line intersects with the button of the if-structure’s diamo General Form of a Single Alternative If-structure o The condition goes in a diamond, without the If or Then keywords. o If the condition is true, the solution branches to the right. This is called the “True conditional branch”. o The true conditional branch contains one or more outcomes. o When finished, the true conditional branch exits the structure and proceeds to the next step. o If the condition is false, the solution ignores the branch, and leaves through the exit point Outcome in the true conditional branch ge that says, “You can apply for a government grant”. If your income is greater than or equal to \$17000, the solution displays no such message. Regard Outcome in the true conditional branch decimal taxrate decimal result get income, taxrate if income < 17000 then display eligibility message Endif result = income * taxrate display result Stop The if-structure begins on the same indentation level as the line above it This outcome has been indented to show that it belongs to the true conditional branch ment media (compact discs and DVD’s). The solution asks how many compact discs you have, the average cost of a compact disk, and whether y ing the number of CD’s by the average cost of a CD, and displays the answer. erage cost of a DVD, displaying the result of the calculation. dcalc is a Boolean, because users enter “true” if they want to do the DVD calculations, and “False” if they don’t. true conditional branch Get NumCds, CdAvgPrice, DvdCalc CdTotalValue = NumCds* CdAvgPrice Display CdTotalvalue If DvdCalc = true then Get NumDvds, DvdAvgPrice DVDtotalvalue = NumDvds* DvdAvgPrice Display DVDTotalValue End if Display “Calculations Complete” Stop re contains two outcomes. The first outcome executes if the condition is true. The second outcome executes when the condition evaluates to false. A false conditional branch contains an outcome which executes when a condition evaluates to false. General Form of a Double Alternative If-Structure Example 5.05.01 The structure starts with a diamond Inside the diamond is a condition as usual The structure has two branches – the true and false conditional branches. Each branch has its own set of flowcharting symbols (outcomes) Entry Point Exit Point r’s status is “preferred” the program displays “Price is \$8”. If the customer’s status is not preferred, the program displays “Price is \$10”.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern