Presentation is loading. Please wait.

Presentation is loading. Please wait.

Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 5: Decision Control Structures.

Similar presentations


Presentation on theme: "Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 5: Decision Control Structures."— Presentation transcript:

1 Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 5: Decision Control Structures

2 Today We Are Going To: Utilize if statements to control the flow of a program Utilize if statements to control the flow of a program Explore nested if statements Explore nested if statements Revisit boolean variables Revisit boolean variables Explore special constructs used for commonly-occuring types of decisions Explore special constructs used for commonly-occuring types of decisions

3 Topic Making Decisions

4 The Structure of a Decision You make decisions all the time You make decisions all the time Example: If the temperature is below 30° wear a heavy coat, otherwise if it is below 60° wear a sweatshirt, otherwise wear shorts Example: If the temperature is below 30° wear a heavy coat, otherwise if it is below 60° wear a sweatshirt, otherwise wear shorts Express this thought process using if statements in C++ Express this thought process using if statements in C++

5 Syntax of an if Statement if (condition1) {statement1;statement2;} else if (condition2) {statement3;statement4;} [repeat as necessary] else{statement5;statement6;}

6 Breakdown Blocks are used to group multiple actions into a single unit Blocks are used to group multiple actions into a single unit Sometimes not necessary, but aid readability Sometimes not necessary, but aid readability Only the first consequence is required; all of the else s are optional Only the first consequence is required; all of the else s are optional Conditions: parens required Conditions: parens required Conditions: Boolean expressions Conditions: Boolean expressions (Recall the set of boolean operators) (Recall the set of boolean operators)

7 Example #1 Write a program that will prompt for an input number test whether it is positive Write a program that will prompt for an input number test whether it is positive How many tests will you need? How many tests will you need? Perform them in succession Perform them in succession Mechanics: Mechanics: Create new project (Win32 Console App) Create new project (Win32 Console App) Add a source file Add a source file Create the program structure Create the program structure

8 Example #1 (cont.) Specification: Is that enough? Specification: Is that enough? Analysis Analysis Design Design Implementation Implementation

9 Example #2 Now take the program you just wrote and modify it so that it uses else blocks Now take the program you just wrote and modify it so that it uses else blocks No need to repeat the preparation; just modify the implementation No need to repeat the preparation; just modify the implementation

10 Example #3 Write another program to test an input number; this time test to see whether the number is even or odd Write another program to test an input number; this time test to see whether the number is even or odd Analysis Analysis Design Design Worth Noting: Worth Noting: I use lots of parentheses in conditions I use lots of parentheses in conditions Watch for the == problem again Watch for the == problem again

11 Example #4 Now modify that program so that it uses a boolean variable to perform the test Now modify that program so that it uses a boolean variable to perform the test Again, no need to repeat early steps; just modify the implementation Again, no need to repeat early steps; just modify the implementation

12 Example #4 (Worth noting) Worth Noting: Worth Noting: Trusted operator precedence in the assignment (usually I would not) Trusted operator precedence in the assignment (usually I would not) Still placed parentheses around isEven Still placed parentheses around isEven Prepending “is” is a common approach to creating boolean variable names Prepending “is” is a common approach to creating boolean variable names Could we have used this approach with pos/neg test? Could we have used this approach with pos/neg test?

13 Topic Nesting if Statements

14 Example #5 Write a program that will convert from a percentage grade to a letter grade Write a program that will convert from a percentage grade to a letter grade Break it down by letter grade first Break it down by letter grade first Then determine + or - Then determine + or - (Note: Try entering a decimal to show the need to filter input.) (Note: Try entering a decimal to show the need to filter input.)

15 Example #6 Convert this to an unnested if structure Convert this to an unnested if structure Is this not easier to read? Is this not easier to read? Consider: When would a nested if be required? Consider: When would a nested if be required? Example: Imagine a teacher who bases letter grades on percentage, but + and – on an extra credit project Example: Imagine a teacher who bases letter grades on percentage, but + and – on an extra credit project

16 Topic Combining Conditions

17 Example #7 Now add an input filter to ensure that the percentage lies within a valid range Now add an input filter to ensure that the percentage lies within a valid range Use logical operators to combine conditions Use logical operators to combine conditions Be generous with parentheses Be generous with parentheses Remember to enclose condition in parentheses Remember to enclose condition in parentheses

18 Topic Enumerated Possibilities

19 The switch Statement In some cases a large nested if may be simplified with a switch statement In some cases a large nested if may be simplified with a switch statement Requires that decision condition be an enumerated type: char, byte, short, or int Requires that decision condition be an enumerated type: char, byte, short, or int Condition is the enumerated value, rather than a boolean expression Condition is the enumerated value, rather than a boolean expression

20 Syntax of a switch Statement switch (enum_value) { case val_1:statement1; case val_1:statement1;statement2;break; case val_2:statement3; case val_2:statement3;statement4;break; case val_3:statement5; case val_3:statement5;statement6;break; default:statement7; default:statement7;statement8;}

21 Things to Remember If break is omitted execution will continue with the statements in the following case If break is omitted execution will continue with the statements in the following case ==> This can be useful in some cases ==> BUT it can make the switch difficult to debug The default case is optional but strongly recommended The default case is optional but strongly recommended

22 Today We Covered: The basics of if statements The basics of if statements Considered nested if statements Considered nested if statements Revisited the use of boolean variables Revisited the use of boolean variables Specialized statements used to simplify types of decisions that occur frequently Specialized statements used to simplify types of decisions that occur frequently


Download ppt "Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 5: Decision Control Structures."

Similar presentations


Ads by Google