CSI 1430 Exam 3: IF-ELSE Branches and Equality
32 Questions
0 Views

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

When comparing floating-point numbers, which approach is recommended due to their imprecise representation?

  • Comparing their string representations.
  • Using the equality operator (==).
  • Using the inequality operator (!=).
  • Using a tolerance range to check if they are close enough. (correct)

In an if-else statement, both branches of code are always executed, regardless of the condition.

False (B)

What is the purpose of using curly braces {} in the context of if statements?

grouping statements

The ________ operator evaluates to true if the left and right sides are not equal.

<p>inequality</p> Signup and view all the answers

Match the following operators with their correct descriptions:

<p>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. Relational Operator (&gt;) = Checks if the left operand's value is greater than the right operand's value. Logical Operator (&amp;&amp;) = Treats operands as true or false, and evaluates to true only if both operands are true.</p> Signup and view all the answers

Which of the following is true regarding string comparison?

<p>Strings are equal if they have the same number of characters and corresponding characters are identical (case is important). (B)</p> Signup and view all the answers

Explain the concept of a nested if-else statement.

<p>if-else statements within an if-else statement</p> Signup and view all the answers

What is the outcome of the expression (5 > 3) && (1 < 4)?

<p>true (B)</p> Signup and view all the answers

Given a string myString = "programming", what would myString.at(4) return?

<p><code>r</code> (D)</p> Signup and view all the answers

The size() and length() methods for strings return potentially different values, with size() representing the actual memory allocated and length() representing the number of characters.

<p>False (B)</p> Signup and view all the answers

How can you add the character '!' to the end of the string variable greeting using the push_back() method?

<p>greeting.push_back('!')</p> Signup and view all the answers

Given the expression (a || b) && !c, which operation is evaluated first according to operator precedence?

<p>Parentheses <code>()</code> (C)</p> Signup and view all the answers

If you want to locate the first occurrence of the substring "abc" within the string longString, you would use the ______ method.

<p>find()</p> Signup and view all the answers

In C++, using & instead of && will always result in a compile-time error.

<p>False (B)</p> Signup and view all the answers

Match the cctype function with its functionality:

<p>isalpha(c) = Checks if character <code>c</code> is an alphabetic letter. isdigit(c) = Checks if character <code>c</code> is a digit. isspace(c) = Checks if character <code>c</code> is a whitespace character. toupper(c) = Converts character <code>c</code> to uppercase.</p> Signup and view all the answers

Which of the following statements about the substr() function is most accurate?

<p>It returns a new string that is a contiguous part of the original string. (C)</p> Signup and view all the answers

The insert() function replaces existing text within a string.

<p>False (B)</p> Signup and view all the answers

Explain in what context you might omit a break statement in a switch statement.

<p>Omitting <code>break</code> is used when you want multiple cases to execute the same block of code.</p> Signup and view all the answers

What is the purpose of string::npos when using the find() method?

<p>Indicates that the search failed to find the substring. (B)</p> Signup and view all the answers

A boolean variable can have only two possible values: true or ______.

<p>false</p> Signup and view all the answers

Which of the following operations correctly appends the string ", world!" to the string "Hello" and assigns the result to newString?

<p><code>newString</code> = <code>&quot;Hello&quot;</code> + <code>&quot;, world!&quot;</code> (D)</p> Signup and view all the answers

The replace() function takes a starting index, the number of characters to replace, and the ______ to insert.

<p>new text</p> Signup and view all the answers

Match the following operators with their descriptions:

<p>&amp;&amp; = Logical AND - returns true if both operands are true || = Logical OR - returns true if at least one operand is true ! = Logical NOT - returns true if the operand is false == = Equality - returns true if two values are equal</p> Signup and view all the answers

How do you modify the string text = "example" so it becomes "EXample", changing only the first character to uppercase, utilizing the appropriate cctype function?

<p>text.at(0) = toupper(text.at(0))</p> Signup and view all the answers

What is the correct syntax for a conditional expression?

<p>condition ? exprWhenTrue : exprWhenFalse (D)</p> Signup and view all the answers

