Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 5: Decision Control Structures
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
Topic Making Decisions
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++
Syntax of an if Statement if (condition1) {statement1;statement2;} else if (condition2) {statement3;statement4;} [repeat as necessary] else{statement5;statement6;}
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)
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
Example #1 (cont.) Specification: Is that enough? Specification: Is that enough? Analysis Analysis Design Design Implementation Implementation
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
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
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
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?
Topic Nesting if Statements
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.)
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
Topic Combining Conditions
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
Topic Enumerated Possibilities
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
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;}
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
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