Summary

These notes cover key concepts for CSI 1430 Mastery Exam 3, including if-else branches, and string comparisons. The document reviews the use of logical operators and switch statements for various programming scenarios, which should help in coding exercises.

Full Transcript

CSI 1430​ MASTERY EXAM 3 IF-ELSE BRANCHES (GENERAL) Branch Basics (If) ​ Branch = a sequence of statements only executed under a certain condition ○​ If branch = a branch taken only IF an expression is true ○​ If-Else Branch = has two branches, first branch is executed I...

CSI 1430​ MASTERY EXAM 3 IF-ELSE BRANCHES (GENERAL) Branch Basics (If) ​ Branch = a sequence of statements only executed under a certain condition ○​ If branch = a branch taken only IF an expression is true ○​ If-Else Branch = has two branches, first branch is executed IF an expression is true, ELSE the other branch is executed. DETECTING EQUAL VALUES WITH BRANCHES Detecting If Two Items are Equal Using an If Statement ​ If Statement = executes a group of statements IF an expression is true ○​ Surrounded by Braces {} = curly braces, represent a grouping, such as a grouping of statements ○​ Equality Operator (==) = evaluates to true if the left and right sides are equal. ○​ Inequality Operator (!=) == evaluates to true if the left and right sides are not equal, or different ​ Expressions involving these two operators evaluates to a Boolean ​ Boolean = a type that has just two values, true or false ​ If-Else Statement = executes one group of statements when an expression is true, and another group of statements when the expression is false. ○​ Nested If-Else Statements = if-else statements WITHIN an if-else statement Comparing Characters, Strings, and Floating-Point Types ​ Relational and Equality operators work for integer, character, and floating-point built-in types. 1 ○​ Compares their ASCII numerical encoding ○​ Floating-point types should not be compared using the equality operators, due to the imprecise representation of floating-point numbers. ○​ Can also be used for string type. ​ Strings are equal if they have the same # of characters & corresponding characters are identical. (case is important) DETECTING RANGES WITH BRANCHES Relational Operators ​ Relational Operator = checks how one operand’s value relates to another, like being greater than. ○​ Some operators involve two characters, order matters. ​ a < b means a is less than b ​ a > b means a is greater than b ​ a = b means a is greater than or equal to b DETECTING RANGES USING LOGICAL OPERATORS Logical AND, OR, and NOT ​ Logical Operator = treat operands as being true or false, and evaluates to true or false. ○​ a AND b or a && b returns true when both of its operands are true ○​ a OR b or a || b returns true when at least one of its two operands are true ○​ NOT a or !a returns true when its one operand is false, and vice-versa ORDER OF EVALUATION Precedence Rules ​ Precedence Rules = the order in which operators are evaluated in an expression ○​ () Parentheses ○​ ! Logical NOT 2 ○​ */%+- Arithmetic Operators (using their precedence rules) ​ () Parenthesis ​ Unary - ​ */% Complex Arithmetic ​ + - Simple Arithmetic ​ Left-To-Right ○​ < >= Relational Operators ○​ == != Equality and Inequality Operators ○​ && Logical AND ○​ || Logical OR Common Error: Bitwise Rather than Logical Operators ​ Bitwise Operators = using a singular & or | when trying to use logical operators, may yield different behavior Switch Statements Switch Statement ​ Switch = can more clearly represent multi-branch behavior involving a variable being compared to constant values ○​ ○​ Case = each scenario whose constant expression matches the value of the switch expression 3 ​ If no case matches, then the default case statements are executed. Omitting the Break Statement ​ Omitting Break = will cause the statements within the next case to be executed, only use when multiple cases should yield the same result. ○​ BOOLEAN DATA TYPE Boolean Data Type ​ Boolean = refers to a quantity that has only two possible values, true or false (bool) ○​ isWeekend = true; ○​ isLargeParty = (partySize > 6); STRING COMPARISONS String Comparison: Equality ​ Equal strings have the same # of characters, and each corresponding character is identical (using == or !=) String Comparison: Relational ​ Begins at index 0, and compares each character until the evaluation results in false, or the end of a string is reached. ○​ ‘A’ is 65, ‘B’ is 66, etc, while ‘a’ is 97, ‘b’ is 98, etc. ​ So “Apples" is less than “apples” because 65 is less than 97. ○​ If existing characters are equal, the shorter string is less than. STRING ACCESS OPERATIONS String Character Indices 4 ​ String = a sequence of characters in memory ○​ Each string character has a position number called an index, starting with 0 (not 1) Accessing & Changing String Characters ​ at() = the notation someString.at(x) accesses the character at index x of a string ○​ A character in a string can be assigned. If userString is “abcde”, then userString.at(3) = ‘X’; changes userString to “abcXe” ​ size() = returns the strings length ​ push_back() = adds a character to the end of a string ○​ s1 = “Why”, s1.push_back(‘?’); changes s1 to “Why?” ​ append() = adds one string to the end of another ○​ s1 = “Hey”, s1.append(“!!!”); changes s1 to “Hey!!!” ​ The + operator can return a new string with one string appended to another string. ○​ If s1 is “Hey”, then s2 = s1 + “!!!”; assigns s2 with “Hey!!!” ​ size() and length() both return string length Common Errors ​ Exception = is a detected runtime error that commonly prints an error message and terminates the program ○​.at generates an exception if the index is out of range for the string’s size ​ Brackets are allowed (ex. someString) but doesn’t provide error checking. (Not good practice) CHARACTER OPERATIONS Character Operations ​ cctype library (#includ ) = character type ○​ isalpha(c) = returns true if alphabetic ○​ isdigit(c) = returns true if digit ○​ isspace(c) = returns true if whitespace ○​ toupper(c) = returns uppercase version of that index (letter = toupper(‘a’)) 5 ○​ tolower(c) = returns lowercase version of that index (letter = tolower(‘A’)) FINDING, INSERTING, AND REPLACING TEXT IN A STRING Finding Text in a String ​ find() = used to find a string or character in a string (s1.find(textToFind) ○​ textToFind can be a string or character ○​ string::npos = special value representing “no position” ​ if (domainIndex != string::npos) {... Finding Text Starting from the Middle of a String ​ find(textToFind, startIndex) = returns the index of the first occurrence of textToFind starting at index startIndex instead of index 0. Getting a Substring ​ Substring = a contiguous part of a string (“world” is a substring of “Hello world”) ○​ substr() = used to get a substring of a string (s1.substr(startIndex, numCharacters)) Inserting and Replacing Text in a String ​ insert() = inserts text into a string (s1.insert(startIndex, newText)) ​ replace() = replaces text in a string (s1.replace(startIndex, numCharacters, newText)) CONDITIONAL EXPRESSIONS Conditional Expressions ​ Conditional expression = has the form ○​ condition ? exprWhenTrue : exprWhenFalse ○​ If the condition evaluates to true, then exprWhenTrue is evaluated. If the condition evaluates to false, then exprWhenFalse is evaluated. ○​ Ternary operator = the “?’ and “:” used in the conditional expression 6 FLOATING-POINT COMPARISON Floating-Point Comparison ​ Floating-point numbers should not be compared using == because they cannot be exactly represented in the limited available memory bits like 64. Numbers expected to be equal may be close but not exactly equal. ○​ Use < 0.0001 and fabs() ○​ Epsilon = the difference threshold indicating that floating-point numbers are equal ○​ Not always store with some inaccuracy SHORT CIRCUIT EVALUATION Short Circuit Evaluation ​ Short circuit evaluation = skips evaluating later operands if the result of the logical operator can already be determined. ○​ AND short circuits if the first operand evaluates to false ​ True && operand2 => If the first operand evaluates to true, operand2 is evaluated. ​ False && operand2 => If the first operand evaluates to false, the result is always false, so operand2 is not evaluated. ○​ OR short circuits to true if the first operand is true ​ True || operand2 => If the first operand evaluates to true, the result is always true, so operand2 is not evaluated. ​ False || operand2 => If the first operand evaluates to false, operand2 is evaluated. 7

Use Quizgecko on...
Browser
Browser