What is the result of comparing the strings "apple" and "Apple" using relational operators in C++?

<p><code>&quot;apple&quot;</code> is greater than <code>&quot;Apple&quot;</code> (A)</p> Signup and view all the answers

Why should floating-point numbers be compared using a threshold (epsilon) instead of the '==' operator?

<p>Floating-point numbers cannot be exactly represented due to limited memory bits. (D)</p> Signup and view all the answers

Define 'short circuit evaluation' in the context of logical operators.

<p>skips evaluating later operands if the result of the logical operator can already be determined.</p> Signup and view all the answers

Given the following code snippet, what will be the value of 'result'? bool a = true; bool b = false; bool result = a && (b || !b);

<p>true (B)</p> Signup and view all the answers

In short circuit evaluation, True && operand2 will always evaluate operand2.

<p>True (A)</p> Signup and view all the answers

What happens if no case matches the switch expression, and there is no default case?

<p>The switch statement is skipped, and execution continues after the switch block. (C)</p> Signup and view all the answers

Match the following logical expressions with their short-circuit evaluation behavior:

<p>False &amp;&amp; operand2 = operand2 is not evaluated True &amp;&amp; operand2 = operand2 is evaluated True || operand2 = operand2 is not evaluated False || operand2 = operand2 is evaluated</p> Signup and view all the answers

Flashcards

Logical AND (&&)

Returns true only if both operands are true.

Logical OR (||)

Returns true if at least one operand is true.

Logical NOT (!)

Returns true if the operand is false, and vice versa.

Operator Precedence

The order in which operations are performed in an expression.

Signup and view all the flashcards

Bitwise vs. Logical Error

Using '&' or '|' instead of '&&' or '||'; can cause unexpected results.

Signup and view all the flashcards

Switch Statement

A control statement that compares a variable to constant values.

Signup and view all the flashcards

Boolean

A data type with two possible values: true or false.

Signup and view all the flashcards

String Comparison

Compares strings character by character, based on ASCII/Unicode values.

Signup and view all the flashcards

Branch (in programming)

A sequence of statements executed only if a condition is met.

Signup and view all the flashcards

If Branch

A branch that executes only if a specified condition is true.

Signup and view all the flashcards

If-Else Branch

A structure with two branches: one executes if a condition is true, the other if false.

Signup and view all the flashcards

If Statement

Executes a block of code if a condition is true.

Signup and view all the flashcards

Equality Operator (==)

A symbol (==) that checks if two values are equal.

Signup and view all the flashcards

Inequality Operator (!=)

A symbol (!=) that checks if two values are NOT equal.

Signup and view all the flashcards

Relational Operator

An operator that compares two operands (e.g., greater than, less than).

Signup and view all the flashcards

String

A sequence of characters stored in memory. Each character has an index (position number), starting from 0.

Signup and view all the flashcards

at()

Accesses the character at a specific index within a string. Allows for both viewing and changing the character at that position.

Signup and view all the flashcards

size()

Returns the number of characters in the string.

Signup and view all the flashcards

push_back()

Adds a single character to the end of an existing string.

Signup and view all the flashcards

append()

Adds one string to the end of another string.

Signup and view all the flashcards

Exception

A detected runtime error that typically prints an error message and terminates the program.

Signup and view all the flashcards

isalpha(c)

Returns true if the character is an alphabet letter (A-Z, a-z).

Signup and view all the flashcards

find()

Locates the first occurrence of a specified substring within a string, returning its starting index. If the substring is not found, it returns string::npos.

Signup and view all the flashcards

Substring

A contiguous sequence of characters within a string.

Signup and view all the flashcards

substr()

A function used to extract a substring from a string. Requires a starting index and number of characters.

Signup and view all the flashcards

insert()

A function used to insert text into a string at a specified index.

Signup and view all the flashcards

replace()

A function used to replace a portion of a string with new text. Requires a starting index, number of characters to replace, and the new text.

Signup and view all the flashcards

Conditional Expression

An expression that evaluates one of two expressions based on a condition.

Signup and view all the flashcards

Ternary Operator

The symbols '?' and ':' used in a conditional expression.

