This study guide covers decision making and conditional statements in programming. It discusses relational operators for comparing values, if and if/else statements for conditional execution, logical operators, validating user input, switch statements, and more. Techniques covered include checking numeric ranges, comparing characters and strings, implementing menus, and using blocks to group statements.
This study guide covers decision making and conditional statements in programming. It discusses relational operators for comparing values, if and if/else statements for conditional execution, logical operators, validating user input, switch statements, and more. Techniques covered include checking numeric ranges, comparing characters and strings, implementing menus, and using blocks to group statements.
This study guide covers decision making and conditional statements in programming. It discusses relational operators for comparing values, if and if/else statements for conditional execution, logical operators, validating user input, switch statements, and more. Techniques covered include checking numeric ranges, comparing characters and strings, implementing menus, and using blocks to group statements.
We use relational operators to compare numbers to determine relative order
There are the following operators: o > means greater than o < means lesser than o >= means greater than or equal to o <- means less than or equal to o == means equal to o != means not equal to Relational Expressions o There are two boolean expressions: true or false Example: 12 > 5 is true 7<= 5 is false If x is 10, then x == 10 is true x !=8 is true x == 8 is false o You can assign relational expressions to a variable For example: result = x <= y; o If you use relational expressions, assigns 0 for false, 1 for true o Remember not to confuse = (which is assigning) and == (which is equating) The If statement The if statement The if statement allows to be conditionally executed or over It models the way we mentally evaluation situations, for example o If it is raining, take an umbrella o If it is cold outside, wear a coat You use the general format of o If (expression) Statement; What Happens in an If Statement To evaluate an if statement, o If the expression is true (in the example above), then the statement is executed o If the expression is false (in the example above), then the statement is skipped over Note Notes on the If Statement Remember to not please the ; semicolon after the (expression) Please the statement; on a separate line after the expression, indented Be careful testing floats and doubles for equality (you would use an epsilon for that) 0 is considered false; any other value is true Expanding the if statement Expanding the if statement To execute multiple statements in an if statement, enclose them in curly brackets {} The curly brackets {} create a block of code If if/else statement The if/else statement Provides two possible paths of execution Performs one statement or block if the expression is true, otherwise performs the else statement or block Nested if Statements An if statement that is nested inside another if statement Nested if statements can be used to test more than one condition The if/else if statement The if/else if statement Tests a series of conditions until one is found to be true Often simpler than using nested if/else statements Follows the format: o if (expression) statement1; // or block o else if (expression) statement2; // or block . . // other else ifs . o else if (expression) statementn; // or block Flags IDK WHAT A FLAG IS Flags Variable that signals a condition Usually implemented as a bool variable Can also be an integer o The value 0 is considered false o Any nonzero value is considered true As with other variables in functions, it must be assigned an initial value before it is use Logical Operators Logical Operators Used to create relational expressions from other relational expressions Operators, meaning, and explanation: o && AND : new relational expression is true if both the expressions are true o || OR : new relational expression is true if either expression is true o ! NOT: reverse the value of an expression – true expression becomes false, and false becomes true Logical Operator Notes ! has highest precedence, followed by && then || Short circuit evaluation: If the value of an expression can be determined by evaluating just the expression on the left side of a logical operator, then the sub-expression on the right side will not be evaluated o For example: (FALSE STAMENT && SOMETHING ELSE) Will automatically go to false because it don’t matter what that something else is, you know what I’m saying? Checking Numeric Ranges with Logical Operators Checking Numeric Ranges with Logical Operators Used to test to see if a value falls inside a range o if (grade >= 0 && grade <= 100) cout << "Valid grade"; Can also test to see if a value falls outside of a range o if (grade <= 0 || grade >= 100) cout << "Invalid grade"; Cannot use mathematical notation: o if (0 <= grade <= 100) //doesn’t work! Menus Menus Menu driven program: a program execution controlled by user selecting from a list of actions Menu: list of choices on the screen Menus can be implemented using if/else if statements Menu-Driven Program Organization Displays list of numbered of lettered choices for actions Prompts user to make a selection Test user selection in expression o If a match, then execute code for action o If not, then go to next expression Validating User Input Validating User Input Input validation: inspecting input data to determine whether it is acceptable Bad output will be produced from bad input, that’s why you should generally include code that will fix or prompt the user to redo their input so it is something that the program can handle Can perform tests such as o Range o Reasonableness o Valid menu choice o Divide by zero Comparing Characters and Strings Characters are compared using their ASCII values ‘A’ is less than ‘B’ o Because the ASCII value of ‘A’, which is 65, is less than the value of the ASCII value of ‘B’ which is 66 ‘1’ is less than ‘2’ o Because the ASCII value of ‘1’ is 49, which is less than the ASCI value of ‘2’ which is 50 Lowercase letters have higher ASCII codes than uppercase letters, so ‘a’ > ‘Z’ Comparing string objects Like characters, strings are compared using their ASCII values The conditional operator The conditional operator Can use to create short if/else statements Usually you want to use these for assignment statements Format: expr ? expr : expr; o First expression is the expression to be tested o Second expression is executes if the first expression is true o Third expression executes if the first expression is false The conditional operator The value of a conditional expression is o The value of the second expression if the first expression is true o The value of the third expression if the first expression is false Parentheses () may be needed in an expression due to the precedence of a conditional operator INCDLUE GRAPH FROM ZYBOOKS ABOUT THE PRECEDNECE The Switch Statement The switch statement Is used to select among statements from several alternatives Kind of like a checklist If some cases, the switch statement can be used instead of the if/else if statements Switch statement format switch (expression) //integer { o case exp1: statement1; o case exp2: statement2; o ... o case expn: statementn; o default: statementn+1; } Switch statement requirements 1. Expression must be an integer variable or an expression that evaluates to an integer value 2. Exp1 through expn must be constant integer expressions or literals and must be unique in the switch statement 3. Default is optional but is recommended Switch statement – how it works 1. Expression is evaluated 2. The value of expression is compared against exp1 through expn 3. If expression matches the value expi, then the program branches to the statement following expi and continues to the end of the switch 4. If no matching value is found, the program branches to the statement after default. Break statement In a switch statement, you use the break statement You use it to exit the switch statement Or else the program will “fall through” aka the remaining statements in the switch statements will execute Always remember the switch statement! Using switch in menu systems The switch statement is a natural choice for a menu-driven program o Displays the menu o Then get the user’s menu selection o Use user input as expression in switch statement o Use menu choices as expr in case statements More About Blocks and Scope DON’T UNDERSTAND READ THE TEXTBOOK AND FILL IN More about blocks and scope Scope of the variable is the block in which it is defined from the point of definition to the end of the block Usually defined at the beginning of function May be defined close to first use Variables with the same name Variables defined inside { } have local or a block scope When inside a block within another block, you can define variables with the same name as in the outer block o When in inner block, outer definition is not available o Not a good idea