Signup and view all the flashcards

Short Circuit Evaluation

An optimization technique where the second operand of a logical operator is not evaluated if the result can be determined by the first operand alone.

Signup and view all the flashcards

How does Short Circuit work

skips evaluating later operands if the result of the logical operator can already be determined. && short circuits if the first operand evaluates to false. || short circuits to true if the first operand is true.

Signup and view all the flashcards

Study Notes

  • CSI 1430 Mastery Exam 3 study notes

IF-ELSE Branches (General)

  • Branch = sequence of statements executed under a condition.
  • If branch = branch only taken if an expression is true
  • If-Else Branch = has two branches
  • The first branch executes if an expression is true
  • Else the other branch is executed

Detecting Equal Values with Branches

  • If Statement = executes a group of statements if an expression is true
    • Surrounded by Braces {} which represent a grouping, such as statements
  • Equality Operator (==) = results in true if the left and right sides are equal
  • Inequality Operator (!=) = results in true if the left and right sides are not equal, or different
  • Expressions involving these 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
  • Relational and Equality operators can work for integer, character, and floating-point built-in types
  • Compares ASCII numerical encoding
  • Floating-point types should not be compared using the equality operators because of the imprecise representation of floating-point numbers
  • Can also be used for string type
  • Strings are equal if they have the same number of characters and corresponding characters and are identical

Detecting Ranges with Branches

  • Relational Operator = checks how one operand's value relates to another, like being greater than
    • Some operators involve two characters in a specific order
    • a < b means a is less than b
    • a > b means a is greater than b
    • a <= b means a is less than or equal to b
    • a >= b means a is greater than or equal to b

Detecting Ranges Using Logical Operators

  • 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 bora || 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 = the order in which operators are evaluated in an expression:
  • () Parentheses
  • ! Logical NOT
  • */%+- 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
  • Bitwise Operators = using a singular & or | when trying to use logical operators, may yield different behavior

Switch Statements

  • Switch = clearly represents multi-branch behavior involving a variable being compared to constant values
  • Case = each scenario whose constant expression matches the value of the switch expression
  • 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, but may be useful when multiple cases should yield the same result

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

  • Equal strings have the same number of characters, and each corresponding character is identical
  • 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.
    • “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 = a sequence of characters in memory
    • Each string character has a position number called an index, starting with 0
  • at() = the notation someString.at(x) accesses the character at index x of a string
    • A character in a string can be assigned (e.g., userString.at(3) = 'X'; changes userString to “abcXe”)
  • size() = returns the string's length
  • push_back() = adds a character to the end of a string
  • append() = adds one string to the end of another
  • The + operator can return a new string with one string appended to another string
  • size() and length() both return string length

Common Errors

  • Exception = detected runtime error that often 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[0]) but doesn't provide error checking

Character Operations

  • cctype library (#includ ) = character type
    • isalpha(c) = returns true if alphabetic
    • isdigit(c) = returns true if a digit
    • isspace(c) = returns true if whitespace
    • toupper(c) = returns uppercase version of that index (letter = toupper('a'))
    • tolower(c) = returns lowercase version of that index (letter = tolower('A'))

Finding, Inserting and Replacing 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”
  • 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 expression = has the form: condition ? exprWhenTrue : exprWhenFalse
  • If the condition evaluates to true, exprWhenTrue is evaluated
  • If the condition evaluates to false, then exprWhenFalse is evaluated
  • Ternary operator used the “?” and “:” in the conditional expression

Floating-Point Comparison

  • Floating-point numbers should not be compared using == because they cannot be exactly represented
    • Use < 0.0001 and fabs()
    • Epsilon = difference threshold indicating that floating-point numbers are equal
    • Floating point numbers are not always stored with 100% accuracy

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

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

Related Documents

Description

Study notes covering IF-ELSE branches, including the use of equality (==) and inequality (!=) operators in conditional statements. Also covers Boolean expressions and nested IF-ELSE structures. For CSI 1430 Exam 3.

More Like This

Python If-Else Statements Quiz
30 questions
If-Else Statements in Programming
16 questions
Use Quizgecko on...
Browser
Browser